From da330f4281d04542d662046e5540fad08b4ac38a Mon Sep 17 00:00:00 2001 From: beorn7 Date: Mon, 3 Sep 2018 16:20:55 +0200 Subject: [PATCH] Un-export prometheus.LabelPairSorter The only known external usage of it was in prometheus/pushgateway, where it was removed by https://github.com/prometheus/pushgateway/pull/200 . Originally, the expectation was that users would implement the Metric interface now and then. As we know now, neither it is happening, nor would it make a lot of sense. (Users implement the Collector interface instead.) By now, LabelPairSorter is essentially noise in the already quite cluttered namespace in the prometheus package. Signed-off-by: beorn7 --- prometheus/desc.go | 2 +- prometheus/examples_test.go | 14 -------------- prometheus/metric.go | 18 ++++++++---------- prometheus/registry.go | 4 ++-- prometheus/value.go | 2 +- 5 files changed, 12 insertions(+), 28 deletions(-) diff --git a/prometheus/desc.go b/prometheus/desc.go index 4a755b0..e959162 100644 --- a/prometheus/desc.go +++ b/prometheus/desc.go @@ -156,7 +156,7 @@ func NewDesc(fqName, help string, variableLabels []string, constLabels Labels) * Value: proto.String(v), }) } - sort.Sort(LabelPairSorter(d.constLabelPairs)) + sort.Sort(labelPairSorter(d.constLabelPairs)) return d } diff --git a/prometheus/examples_test.go b/prometheus/examples_test.go index 5f909a6..775e03c 100644 --- a/prometheus/examples_test.go +++ b/prometheus/examples_test.go @@ -19,7 +19,6 @@ import ( "math" "net/http" "runtime" - "sort" "strings" "time" @@ -168,19 +167,6 @@ func ExampleInstrumentHandler() { )) } -func ExampleLabelPairSorter() { - labelPairs := []*dto.LabelPair{ - {Name: proto.String("status"), Value: proto.String("404")}, - {Name: proto.String("method"), Value: proto.String("get")}, - } - - sort.Sort(prometheus.LabelPairSorter(labelPairs)) - - fmt.Println(labelPairs) - // Output: - // [name:"method" value:"get" name:"status" value:"404" ] -} - func ExampleRegister() { // Imagine you have a worker pool and want to count the tasks completed. taskCounter := prometheus.NewCounter(prometheus.CounterOpts{ diff --git a/prometheus/metric.go b/prometheus/metric.go index 06897f2..bfda075 100644 --- a/prometheus/metric.go +++ b/prometheus/metric.go @@ -46,9 +46,8 @@ type Metric interface { // While populating dto.Metric, it is the responsibility of the // implementation to ensure validity of the Metric protobuf (like valid // UTF-8 strings or syntactically valid metric and label names). It is - // recommended to sort labels lexicographically. (Implementers may find - // LabelPairSorter useful for that.) Callers of Write should still make - // sure of sorting if they depend on it. + // recommended to sort labels lexicographically. Callers of Write should + // still make sure of sorting if they depend on it. Write(*dto.Metric) error // TODO(beorn7): The original rationale of passing in a pre-allocated // dto.Metric protobuf to save allocations has disappeared. The @@ -113,20 +112,19 @@ func BuildFQName(namespace, subsystem, name string) string { return name } -// LabelPairSorter implements sort.Interface. It is used to sort a slice of -// dto.LabelPair pointers. This is useful for implementing the Write method of -// custom metrics. -type LabelPairSorter []*dto.LabelPair +// labelPairSorter implements sort.Interface. It is used to sort a slice of +// dto.LabelPair pointers. +type labelPairSorter []*dto.LabelPair -func (s LabelPairSorter) Len() int { +func (s labelPairSorter) Len() int { return len(s) } -func (s LabelPairSorter) Swap(i, j int) { +func (s labelPairSorter) Swap(i, j int) { s[i], s[j] = s[j], s[i] } -func (s LabelPairSorter) Less(i, j int) bool { +func (s labelPairSorter) Less(i, j int) bool { return s[i].GetName() < s[j].GetName() } diff --git a/prometheus/registry.go b/prometheus/registry.go index 79c3dd6..5652850 100644 --- a/prometheus/registry.go +++ b/prometheus/registry.go @@ -819,7 +819,7 @@ 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)) + sort.Sort(labelPairSorter(dtoMetric.Label)) for _, lp := range dtoMetric.Label { h = hashAdd(h, lp.GetName()) h = hashAddByte(h, separatorByte) @@ -863,7 +863,7 @@ func checkDescConsistency( metricFamily.GetName(), dtoMetric, desc, ) } - sort.Sort(LabelPairSorter(lpsFromDesc)) + sort.Sort(labelPairSorter(lpsFromDesc)) for i, lpFromDesc := range lpsFromDesc { lpFromMetric := dtoMetric.Label[i] if lpFromDesc.GetName() != lpFromMetric.GetName() || diff --git a/prometheus/value.go b/prometheus/value.go index 9fb7eab..e066298 100644 --- a/prometheus/value.go +++ b/prometheus/value.go @@ -153,6 +153,6 @@ func makeLabelPairs(desc *Desc, labelValues []string) []*dto.LabelPair { }) } labelPairs = append(labelPairs, desc.constLabelPairs...) - sort.Sort(LabelPairSorter(labelPairs)) + sort.Sort(labelPairSorter(labelPairs)) return labelPairs }