我正在尝试向推送网关中的指标添加标签。这是我正在使用的代码:
completionTime := prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "completion_timestamp_seconds",
Help: "The timestamp of the last successful completion.",
},
[]string{"cluster"},
)
completionTime.With(prometheus.Labels{"cluster": cluster}).SetToCurrentTime()
if err := push.New(fmt.Sprintf(pushgatewayIngress, cluster), "job_completion_time").
Collector(completionTime).
Push(); err != nil {
fmt.Println("Could not push completion time to Pushgateway:", err)
}
指标正在更新,但不包括标签。我需要向 Collector
添加一些内容吗?
最佳答案
您能分享一下您的 prometheus scrape 配置吗?
我根据你的例子做了一个最小的例子,标签似乎在普罗米修斯方面正确更新。
main.go
package main
import (
"fmt"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/push"
)
const (
pushgatewayIngress = "http://localhost:9091"
cluster = "testCluster"
)
func main() {
completionTime := prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "completion_timestamp_seconds",
Help: "The timestamp of the last successful completion.",
},
[]string{"cluster"},
)
completionTime.With(prometheus.Labels{"cluster": cluster}).SetToCurrentTime()
if err := push.New(pushgatewayIngress, "job_completion_time").
Collector(completionTime).
Push(); err != nil {
fmt.Println("Could not push completion time to Pushgateway:", err)
}
fmt.Println("done")
}
prometheus.yml - (配置文件)
global:
scrape_interval: 15s
scrape_timeout: 10s
evaluation_interval: 15s
scrape_configs:
- job_name: pushgateway
scrape_interval: 5s
static_configs:
- targets: ['pushgateway:9091']
docker-compose.yml - 在容器中为pushgateway和prometheus设置容器
pushgateway:
image: prom/pushgateway
ports:
- 9091:9091
prometheus:
image: prom/prometheus
ports:
- 9090:9090
links:
- pushgateway:pushgateway
volumes:
- ./prometheus.yml:/etc/prometheus/prometheus.yml
您从包含 docker-compose.yml 和 prometheus.yml 的文件夹中运行 docker-compose up,这应该可以工作(它在我这边工作)。您发现这与您的配置之间有什么区别吗?
请注意,您可能需要在抓取配置中使用 honor_labels: true
来获取正确的标签集合,如 here 中所述。
关于go - 为什么我的普罗米修斯标签不显示?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50935264/