From 3798bbca124fd0b0924d79db7bd10316031ebdb2 Mon Sep 17 00:00:00 2001 From: Bjoern Rabenstein Date: Wed, 28 Jan 2015 15:47:48 +0100 Subject: [PATCH] Add const labels to counter. --- prometheus/counter.go | 2 +- prometheus/counter_test.go | 18 +++++++++++++++--- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/prometheus/counter.go b/prometheus/counter.go index 6a729a3..88fb08f 100644 --- a/prometheus/counter.go +++ b/prometheus/counter.go @@ -55,7 +55,7 @@ func NewCounter(opts CounterOpts) Counter { nil, opts.ConstLabels, ) - result := &counter{value: value{desc: desc, valType: CounterValue}} + result := &counter{value: value{desc: desc, valType: CounterValue, labelPairs: desc.constLabelPairs}} result.Init(result) // Init self-collection. return result } diff --git a/prometheus/counter_test.go b/prometheus/counter_test.go index 6df4a86..99d52f1 100644 --- a/prometheus/counter_test.go +++ b/prometheus/counter_test.go @@ -13,12 +13,17 @@ package prometheus -import "testing" +import ( + "testing" + + dto "github.com/prometheus/client_model/go" +) func TestCounterAdd(t *testing.T) { counter := NewCounter(CounterOpts{ - Name: "test", - Help: "test help", + Name: "test", + Help: "test help", + ConstLabels: Labels{"a": "1", "b": "2"}, }).(*counter) counter.Inc() if expected, got := 1., counter.val; expected != got { @@ -32,6 +37,13 @@ func TestCounterAdd(t *testing.T) { if expected, got := "counter cannot decrease in value", decreaseCounter(counter).Error(); expected != got { t.Errorf("Expected error %q, got %q.", expected, got) } + + m := &dto.Metric{} + counter.Write(m) + + if expected, got := `label: label: counter: `, m.String(); expected != got { + t.Errorf("expected %q, got %q", expected, got) + } } func decreaseCounter(c *counter) (err error) {