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.
|
|
|
|
|
2014-04-25 14:22:17 +04:00
|
|
|
package model
|
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-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
|
|
|
|
}
|
|
|
|
|
2014-04-25 14:22:17 +04:00
|
|
|
names := make([]string, 0, len(labels))
|
2013-06-27 20:46:16 +04:00
|
|
|
for name := range labels {
|
2014-04-25 14:22:17 +04:00
|
|
|
names = append(names, name)
|
2013-01-19 17:48:30 +04:00
|
|
|
}
|
|
|
|
|
2014-04-25 14:22:17 +04:00
|
|
|
sort.Strings(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))
|
2014-04-25 14:22:17 +04:00
|
|
|
hasher.Write([]byte(labels[name]))
|
2014-04-14 20:45:16 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return hasher.Sum64()
|
|
|
|
}
|
|
|
|
|
|
|
|
// LabelValuesToSignature provides a way of building a unique signature
|
|
|
|
// (i.e., fingerprint) for a given set of label's values.
|
2014-04-25 14:22:17 +04:00
|
|
|
func LabelValuesToSignature(labels map[string]string) uint64 {
|
2014-04-14 20:45:16 +04:00
|
|
|
if len(labels) == 0 {
|
|
|
|
return emptyLabelSignature
|
|
|
|
}
|
|
|
|
|
2014-04-25 14:22:17 +04:00
|
|
|
names := make([]string, 0, len(labels))
|
2014-04-14 20:45:16 +04:00
|
|
|
for name := range labels {
|
2014-04-25 14:22:17 +04:00
|
|
|
names = append(names, name)
|
2014-04-14 20:45:16 +04:00
|
|
|
}
|
|
|
|
|
2014-04-25 14:22:17 +04:00
|
|
|
sort.Strings(names)
|
2014-04-14 20:45:16 +04:00
|
|
|
|
|
|
|
hasher := fnv.New64a()
|
|
|
|
|
|
|
|
for _, name := range names {
|
2014-04-25 14:22:17 +04:00
|
|
|
hasher.Write([]byte(labels[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
|
|
|
}
|