Move signature.go and related tests to the model package.

The LabelsToSignature function is now used outside of the prometheus
package, too. Leaving it in the prometheuos package is misleading
design and will lead to circulat import chains soon.

Change-Id: If1ca442d4023b33b138cf79fee68e82ff2a355be
This commit is contained in:
Bjoern Rabenstein 2014-04-25 12:22:17 +02:00
parent ad3452a46c
commit e5dc0421cd
8 changed files with 35 additions and 28 deletions

View File

@ -4,13 +4,11 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package prometheus
package model
import (
"hash/fnv"
"sort"
"github.com/prometheus/client_golang/model"
)
// cache the signature of an empty label set.
@ -23,18 +21,18 @@ func LabelsToSignature(labels map[string]string) uint64 {
return emptyLabelSignature
}
names := make(model.LabelNames, 0, len(labels))
names := make([]string, 0, len(labels))
for name := range labels {
names = append(names, model.LabelName(name))
names = append(names, name)
}
sort.Sort(names)
sort.Strings(names)
hasher := fnv.New64a()
for _, name := range names {
hasher.Write([]byte(name))
hasher.Write([]byte(labels[string(name)]))
hasher.Write([]byte(labels[name]))
}
return hasher.Sum64()
@ -42,22 +40,22 @@ func LabelsToSignature(labels map[string]string) uint64 {
// 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 {
func LabelValuesToSignature(labels map[string]string) uint64 {
if len(labels) == 0 {
return emptyLabelSignature
}
names := make(model.LabelNames, 0, len(labels))
names := make([]string, 0, len(labels))
for name := range labels {
names = append(names, model.LabelName(name))
names = append(names, name)
}
sort.Sort(names)
sort.Strings(names)
hasher := fnv.New64a()
for _, name := range names {
hasher.Write([]byte(labels[string(name)]))
hasher.Write([]byte(labels[name]))
}
return hasher.Sum64()

View File

@ -4,14 +4,16 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package prometheus
package model
import (
"runtime"
"testing"
"github.com/prometheus/client_golang/test"
)
func testLabelsToSignature(t tester) {
func testLabelsToSignature(t test.Tester) {
var scenarios = []struct {
in map[string]string
out uint64
@ -66,7 +68,7 @@ func BenchmarkLabelToSignature(b *testing.B) {
func benchmarkLabelValuesToSignature(b *testing.B, l map[string]string, e uint64) {
for i := 0; i < b.N; i++ {
if a := labelValuesToSignature(l); a != e {
if a := LabelValuesToSignature(l); a != e {
b.Fatalf("expected signature of %d for %s, got %d", e, l, a)
}
}

View File

@ -14,6 +14,8 @@ import (
dto "github.com/prometheus/client_model/go"
"code.google.com/p/goprotobuf/proto"
"github.com/prometheus/client_golang/model"
)
// TODO(matt): Refactor to de-duplicate behaviors.
@ -49,7 +51,7 @@ func (metric *counter) Set(labels map[string]string, value float64) float64 {
labels = blankLabelsSingleton
}
signature := labelValuesToSignature(labels)
signature := model.LabelValuesToSignature(labels)
metric.mutex.Lock()
defer metric.mutex.Unlock()
@ -67,7 +69,7 @@ func (metric *counter) Set(labels map[string]string, value float64) float64 {
}
func (metric *counter) Reset(labels map[string]string) {
signature := labelValuesToSignature(labels)
signature := model.LabelValuesToSignature(labels)
metric.mutex.Lock()
defer metric.mutex.Unlock()
@ -100,7 +102,7 @@ func (metric *counter) IncrementBy(labels map[string]string, value float64) floa
labels = blankLabelsSingleton
}
signature := labelValuesToSignature(labels)
signature := model.LabelValuesToSignature(labels)
metric.mutex.Lock()
defer metric.mutex.Unlock()
@ -126,7 +128,7 @@ func (metric *counter) DecrementBy(labels map[string]string, value float64) floa
labels = blankLabelsSingleton
}
signature := labelValuesToSignature(labels)
signature := model.LabelValuesToSignature(labels)
metric.mutex.Lock()
defer metric.mutex.Unlock()

View File

@ -14,6 +14,8 @@ import (
"code.google.com/p/goprotobuf/proto"
dto "github.com/prometheus/client_model/go"
"github.com/prometheus/client_golang/model"
)
// A gauge metric merely provides an instantaneous representation of a scalar
@ -55,7 +57,7 @@ func (metric *gauge) Set(labels map[string]string, value float64) float64 {
labels = blankLabelsSingleton
}
signature := labelValuesToSignature(labels)
signature := model.LabelValuesToSignature(labels)
metric.mutex.Lock()
defer metric.mutex.Unlock()
@ -73,7 +75,7 @@ func (metric *gauge) Set(labels map[string]string, value float64) float64 {
}
func (metric *gauge) Reset(labels map[string]string) {
signature := labelValuesToSignature(labels)
signature := model.LabelValuesToSignature(labels)
metric.mutex.Lock()
defer metric.mutex.Unlock()

View File

@ -18,6 +18,8 @@ import (
dto "github.com/prometheus/client_model/go"
"code.google.com/p/goprotobuf/proto"
"github.com/prometheus/client_golang/model"
)
// This generates count-buckets of equal size distributed along the open
@ -101,7 +103,7 @@ func (h *histogram) Add(labels map[string]string, value float64) {
labels = blankLabelsSingleton
}
signature := labelValuesToSignature(labels)
signature := model.LabelValuesToSignature(labels)
var histogram *histogramVector = nil
h.mutex.Lock()
@ -298,7 +300,7 @@ func (h *histogram) Purge() {
}
func (h *histogram) Reset(labels map[string]string) {
signature := labelValuesToSignature(labels)
signature := model.LabelValuesToSignature(labels)
h.mutex.Lock()
defer h.mutex.Unlock()

View File

@ -158,7 +158,7 @@ func (r *registry) isValidCandidate(name string, baseLabels map[string]string) (
}
baseLabels[string(model.MetricNameLabel)] = name
signature = LabelsToSignature(baseLabels)
signature = model.LabelsToSignature(baseLabels)
if _, contains := r.signatureContainers[signature]; contains {
err = fmt.Errorf("metric named %s with baseLabels %s is already registered", name, baseLabels)

View File

@ -14,6 +14,8 @@ import (
"code.google.com/p/goprotobuf/proto"
dto "github.com/prometheus/client_model/go"
"github.com/prometheus/client_golang/model"
)
// An Untyped metric represents scalar values without any type implications
@ -56,7 +58,7 @@ func (metric *untyped) Set(labels map[string]string, value float64) float64 {
labels = blankLabelsSingleton
}
signature := labelValuesToSignature(labels)
signature := model.LabelValuesToSignature(labels)
metric.mutex.Lock()
defer metric.mutex.Unlock()
@ -74,7 +76,7 @@ func (metric *untyped) Set(labels map[string]string, value float64) float64 {
}
func (metric *untyped) Reset(labels map[string]string) {
signature := labelValuesToSignature(labels)
signature := model.LabelValuesToSignature(labels)
metric.mutex.Lock()
defer metric.mutex.Unlock()

View File

@ -26,7 +26,6 @@ import (
"code.google.com/p/goprotobuf/proto"
"github.com/prometheus/client_golang/model"
"github.com/prometheus/client_golang/prometheus"
)
// A stateFn is a function that represents a state in a state machine. By
@ -332,7 +331,7 @@ func (p *Parser) readingValue() stateFn {
// infamous special case of a summary, we can finally find out
// if the metric already exists.
if p.currentMF.GetType() == dto.MetricType_SUMMARY {
signature := prometheus.LabelsToSignature(p.currentLabels)
signature := model.LabelsToSignature(p.currentLabels)
if summary := p.summaries[signature]; summary != nil {
p.currentMetric = summary
} else {