为什么我的普罗米修斯标签不显示?

时间:2018-06-19 18:58:01

标签: go prometheus

我正在尝试向推送网关中的指标添加标签。这是我正在使用的代码:

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添加一些内容?

1 个答案:

答案 0 :(得分:3)

您能分享您的普罗米修斯刮擦配置吗?

我根据您的情况做了一个最小的示例,标签在Prometheus方面似乎已正确更新。

main.go     包主

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 -配置文件     全球:       scrape_interval:15秒       scrape_timeout:10秒       Evaluation_interval:15秒

scrape_configs:
- job_name: pushgateway
  scrape_interval: 5s
  static_configs:
    - targets: ['pushgateway:9091']

docker-compose.yml     Pushgateway:       图片:舞会/ pushgateway       端口:         -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

所述