prometheus的ConstLabels取值

huangapple go评论79阅读模式
英文:

prometheus ConstLabels take value

问题

我是你的中文翻译助手,以下是你要翻译的内容:

我对Golang还很陌生,也许我的问题很简单,但我已经尝试了很多方法来解决它。
我正在尝试编写一个简单的prometheus导出器。
我的代码从gitlab API获取一个指标,我只想添加ConstLabels。
我的代码如下:

func enqueueJob() {
	for {
		dat, err := getJobData()
		if err != nil {
			fmt.Println(err)
		}
		time.Sleep(10 * time.Second)
		jobsInQueues.Set(dat[0].QueuedDuration)
	}

}

var jobsInQueues = promauto.NewGauge(
	prometheus.GaugeOpts{
		Name:        "A_jobs_panding",
		Help:        "A_Jobs Pending",
		ConstLabels: prometheus.Labels{"name": dat[0].Name},
	},
)

func main() {
	enqueueJob()
	http.Handle("/metrics", promhttp.Handler())
	http.ListenAndServe(":2112", nil)
}

问题是如何将getJobData()函数中的dat[0].Name传递给jobsInQueues变量。
getJobData函数返回一个Job结构体。

type Job struct {
	ID             int      `json:"id"`
	Status         string   `json:"status"`
	Stage          string   `json:"stage"`
	Name           string   `json:"name"`
	QueuedDuration float64  `json:"queued_duration"`
	TagList        []string `json:"tag_list"`
}

如果你在函数外定义了var dat, err = getJobData(),它不会在for循环外更新Name的值,我理解这一点。

英文:

I'm new to Golang and maybe my question is simple, but I've tried many ways to do this.
I am trying to write a simple exporter for prometheus.
My code takes a metric from the gitlab API and I want it to add only ConstLabels.
My code:

func enqueueJob() {
	for {
		dat, err := getJobData()
		if err != nil {
			fmt.Println(err)
		}
		time.Sleep(10 * time.Second)
		jobsInQueues.Set(dat[0].QueuedDuration)
	}

}

var jobsInQueues = promauto.NewGauge(
	prometheus.GaugeOpts{
		Name:        "A_jobs_panding",
		Help:        "A_Jobs Pending",
		ConstLabels: prometheus.Labels{"name": dat[0].Name},
	},
)

func main() {
	enqueueJob()
	http.Handle("/metrics", promhttp.Handler())
	http.ListenAndServe(":2112", nil)
}

The question is how can I pass to jobsInQueues, dat[0].Name from the getJobData() function
getJobData returns a Job struct?

type Job struct {
	ID             int      `json:"id"`
	Status         string   `json:"status"`
	Stage          string   `json:"stage"`
	Name           string   `json:"name"`
	QueuedDuration float64  `json:"queued_duration"`
	TagList        []string `json:"tag_list"`
}

If you define var dat, err = getJobData() outside of the function, it doesn't update the value of Name outside of for and I understand that

答案1

得分: 0

如其名,ConstLabels 是一组标签(键值对),存在于指标上且不可更改。看起来你想要一个标签的值在每次执行时都不同。

如果是这样的话,ConstLabels 并不是你要找的。相反,你应该考虑使用一个带有 name 标签的 GaugeVec(测量向量)。

将你的指标定义如下:

var jobsInQueues = promauto.NewGaugeVec(
	prometheus.GaugeOpts{
		Name: "A_jobs_panding",
		Help: "A_Jobs Pending",
	},
	[]string{"name"},
)

然后,通过从 getJobData 中提供名称和值来设置该测量向量:

func enqueueJob() {
    for {
        dat, err := getJobData()
        if err != nil {
            fmt.Println(err)
        }
        time.Sleep(10 * time.Second)
        jobsInQueues.With(prometheus.Labels{
            "name": dat[0].Name,
        }).Set(dat[0].QueuedDuration)
    }

}

请注意,enqueueJob 是你代码中的一个阻塞操作,因此它永远不会启动 HTTP 服务器。你需要在它们自己的 goroutine 上运行 enqueueJobhttp.ListenAndServe。此外,考虑一下 Gitlab API 返回的可能不同的名称以及它们对指标的基数的影响也是值得考虑的。

英文:

As the name implies, ConstLabels are a set of labels (=key-value pairs) that are present on the metric and cannot be changed. It seems you'd like to have a label where the value differs per execution.

If so, ConstLabels is not what you're looking for. Instead, you should be looking at using a GaugeVec (gauge vector) with a label name.

Define your metric as such:

var jobsInQueues = promauto.NewGaugeVec(
	prometheus.GaugeOpts{
		Name: "A_jobs_panding",
		Help: "A_Jobs Pending",
	},
	[]string{"name"},
)

and then, set the gauge by providing the name an value from getJobData:

func enqueueJob() {
    for {
        dat, err := getJobData()
        if err != nil {
            fmt.Println(err)
        }
        time.Sleep(10 * time.Second)
        jobsInQueues.With(prometheus.Labels{
            "name": dat[0].Name,
        }).Set(dat[0].QueuedDuration)
    }

}

Note that enqueueJob is a blocking operation in your code, so it will never get around to bringing up the HTTP server. You'll want to run either the enqueueJob or http.ListenAndServe invocations on their own goroutine. Also, it's worth considering the potential distinct names returned from the Gitlab API and how they affect your metric's cardinality.

huangapple
  • 本文由 发表于 2022年11月12日 18:14:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/74412021.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定