[英]Adding metric labels in prometheus on the fly
I have a counter metric in prometheus.我在普罗米修斯中有一个计数器指标。 I want to add lables to it dynamically for example if my request comes http://abc123.com/{p1}
,I want my custom_metric_name to store {statuscode=200, p1=p1Value, host="abc123"}
and if request comes http://def123.com/ {p2}.我想动态添加标签,例如如果我的请求来自http://abc123.com/{p1}
,我希望我的 custom_metric_name 存储{statuscode=200, p1=p1Value, host="abc123"}
并且如果请求来自http://def123.com/ {p2}。 I want custom_metric_name to store {statuscode=200, p2=p2Value, host="def123"}
but custom_metric_name
will be shared metric by both.我希望 custom_metric_name 存储{statuscode=200, p2=p2Value, host="def123"}
但custom_metric_name
将由两者共享指标。
I am trying still not able to get answer我正在尝试仍然无法得到答案
You can use relabel_config or metric_relabel_config in your Prometheus config. 您可以在Prometheus配置中使用relabel_config或metric_relabel_config 。
It would look like the following: 它看起来如下:
- source_labels: [request_origin]
regex: 'http://(\w+)/.*'
replacement: '${1}'
target_label: host
See also this article showing usage of relabelling. 另请参阅本文,其中显示了重新标记的用法。
Dynamic labels can be easily added to the exported Prometheus metrics when using github.com/VictoriaMetrics/metrics Go package:使用github.com/VictoriaMetrics/metrics Go package 时,可以轻松地将动态标签添加到导出的 Prometheus 指标中:
import (
"fmt"
"github.com/VictoriaMetrics/metrics"
)
// CountRequest increments `requests_total{statuscode="statusCode", host="host", path="path"}` Prometheus counter
func CountRequest(statusCode int, host, path string) {
s := fmt.Sprintf(`requests_total{statuscode="%d", host=%q, path=%q}`, statusCode, host, path)
metrics.GetOrCreateCounter(s).Inc()
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.