英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论