Skip to content

Commit da11cf2

Browse files
authored
Merge pull request #488 from peterj/master
Add more info to the inconsistent cardinality errors
2 parents 1444b91 + f1bec54 commit da11cf2

File tree

6 files changed

+24
-7
lines changed

6 files changed

+24
-7
lines changed

prometheus/counter.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ func NewCounterVec(opts CounterOpts, labelNames []string) *CounterVec {
136136
return &CounterVec{
137137
metricVec: newMetricVec(desc, func(lvs ...string) Metric {
138138
if len(lvs) != len(desc.variableLabels) {
139-
panic(errInconsistentCardinality)
139+
panic(makeInconsistentCardinalityError(desc.fqName, desc.variableLabels, lvs))
140140
}
141141
result := &counter{desc: desc, labelPairs: makeLabelPairs(desc, lvs)}
142142
result.init(result) // Init self-collection.

prometheus/examples_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ func ExampleRegister() {
282282
// taskCounter unregistered.
283283
// taskCounterVec not registered: a previously registered descriptor with the same fully-qualified name as Desc{fqName: "worker_pool_completed_tasks_total", help: "Total number of tasks completed.", constLabels: {}, variableLabels: [worker_id]} has different label names or a different help string
284284
// taskCounterVec registered.
285-
// Worker initialization failed: inconsistent label cardinality
285+
// Worker initialization failed: inconsistent label cardinality: expected 1 label values but got 2 in []string{"42", "spurious arg"}
286286
// notMyCounter is nil.
287287
// taskCounterForWorker42 registered.
288288
// taskCounterForWorker2001 registered.

prometheus/gauge.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ func NewGaugeVec(opts GaugeOpts, labelNames []string) *GaugeVec {
147147
return &GaugeVec{
148148
metricVec: newMetricVec(desc, func(lvs ...string) Metric {
149149
if len(lvs) != len(desc.variableLabels) {
150-
panic(errInconsistentCardinality)
150+
panic(makeInconsistentCardinalityError(desc.fqName, desc.variableLabels, lvs))
151151
}
152152
result := &gauge{desc: desc, labelPairs: makeLabelPairs(desc, lvs)}
153153
result.init(result) // Init self-collection.

prometheus/histogram.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ func NewHistogram(opts HistogramOpts) Histogram {
165165

166166
func newHistogram(desc *Desc, opts HistogramOpts, labelValues ...string) Histogram {
167167
if len(desc.variableLabels) != len(labelValues) {
168-
panic(errInconsistentCardinality)
168+
panic(makeInconsistentCardinalityError(desc.fqName, desc.variableLabels, labelValues))
169169
}
170170

171171
for _, n := range desc.variableLabels {

prometheus/labels.go

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,22 @@ const reservedLabelPrefix = "__"
3737

3838
var errInconsistentCardinality = errors.New("inconsistent label cardinality")
3939

40+
func makeInconsistentCardinalityError(fqName string, labels, labelValues []string) error {
41+
return fmt.Errorf(
42+
"%s: %q has %d variable labels named %q but %d values %q were provided",
43+
errInconsistentCardinality, fqName,
44+
len(labels), labels,
45+
len(labelValues), labelValues,
46+
)
47+
}
48+
4049
func validateValuesInLabels(labels Labels, expectedNumberOfValues int) error {
4150
if len(labels) != expectedNumberOfValues {
42-
return errInconsistentCardinality
51+
return fmt.Errorf(
52+
"%s: expected %d label values but got %d in %#v",
53+
errInconsistentCardinality, expectedNumberOfValues,
54+
len(labels), labels,
55+
)
4356
}
4457

4558
for name, val := range labels {
@@ -53,7 +66,11 @@ func validateValuesInLabels(labels Labels, expectedNumberOfValues int) error {
5366

5467
func validateLabelValues(vals []string, expectedNumberOfValues int) error {
5568
if len(vals) != expectedNumberOfValues {
56-
return errInconsistentCardinality
69+
return fmt.Errorf(
70+
"%s: expected %d label values but got %d in %#v",
71+
errInconsistentCardinality, expectedNumberOfValues,
72+
len(vals), vals,
73+
)
5774
}
5875

5976
for _, val := range vals {

prometheus/summary.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ func NewSummary(opts SummaryOpts) Summary {
181181

182182
func newSummary(desc *Desc, opts SummaryOpts, labelValues ...string) Summary {
183183
if len(desc.variableLabels) != len(labelValues) {
184-
panic(errInconsistentCardinality)
184+
panic(makeInconsistentCardinalityError(desc.fqName, desc.variableLabels, labelValues))
185185
}
186186

187187
for _, n := range desc.variableLabels {

0 commit comments

Comments
 (0)