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 4 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
1 change: 1 addition & 0 deletions agent/metrics_agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ import (
_ "flashcat.cloud/categraf/inputs/whois"
_ "flashcat.cloud/categraf/inputs/xskyapi"
_ "flashcat.cloud/categraf/inputs/zookeeper"
_ "flashcat.cloud/categraf/inputs/iptables"
Copy link
Collaborator

Choose a reason for hiding this comment

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

怎么还多了一个iptables

)

type MetricsAgent struct {
Expand Down
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
Loading