Skip to content

Commit

Permalink
pushgateway接口支持用户指定值置换agent_hostname
Browse files Browse the repository at this point in the history
  • Loading branch information
tianyanli committed Jun 27, 2024
1 parent b657029 commit e83f201
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 8 deletions.
18 changes: 15 additions & 3 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,13 +97,20 @@ 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 _, has := samples[i].Labels[agentHostnameLabelKey]; !has && AgentHostTag == "" {
samples[i].Labels[agentHostnameLabelKey] = config.Config.GetHostname()
} else if AgentHostTag != "" {
// 从当前现有的 Labels 中找到 key 等于 config.Config.HTTP.AgentHostTag 的值并置换到agent_hostname
if value, exists := samples[i].Labels[AgentHostTag]; exists {
samples[i].Labels[agentHostnameLabelKey] = value
}
}
}

}
writer.WriteSamples(samples)
c.String(200, "forwarding...")
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

1 comment on commit e83f201

@tianyanli
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

categraf 支持开启http服务后pushtateway接口后通过配置文件中agent_host_tag从推送的现有指标中指定一个key提取它的值置换到agent_hostname中,同时也直接动态生效模式,在推送给的指标中增加agent_host_tag的label指定的值就是用于置换的key。ignore_hostname在配置文件中为false才会生效。
动态配置方法
ignore_hostname //用于决定是否忽略主机名
ignore_global_labels //是否忽略标签
agent_host_tag //使用指定的key对应的值置换agent_hostname的值,为空的情况下默认使用categraf hostname配置

Please sign in to comment.