Merge pull request #60 from prometheus/beorn7/fix-const-labels

Add const labels to counter.
This commit is contained in:
Björn Rabenstein 2015-01-28 15:59:29 +01:00
commit e192dfc497
2 changed files with 16 additions and 4 deletions

View File

@ -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
}

View File

@ -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",
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:<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) {