Prometheus – 向度量标签添加新标签会导致“不一致的标签基数”错误。

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

Prometheus - Adding new labels to gauge results in an `inconsistent label cardinality` error

问题

我有一个Go应用程序,它将数据发送到Prometheus的仪表盘。

...
import (
	"github.com/prometheus/client_golang/prometheus"
	"github.com/prometheus/client_golang/prometheus/promauto"
)
...
var gauge = promauto.NewGaugeVec(prometheus.GaugeOpts{
	Name: "some_name",
	Help: "some desc",
},
	[]string{"labelA", "labelB"},
)
...
// 发送数据到仪表盘
gauge.With(prometheus.Labels{
  "labelA": "...",
  "labelB": "...",
})

然后,我修改了应用程序,添加了第三个标签(labelC)。

...
var gauge = promauto.NewGaugeVec(prometheus.GaugeOpts{
    Name: "some_name",
    Help: "some desc",
},
    []string{"labelA", "labelB", "labelC"},
)
...
gauge.With(prometheus.Labels{
  "labelA": "...",
  "labelB": "...",
  "labelC": "...",
})

但是,当我运行包含新标签的应用程序时,我收到以下错误:

panic: inconsistent label cardinality: expected ... label values but got ... in prometheus.Labels{...}

错误发生在调用gauge.With时。

有人知道为什么会出现这个错误吗?

英文:

I have a Go app that sends data to a prometheus gauge

...
import (
	"github.com/prometheus/client_golang/prometheus"
	"github.com/prometheus/client_golang/prometheus/promauto"
)
...
var gauge = promauto.NewGaugeVec(prometheus.GaugeOpts{
	Name: "some_name",
	Help: "some desc",
},
	[]string{"labelA", "labelB"},
)
...
// sending data to gauge
gauge.With(prometheus.Labels{
  "labelA": "...",
  "labelB": "...",
})

I then modified the app to include a third label (labelC)

...
var gauge = promauto.NewGaugeVec(prometheus.GaugeOpts{
    Name: "some_name",
    Help: "some desc",
},
    []string{"labelA", "labelB", "labelC"},
)
...
gauge.With(prometheus.Labels{
  "labelA": "...",
  "labelB": "...",
  "labelC": "...",
})

But now when I run the app that contains the new label, I get this error

panic: inconsistent label cardinality: expected ... label values but got ... in prometheus.Labels{...}

the error happens when calling gauge.With

Anyone has any idea why?

答案1

得分: 3

客户端库将在With中的标签数量与NewGaugeVec中的标签数量不匹配时抛出此错误。因此,您可能忘记在代码中的某个地方添加labelC: "..."。您应该能够在堆栈跟踪中找到该行。

英文:

The client library will throw this error if the number of labels in With doesn't match the number of labels in NewGaugeVec. So you likely forgot to add labelC: "..." somewhere in your code. You should be able to find the line in the stack trace.

huangapple
  • 本文由 发表于 2022年1月9日 23:47:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/70642937.html
匿名

发表评论

匿名网友

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

确定