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

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

最佳答案

您能分享一下您的 prometheus scrape 配置吗?

我根据你的例子做了一个最小的例子,标签似乎在普罗米修斯方面正确更新。

ma​​in.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/

相关文章:

google-app-engine - 如何管理 App Engine Go 运行时上下文以避免 App Engine 锁定?

go - 从 Golang image.Image 检索像素数据时,img.At() 或 rgba.Pix() 更快/更好吗?

prometheus - Prometheus 中的标签是什么?

spring-boot - 使用千分尺时 Prometheus 端点错误

kubernetes - 纠正 GKE 集群中的时钟偏差

arrays - slice - 容量/长度?

go - 如何遍历特定文件夹

go - mmonit golang 重启缓慢且状态不存在

node.js - NodeJS API 服务失败如何重启?

Prometheus 从 Docker Swarm 中的多个容器中抓取特定的微服务指标