...
1
2
3
4
5
6
7
8
9
10
11
12
13
14 package model
15
16 import (
17 "sort"
18 )
19
20
21
22
23 const SeparatorByte byte = 255
24
25
26 var emptyLabelSignature = hashNew()
27
28
29
30
31 func LabelsToSignature(labels map[string]string) uint64 {
32 if len(labels) == 0 {
33 return emptyLabelSignature
34 }
35
36 labelNames := make([]string, 0, len(labels))
37 for labelName := range labels {
38 labelNames = append(labelNames, labelName)
39 }
40 sort.Strings(labelNames)
41
42 sum := hashNew()
43 for _, labelName := range labelNames {
44 sum = hashAdd(sum, labelName)
45 sum = hashAddByte(sum, SeparatorByte)
46 sum = hashAdd(sum, labels[labelName])
47 sum = hashAddByte(sum, SeparatorByte)
48 }
49 return sum
50 }
51
52
53
54 func labelSetToFingerprint(ls LabelSet) Fingerprint {
55 if len(ls) == 0 {
56 return Fingerprint(emptyLabelSignature)
57 }
58
59 labelNames := make(LabelNames, 0, len(ls))
60 for labelName := range ls {
61 labelNames = append(labelNames, labelName)
62 }
63 sort.Sort(labelNames)
64
65 sum := hashNew()
66 for _, labelName := range labelNames {
67 sum = hashAdd(sum, string(labelName))
68 sum = hashAddByte(sum, SeparatorByte)
69 sum = hashAdd(sum, string(ls[labelName]))
70 sum = hashAddByte(sum, SeparatorByte)
71 }
72 return Fingerprint(sum)
73 }
74
75
76
77
78 func labelSetToFastFingerprint(ls LabelSet) Fingerprint {
79 if len(ls) == 0 {
80 return Fingerprint(emptyLabelSignature)
81 }
82
83 var result uint64
84 for labelName, labelValue := range ls {
85 sum := hashNew()
86 sum = hashAdd(sum, string(labelName))
87 sum = hashAddByte(sum, SeparatorByte)
88 sum = hashAdd(sum, string(labelValue))
89 result ^= sum
90 }
91 return Fingerprint(result)
92 }
93
94
95
96
97
98 func SignatureForLabels(m Metric, labels ...LabelName) uint64 {
99 if len(labels) == 0 {
100 return emptyLabelSignature
101 }
102
103 sort.Sort(LabelNames(labels))
104
105 sum := hashNew()
106 for _, label := range labels {
107 sum = hashAdd(sum, string(label))
108 sum = hashAddByte(sum, SeparatorByte)
109 sum = hashAdd(sum, string(m[label]))
110 sum = hashAddByte(sum, SeparatorByte)
111 }
112 return sum
113 }
114
115
116
117
118 func SignatureWithoutLabels(m Metric, labels map[LabelName]struct{}) uint64 {
119 if len(m) == 0 {
120 return emptyLabelSignature
121 }
122
123 labelNames := make(LabelNames, 0, len(m))
124 for labelName := range m {
125 if _, exclude := labels[labelName]; !exclude {
126 labelNames = append(labelNames, labelName)
127 }
128 }
129 if len(labelNames) == 0 {
130 return emptyLabelSignature
131 }
132 sort.Sort(labelNames)
133
134 sum := hashNew()
135 for _, labelName := range labelNames {
136 sum = hashAdd(sum, string(labelName))
137 sum = hashAddByte(sum, SeparatorByte)
138 sum = hashAdd(sum, string(m[labelName]))
139 sum = hashAddByte(sum, SeparatorByte)
140 }
141 return sum
142 }
143
View as plain text