-
Notifications
You must be signed in to change notification settings - Fork 3.3k
/
compat.go
49 lines (42 loc) · 1.27 KB
/
compat.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package client
import (
"github.com/prometheus/common/model"
"github.com/prometheus/prometheus/model/labels"
"github.com/grafana/loki/v3/pkg/logproto"
)
const (
// offset64 is an offset require for the FNV (Fowler-Noll-Vo) hash function.
offset64 = 14695981039346656037
// prime64 is a 64bit prime used by the FNV hash function.
prime64 = 1099511628211
)
// hashNew initializes a new fnv64a hash value.
func hashNew() uint64 {
return offset64
}
// FastFingerprint runs the same algorithm as Prometheus labelSetToFastFingerprint()
func FastFingerprint(ls []logproto.LabelAdapter) model.Fingerprint {
if len(ls) == 0 {
return model.Metric(nil).FastFingerprint()
}
var result uint64
for _, l := range ls {
sum := hashNew()
sum = hashAdd(sum, l.Name)
sum = hashAddByte(sum, model.SeparatorByte)
sum = hashAdd(sum, l.Value)
result ^= sum
}
return model.Fingerprint(result)
}
// Fingerprint runs the same algorithm as Prometheus labelSetToFingerprint()
func Fingerprint(labels labels.Labels) model.Fingerprint {
sum := hashNew()
for _, label := range labels {
sum = hashAddString(sum, label.Name)
sum = hashAddByte(sum, model.SeparatorByte)
sum = hashAddString(sum, label.Value)
sum = hashAddByte(sum, model.SeparatorByte)
}
return model.Fingerprint(sum)
}