@@ -63,10 +63,10 @@ func LabelsToSignature(labels map[string]string) uint64 {
63
63
hb := getHashAndBuf ()
64
64
defer putHashAndBuf (hb )
65
65
66
- for k , v := range labels {
67
- hb .b .WriteString (k )
66
+ for labelName , labelValue := range labels {
67
+ hb .b .WriteString (labelName )
68
68
hb .b .WriteByte (SeparatorByte )
69
- hb .b .WriteString (v )
69
+ hb .b .WriteString (labelValue )
70
70
hb .h .Write (hb .b .Bytes ())
71
71
result ^= hb .h .Sum64 ()
72
72
hb .h .Reset ()
@@ -75,23 +75,79 @@ func LabelsToSignature(labels map[string]string) uint64 {
75
75
return result
76
76
}
77
77
78
- // LabelValuesToSignature returns a unique signature (i.e., fingerprint) for the
79
- // values of a given label set.
80
- func LabelValuesToSignature (labels map [string ]string ) uint64 {
81
- if len (labels ) == 0 {
78
+ // metricToFingerprint works exactly as LabelsToSignature but takes a Metric as
79
+ // parameter (rather than a label map) and returns a Fingerprint.
80
+ func metricToFingerprint (m Metric ) Fingerprint {
81
+ if len (m ) == 0 {
82
+ return Fingerprint (emptyLabelSignature )
83
+ }
84
+
85
+ var result uint64
86
+ hb := getHashAndBuf ()
87
+ defer putHashAndBuf (hb )
88
+
89
+ for labelName , labelValue := range m {
90
+ hb .b .WriteString (string (labelName ))
91
+ hb .b .WriteByte (SeparatorByte )
92
+ hb .b .WriteString (string (labelValue ))
93
+ hb .h .Write (hb .b .Bytes ())
94
+ result ^= hb .h .Sum64 ()
95
+ hb .h .Reset ()
96
+ hb .b .Reset ()
97
+ }
98
+ return Fingerprint (result )
99
+ }
100
+
101
+ // SignatureForLabels works like LabelsToSignature but takes a Metric as
102
+ // parameter (rather than a label map) and only includes the labels with the
103
+ // specified LabelNames into the signature calculation.
104
+ func SignatureForLabels (m Metric , labels LabelNames ) uint64 {
105
+ if len (m ) == 0 || len (labels ) == 0 {
106
+ return emptyLabelSignature
107
+ }
108
+
109
+ var result uint64
110
+ hb := getHashAndBuf ()
111
+ defer putHashAndBuf (hb )
112
+
113
+ for _ , label := range labels {
114
+ hb .b .WriteString (string (label ))
115
+ hb .b .WriteByte (SeparatorByte )
116
+ hb .b .WriteString (string (m [label ]))
117
+ hb .h .Write (hb .b .Bytes ())
118
+ result ^= hb .h .Sum64 ()
119
+ hb .h .Reset ()
120
+ hb .b .Reset ()
121
+ }
122
+ return result
123
+ }
124
+
125
+ // SignatureWithoutLabels works like LabelsToSignature but takes a Metric as
126
+ // parameter (rather than a label map) and excludes the labels with any of the
127
+ // specified LabelNames from the signature calculation.
128
+ func SignatureWithoutLabels (m Metric , labels map [LabelName ]struct {}) uint64 {
129
+ if len (m ) == 0 {
82
130
return emptyLabelSignature
83
131
}
84
132
85
133
var result uint64
86
134
hb := getHashAndBuf ()
87
135
defer putHashAndBuf (hb )
88
136
89
- for _ , v := range labels {
90
- hb .b .WriteString (v )
137
+ for labelName , labelValue := range m {
138
+ if _ , exclude := labels [labelName ]; exclude {
139
+ continue
140
+ }
141
+ hb .b .WriteString (string (labelName ))
142
+ hb .b .WriteByte (SeparatorByte )
143
+ hb .b .WriteString (string (labelValue ))
91
144
hb .h .Write (hb .b .Bytes ())
92
145
result ^= hb .h .Sum64 ()
93
146
hb .h .Reset ()
94
147
hb .b .Reset ()
95
148
}
149
+ if result == 0 {
150
+ return emptyLabelSignature
151
+ }
96
152
return result
97
153
}
0 commit comments