Skip to content

Commit

Permalink
支持开启http服务后pushtateway接口后通过配置文件中agent_host_tag从推送的数据中指定agent_hostname…
Browse files Browse the repository at this point in the history
…的值 (#984)

* iptables插件

* 添加注册

* pushgateway接口支持用户指定值置换agent_hostname

* pushgateway接口支持用户指定值置换agent_hostname

* 修改命名
  • Loading branch information
tianyanli authored Jun 27, 2024
1 parent 125be4f commit 07456d1
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 21 deletions.
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

0 comments on commit 07456d1

Please sign in to comment.