From e5dc0421cd253b982c7bf593375aa8460c783a98 Mon Sep 17 00:00:00 2001 From: Bjoern Rabenstein Date: Fri, 25 Apr 2014 12:22:17 +0200 Subject: [PATCH] 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 --- {prometheus => model}/signature.go | 22 ++++++++++------------ {prometheus => model}/signature_test.go | 8 +++++--- prometheus/counter.go | 10 ++++++---- prometheus/gauge.go | 6 ++++-- prometheus/histogram.go | 6 ++++-- prometheus/registry.go | 2 +- prometheus/untyped.go | 6 ++++-- text/parse.go | 3 +-- 8 files changed, 35 insertions(+), 28 deletions(-) rename {prometheus => model}/signature.go (68%) rename {prometheus => model}/signature_test.go (95%) diff --git a/prometheus/signature.go b/model/signature.go similarity index 68% rename from prometheus/signature.go rename to model/signature.go index 24582ae..42b1b16 100644 --- a/prometheus/signature.go +++ b/model/signature.go @@ -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() diff --git a/prometheus/signature_test.go b/model/signature_test.go similarity index 95% rename from prometheus/signature_test.go rename to model/signature_test.go index fd5b735..6dc2e36 100644 --- a/prometheus/signature_test.go +++ b/model/signature_test.go @@ -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) } } diff --git a/prometheus/counter.go b/prometheus/counter.go index 180ad78..9cba02f 100644 --- a/prometheus/counter.go +++ b/prometheus/counter.go @@ -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() diff --git a/prometheus/gauge.go b/prometheus/gauge.go index 0a5000f..27f0359 100644 --- a/prometheus/gauge.go +++ b/prometheus/gauge.go @@ -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() diff --git a/prometheus/histogram.go b/prometheus/histogram.go index 1654e9d..317d013 100644 --- a/prometheus/histogram.go +++ b/prometheus/histogram.go @@ -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() diff --git a/prometheus/registry.go b/prometheus/registry.go index 10c6997..d6359b5 100644 --- a/prometheus/registry.go +++ b/prometheus/registry.go @@ -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) diff --git a/prometheus/untyped.go b/prometheus/untyped.go index e0a40c9..ab4f095 100644 --- a/prometheus/untyped.go +++ b/prometheus/untyped.go @@ -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() diff --git a/text/parse.go b/text/parse.go index 7c531e4..294aa0b 100644 --- a/text/parse.go +++ b/text/parse.go @@ -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 {