@@ -26,16 +26,68 @@ var separator = []byte{0}
26
26
// a singleton and refers to one and only one stream of samples.
27
27
type Metric map [LabelName ]LabelValue
28
28
29
- // Equal compares the fingerprints of both metrics.
29
+ // Equal compares the metrics.
30
30
func (m Metric ) Equal (o Metric ) bool {
31
- // TODO do an actual map comparison
32
- return m .Fingerprint ().Equal (o .Fingerprint ())
31
+ if len (m ) != len (o ) {
32
+ return false
33
+ }
34
+ for ln , lv := range m {
35
+ olv , ok := o [ln ]
36
+ if ! ok {
37
+ return false
38
+ }
39
+ if olv != lv {
40
+ return false
41
+ }
42
+ }
43
+ return true
33
44
}
34
45
35
- // Before compares the fingerprints of both metrics.
46
+ // Before compares the metrics, using the following criteria:
47
+ //
48
+ // If m has fewer labels than o, it is before o. If it has more, it is not.
49
+ //
50
+ // If the number of labels is the same, the superset of all label names is
51
+ // sorted alphanumerically. The first differing label pair found in that order
52
+ // determines the outcome: If the label does not exist at all in m, then m is
53
+ // before o, and vice versa. Otherwise the label value is compared
54
+ // alphanumerically.
55
+ //
56
+ // If m and o are equal, the method returns false.
36
57
func (m Metric ) Before (o Metric ) bool {
37
- // TODO do an actual map comparison
38
- return m .Fingerprint ().Less (o .Fingerprint ())
58
+ if len (m ) < len (o ) {
59
+ return true
60
+ }
61
+ if len (m ) > len (o ) {
62
+ return false
63
+ }
64
+
65
+ lns := make (LabelNames , 0 , len (m )+ len (o ))
66
+ for ln := range m {
67
+ lns = append (lns , ln )
68
+ }
69
+ for ln := range o {
70
+ lns = append (lns , ln )
71
+ }
72
+ // It's probably not worth it to de-dup lns.
73
+ sort .Sort (lns )
74
+ for _ , ln := range lns {
75
+ mlv , ok := m [ln ]
76
+ if ! ok {
77
+ return true
78
+ }
79
+ olv , ok := o [ln ]
80
+ if ! ok {
81
+ return false
82
+ }
83
+ if mlv < olv {
84
+ return true
85
+ }
86
+ if mlv > olv {
87
+ return false
88
+ }
89
+ }
90
+ return false
39
91
}
40
92
41
93
// String implements Stringer.
0 commit comments