2013-04-03 20:33:32 +04:00
|
|
|
// Copyright (c) 2013, Prometheus Team
|
2013-01-19 17:48:30 +04:00
|
|
|
// All rights reserved.
|
|
|
|
//
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2013-04-03 20:33:32 +04:00
|
|
|
package prometheus
|
2013-01-19 17:48:30 +04:00
|
|
|
|
|
|
|
import (
|
2013-06-27 20:46:16 +04:00
|
|
|
"hash/fnv"
|
2013-01-19 17:48:30 +04:00
|
|
|
"sort"
|
|
|
|
|
2013-06-27 20:46:16 +04:00
|
|
|
"github.com/prometheus/client_golang/model"
|
2013-01-19 17:48:30 +04:00
|
|
|
)
|
|
|
|
|
2013-09-11 19:16:34 +04:00
|
|
|
// cache the signature of an empty label set.
|
|
|
|
var emptyLabelSignature = fnv.New64a().Sum64()
|
|
|
|
|
2013-01-19 17:48:30 +04:00
|
|
|
// LabelsToSignature provides a way of building a unique signature
|
|
|
|
// (i.e., fingerprint) for a given label set sequence.
|
2014-04-09 22:38:51 +04:00
|
|
|
func LabelsToSignature(labels map[string]string) uint64 {
|
2013-09-11 19:16:34 +04:00
|
|
|
if len(labels) == 0 {
|
|
|
|
return emptyLabelSignature
|
|
|
|
}
|
|
|
|
|
2013-06-27 20:46:16 +04:00
|
|
|
names := make(model.LabelNames, 0, len(labels))
|
|
|
|
for name := range labels {
|
|
|
|
names = append(names, model.LabelName(name))
|
2013-01-19 17:48:30 +04:00
|
|
|
}
|
|
|
|
|
2013-06-27 20:46:16 +04:00
|
|
|
sort.Sort(names)
|
2013-01-19 17:48:30 +04:00
|
|
|
|
2013-06-27 20:46:16 +04:00
|
|
|
hasher := fnv.New64a()
|
2013-01-19 17:48:30 +04:00
|
|
|
|
2013-06-27 20:46:16 +04:00
|
|
|
for _, name := range names {
|
2014-04-14 20:45:16 +04:00
|
|
|
hasher.Write([]byte(name))
|
|
|
|
hasher.Write([]byte(labels[string(name)]))
|
|
|
|
}
|
|
|
|
|
|
|
|
return hasher.Sum64()
|
|
|
|
}
|
|
|
|
|
|
|
|
// LabelValuesToSignature provides a way of building a unique signature
|
|
|
|
// (i.e., fingerprint) for a given set of label's values.
|
|
|
|
func labelValuesToSignature(labels map[string]string) uint64 {
|
|
|
|
if len(labels) == 0 {
|
|
|
|
return emptyLabelSignature
|
|
|
|
}
|
|
|
|
|
|
|
|
names := make(model.LabelNames, 0, len(labels))
|
|
|
|
for name := range labels {
|
|
|
|
names = append(names, model.LabelName(name))
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Sort(names)
|
|
|
|
|
|
|
|
hasher := fnv.New64a()
|
|
|
|
|
|
|
|
for _, name := range names {
|
|
|
|
hasher.Write([]byte(labels[string(name)]))
|
2013-01-19 17:48:30 +04:00
|
|
|
}
|
|
|
|
|
2013-06-27 20:46:16 +04:00
|
|
|
return hasher.Sum64()
|
2013-01-19 17:48:30 +04:00
|
|
|
}
|