Add const labels to counter.

This commit is contained in:
Bjoern Rabenstein 2015-01-28 15:47:48 +01:00
parent 80ad13d35e
commit 3798bbca12
2 changed files with 16 additions and 4 deletions

View File

@ -55,7 +55,7 @@ func NewCounter(opts CounterOpts) Counter {
nil, nil,
opts.ConstLabels, 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. result.Init(result) // Init self-collection.
return result return result
} }

View File

@ -13,12 +13,17 @@
package prometheus package prometheus
import "testing" import (
"testing"
dto "github.com/prometheus/client_model/go"
)
func TestCounterAdd(t *testing.T) { func TestCounterAdd(t *testing.T) {
counter := NewCounter(CounterOpts{ counter := NewCounter(CounterOpts{
Name: "test", Name: "test",
Help: "test help", Help: "test help",
ConstLabels: Labels{"a": "1", "b": "2"},
}).(*counter) }).(*counter)
counter.Inc() counter.Inc()
if expected, got := 1., counter.val; expected != got { 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 { if expected, got := "counter cannot decrease in value", decreaseCounter(counter).Error(); expected != got {
t.Errorf("Expected error %q, got %q.", expected, got) t.Errorf("Expected error %q, got %q.", expected, got)
} }
m := &dto.Metric{}
counter.Write(m)
if expected, got := `label:<name:"a" value:"1" > label:<name:"b" value:"2" > counter:<value:43 > `, m.String(); expected != got {
t.Errorf("expected %q, got %q", expected, got)
}
} }
func decreaseCounter(c *counter) (err error) { func decreaseCounter(c *counter) (err error) {