diff --git a/api/router_func.go b/api/router_func.go index 6c1794ec..fa4ae546 100644 --- a/api/router_func.go +++ b/api/router_func.go @@ -4,7 +4,6 @@ import ( "compress/gzip" "errors" "io" - "io/ioutil" "net/http" "github.com/gogo/protobuf/proto" @@ -15,29 +14,30 @@ import ( const agentHostnameLabelKey = "agent_hostname" func readerGzipBody(contentEncoding string, request *http.Request) (bytes []byte, err error) { - if contentEncoding == "gzip" { - var ( - r *gzip.Reader - ) + switch contentEncoding { + case "gzip": + var r *gzip.Reader r, err = gzip.NewReader(request.Body) if err != nil { return nil, err } - defer r.Close() - bytes, err = ioutil.ReadAll(r) - } else if contentEncoding == "snappy" { + + bytes, err = io.ReadAll(r) + case "snappy": defer request.Body.Close() - compressed, err := ioutil.ReadAll(request.Body) + var compressed []byte + compressed, err = io.ReadAll(request.Body) if err != nil { return nil, err } bytes, err = snappy.Decode(nil, compressed) - } else { + default: defer request.Body.Close() - bytes, err = ioutil.ReadAll(request.Body) + bytes, err = io.ReadAll(request.Body) } + if err != nil || len(bytes) == 0 { return nil, errors.New("request parameter error") } @@ -48,7 +48,7 @@ func readerGzipBody(contentEncoding string, request *http.Request) (bytes []byte // DecodeWriteRequest from an io.Reader into a prompb.WriteRequest, handling // snappy decompression. func DecodeWriteRequest(r io.Reader) (*prompb.WriteRequest, error) { - compressed, err := ioutil.ReadAll(r) + compressed, err := io.ReadAll(r) if err != nil { return nil, err } diff --git a/api/router_pushgateway.go b/api/router_pushgateway.go index 23fd880b..15ed1061 100644 --- a/api/router_pushgateway.go +++ b/api/router_pushgateway.go @@ -70,6 +70,11 @@ func pushgateway(c *gin.Context) { ignoreHostname := config.Config.HTTP.IgnoreHostname || c.GetBool("ignore_hostname") ignoreGlobalLabels := config.Config.HTTP.IgnoreGlobalLabels || c.GetBool("ignore_global_labels") + // 获取 AgentHostTag 的值 + agentHostTag := config.Config.HTTP.AgentHostTag + if agentHostTag == "" { + agentHostTag = c.GetString("agent_host_tag") + } now := time.Now() @@ -92,16 +97,25 @@ func pushgateway(c *gin.Context) { // add url labels for k, v := range labels { samples[i].Labels[k] = v + } // add label: agent_hostname - if _, has := samples[i].Labels[agentHostnameLabelKey]; !has && !ignoreHostname { - samples[i].Labels[agentHostnameLabelKey] = config.Config.GetHostname() + if !ignoreHostname { + if agentHostTag == "" { + if _, has := samples[i].Labels[agentHostnameLabelKey]; !has { + samples[i].Labels[agentHostnameLabelKey] = config.Config.GetHostname() + } + } else { + // 从当前现有的 Labels 中找到 key 等于 config.Config.HTTP.AgentHostTag 的值 + if value, exists := samples[i].Labels[agentHostTag]; exists { + samples[i].Labels[agentHostnameLabelKey] = value + } + } } - } writer.WriteSamples(samples) - c.String(200, "forwarding...") + c.String(http.StatusOK, "forwarding...") } // fork prometheus/pushgateway handler/push.go diff --git a/conf/config.toml b/conf/config.toml index 13bf40e6..39221909 100644 --- a/conf/config.toml +++ b/conf/config.toml @@ -84,6 +84,7 @@ address = ":9100" print_access = false run_mode = "release" ignore_hostname = false +agent_host_tag = "" ignore_global_labels = false [ibex] diff --git a/config/config.go b/config/config.go index c2543f2d..54c28acf 100644 --- a/config/config.go +++ b/config/config.go @@ -66,11 +66,13 @@ type WriterOption struct { } type HTTP struct { - Enable bool `toml:"enable"` - Address string `toml:"address"` - PrintAccess bool `toml:"print_access"` - RunMode string `toml:"run_mode"` - IgnoreHostname bool `toml:"ignore_hostname"` + Enable bool `toml:"enable"` + Address string `toml:"address"` + PrintAccess bool `toml:"print_access"` + RunMode string `toml:"run_mode"` + IgnoreHostname bool `toml:"ignore_hostname"` + // The tag used to name the agent host + AgentHostTag string `toml:"agent_host_tag"` IgnoreGlobalLabels bool `toml:"ignore_global_labels"` CertFile string `toml:"cert_file"` KeyFile string `toml:"key_file"`