Like Prometheus, but for logs.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 
loki/pkg/ingester/client/compat.go

49 lines
1.3 KiB

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(lbls labels.Labels) model.Fingerprint {
sum := hashNew()
lbls.Range(func(label labels.Label) {
sum = hashAddString(sum, label.Name)
sum = hashAddByte(sum, model.SeparatorByte)
sum = hashAddString(sum, label.Value)
sum = hashAddByte(sum, model.SeparatorByte)
})
return model.Fingerprint(sum)
}