Debian Codegraph @

prometheus/internal/metric.go

raw
1 // Copyright 2018 The Prometheus Authors
2 // Licensed under the Apache License, Version 2.0 (the "License");
3 // you may not use this file except in compliance with the License.
4 // You may obtain a copy of the License at
5 //
6 // http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13
14 package internal
15
16 import (
17 "sort"
18
19 dto "github.com/prometheus/client_model/go"
20 )
21
22 // LabelPairSorter implements sort.Interface. It is used to sort a slice of
23 // dto.LabelPair pointers.
24 type LabelPairSorter []*dto.LabelPair
25
26 func (s LabelPairSorter) Len() int {
27 return len(s)
28 }
29
30 func (s LabelPairSorter) Swap(i, j int) {
31 s[i], s[j] = s[j], s[i]
32 }
33
34 func (s LabelPairSorter) Less(i, j int) bool {
35 return s[i].GetName() < s[j].GetName()
36 }
37
38 // MetricSorter is a sortable slice of *dto.Metric.
39 type MetricSorter []*dto.Metric
40
41 func (s MetricSorter) Len() int {
42 return len(s)
43 }
44
45 func (s MetricSorter) Swap(i, j int) {
46 s[i], s[j] = s[j], s[i]
47 }
48
49 func (s MetricSorter) Less(i, j int) bool {
50 if len(s[i].Label) != len(s[j].Label) {
51 // This should not happen. The metrics are
52 // inconsistent. However, we have to deal with the fact, as
53 // people might use custom collectors or metric family injection
54 // to create inconsistent metrics. So let's simply compare the
55 // number of labels in this case. That will still yield
56 // reproducible sorting.
57 return len(s[i].Label) < len(s[j].Label)
58 }
59 for n, lp := range s[i].Label {
60 vi := lp.GetValue()
61 vj := s[j].Label[n].GetValue()
62 if vi != vj {
63 return vi < vj
64 }
65 }
66
67 // We should never arrive here. Multiple metrics with the same
68 // label set in the same scrape will lead to undefined ingestion
69 // behavior. However, as above, we have to provide stable sorting
70 // here, even for inconsistent metrics. So sort equal metrics
71 // by their timestamp, with missing timestamps (implying "now")
72 // coming last.
73 if s[i].TimestampMs == nil {
74 return false
75 }
76 if s[j].TimestampMs == nil {
77 return true
78 }
79 return s[i].GetTimestampMs() < s[j].GetTimestampMs()
80 }
81
82 // NormalizeMetricFamilies returns a MetricFamily slice with empty
83 // MetricFamilies pruned and the remaining MetricFamilies sorted by name within
84 // the slice, with the contained Metrics sorted within each MetricFamily.
85 func NormalizeMetricFamilies(metricFamiliesByName map[string]*dto.MetricFamily) []*dto.MetricFamily {
86 for _, mf := range metricFamiliesByName {
87 sort.Sort(MetricSorter(mf.Metric))
88 }
89 names := make([]string, 0, len(metricFamiliesByName))
90 for name, mf := range metricFamiliesByName {
91 if len(mf.Metric) > 0 {
92 names = append(names, name)
93 }
94 }
95 sort.Strings(names)
96 result := make([]*dto.MetricFamily, 0, len(names))
97 for _, name := range names {
98 result = append(result, metricFamiliesByName[name])
99 }
100 return result
101 }