From fae889635ce96b79dbe0882363682a9bf45b7268 Mon Sep 17 00:00:00 2001 From: beorn7 Date: Thu, 6 Dec 2018 11:35:30 +0100 Subject: [PATCH] Fix #512 Signed-off-by: beorn7 --- prometheus/registry.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/prometheus/registry.go b/prometheus/registry.go index f98c81a..8301fc8 100644 --- a/prometheus/registry.go +++ b/prometheus/registry.go @@ -872,7 +872,13 @@ func checkMetricConsistency( h = hashAddByte(h, separatorByte) // Make sure label pairs are sorted. We depend on it for the consistency // check. - sort.Sort(labelPairSorter(dtoMetric.Label)) + if !sort.IsSorted(labelPairSorter(dtoMetric.Label)) { + // We cannot sort dtoMetric.Label in place as it is immutable by contract. + copiedLabels := make([]*dto.LabelPair, len(dtoMetric.Label)) + copy(copiedLabels, dtoMetric.Label) + sort.Sort(labelPairSorter(copiedLabels)) + dtoMetric.Label = copiedLabels + } for _, lp := range dtoMetric.Label { h = hashAdd(h, lp.GetName()) h = hashAddByte(h, separatorByte)