Skip to content

Commit edb489a

Browse files
author
beorn7
committed
Add check for duplicated label names
Fixes #471 Signed-off-by: beorn7 <[email protected]>
1 parent dd9e125 commit edb489a

File tree

2 files changed

+24
-10
lines changed

2 files changed

+24
-10
lines changed

prometheus/registry.go

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -787,6 +787,8 @@ func checkMetricConsistency(
787787
dtoMetric *dto.Metric,
788788
metricHashes map[uint64]struct{},
789789
) error {
790+
name := metricFamily.GetName()
791+
790792
// Type consistency with metric family.
791793
if metricFamily.GetType() == dto.MetricType_GAUGE && dtoMetric.Gauge == nil ||
792794
metricFamily.GetType() == dto.MetricType_COUNTER && dtoMetric.Counter == nil ||
@@ -795,33 +797,42 @@ func checkMetricConsistency(
795797
metricFamily.GetType() == dto.MetricType_UNTYPED && dtoMetric.Untyped == nil {
796798
return fmt.Errorf(
797799
"collected metric %q { %s} is not a %s",
798-
metricFamily.GetName(), dtoMetric, metricFamily.GetType(),
800+
name, dtoMetric, metricFamily.GetType(),
799801
)
800802
}
801803

804+
previousLabelName := ""
802805
for _, labelPair := range dtoMetric.GetLabel() {
803-
if !checkLabelName(labelPair.GetName()) {
806+
labelName := labelPair.GetName()
807+
if labelName == previousLabelName {
808+
return fmt.Errorf(
809+
"collected metric %q { %s} has two or more labels with the same name: %s",
810+
name, dtoMetric, labelName,
811+
)
812+
}
813+
if !checkLabelName(labelName) {
804814
return fmt.Errorf(
805815
"collected metric %q { %s} has a label with an invalid name: %s",
806-
metricFamily.GetName(), dtoMetric, labelPair.GetName(),
816+
name, dtoMetric, labelName,
807817
)
808818
}
809-
if dtoMetric.Summary != nil && labelPair.GetName() == quantileLabel {
819+
if dtoMetric.Summary != nil && labelName == quantileLabel {
810820
return fmt.Errorf(
811821
"collected metric %q { %s} must not have an explicit %q label",
812-
metricFamily.GetName(), dtoMetric, quantileLabel,
822+
name, dtoMetric, quantileLabel,
813823
)
814824
}
815825
if !utf8.ValidString(labelPair.GetValue()) {
816826
return fmt.Errorf(
817827
"collected metric %q { %s} has a label named %q whose value is not utf8: %#v",
818-
metricFamily.GetName(), dtoMetric, labelPair.GetName(), labelPair.GetValue())
828+
name, dtoMetric, labelName, labelPair.GetValue())
819829
}
830+
previousLabelName = labelName
820831
}
821832

822833
// Is the metric unique (i.e. no other metric with the same name and the same labels)?
823834
h := hashNew()
824-
h = hashAdd(h, metricFamily.GetName())
835+
h = hashAdd(h, name)
825836
h = hashAddByte(h, separatorByte)
826837
// Make sure label pairs are sorted. We depend on it for the consistency
827838
// check.
@@ -835,7 +846,7 @@ func checkMetricConsistency(
835846
if _, exists := metricHashes[h]; exists {
836847
return fmt.Errorf(
837848
"collected metric %q { %s} was collected before with the same name and label values",
838-
metricFamily.GetName(), dtoMetric,
849+
name, dtoMetric,
839850
)
840851
}
841852
metricHashes[h] = struct{}{}

prometheus/registry_test.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,10 @@ collected metric named "complex_count" collides with previously collected histog
330330
},
331331
},
332332
}
333-
duplicateLabelMsg := []byte(`duplicate label mumble mumble`)
333+
duplicateLabelMsg := []byte(`An error has occurred during metrics gathering:
334+
335+
collected metric "broken_metric" { label:<name:"foo" value:"bar" > label:<name:"foo" value:"baz" > counter:<value:2.7 > } has two or more labels with the same name: foo
336+
`)
334337

335338
type output struct {
336339
headers map[string]string
@@ -675,7 +678,7 @@ collected metric named "complex_count" collides with previously collected histog
675678
},
676679
out: output{
677680
headers: map[string]string{
678-
"Content-Type": `text/plain; version=0.0.4; charset=utf-8`,
681+
"Content-Type": `text/plain; charset=utf-8`,
679682
},
680683
body: duplicateLabelMsg,
681684
},

0 commit comments

Comments
 (0)