Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

支持开启http服务后pushtateway接口后通过配置文件中agent_host_tag从推送的数据中指定agent_hostname的值 #984

Merged
merged 6 commits into from
Jun 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions api/router_func.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"compress/gzip"
"errors"
"io"
"io/ioutil"
"net/http"

"github.com/gogo/protobuf/proto"
Expand All @@ -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")
}
Expand All @@ -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
}
Expand Down
22 changes: 18 additions & 4 deletions api/router_pushgateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions conf/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ address = ":9100"
print_access = false
run_mode = "release"
ignore_hostname = false
agent_host_tag = ""
ignore_global_labels = false

[ibex]
Expand Down
12 changes: 7 additions & 5 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
Loading