我正在尝试向推送网关中的指标添加标签。这是我正在使用的代码:
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)
}
Run Code Online (Sandbox Code Playgroud)
指标正在更新,但不包括标签。我需要添加一些东西Collector吗?
你能分享一下你的 prometheus 抓取配置吗?
我根据你的例子做了一个最小的例子,标签似乎在普罗米修斯方面得到了正确更新。
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")
}
Run Code Online (Sandbox Code Playgroud)
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']
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
您docker-compose up从带有 docker-compose.yml 和 prometheus.yml 的文件夹运行,这应该可以工作(它在我这边工作)。你觉得这和你的配置有什么区别吗?
请注意,你可能需要使用honor_labels: true你的刮配置了正确的标签集合,描述在这里
| 归档时间: |
|
| 查看次数: |
1113 次 |
| 最近记录: |