簡體   English   中英

即時在普羅米修斯中添加指標標簽

[英]Adding metric labels in prometheus on the fly

我在普羅米修斯中有一個計數器指標。 我想動態添加標簽,例如如果我的請求來自http://abc123.com/{p1} ,我希望我的 custom_metric_name 存儲{statuscode=200, p1=p1Value, host="abc123"}並且如果請求來自http://def123.com/ {p2}。 我希望 custom_metric_name 存儲{statuscode=200, p2=p2Value, host="def123"}custom_metric_name將由兩者共享指標。

我正在嘗試仍然無法得到答案

您可以在Prometheus配置中使用relabel_configmetric_relabel_config

它看起來如下:

- source_labels: [request_origin]
  regex: 'http://(\w+)/.*'
  replacement: '${1}'
  target_label: host

另請參閱本文,其中顯示了重新標記的用法。

使用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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM