Skip to content

Commit e192dfc

Browse files
committed
Merge pull request #60 from prometheus/beorn7/fix-const-labels
Add const labels to counter.
2 parents 80ad13d + 3798bbc commit e192dfc

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

prometheus/counter.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ func NewCounter(opts CounterOpts) Counter {
5555
nil,
5656
opts.ConstLabels,
5757
)
58-
result := &counter{value: value{desc: desc, valType: CounterValue}}
58+
result := &counter{value: value{desc: desc, valType: CounterValue, labelPairs: desc.constLabelPairs}}
5959
result.Init(result) // Init self-collection.
6060
return result
6161
}

prometheus/counter_test.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,17 @@
1313

1414
package prometheus
1515

16-
import "testing"
16+
import (
17+
"testing"
18+
19+
dto "github.com/prometheus/client_model/go"
20+
)
1721

1822
func TestCounterAdd(t *testing.T) {
1923
counter := NewCounter(CounterOpts{
20-
Name: "test",
21-
Help: "test help",
24+
Name: "test",
25+
Help: "test help",
26+
ConstLabels: Labels{"a": "1", "b": "2"},
2227
}).(*counter)
2328
counter.Inc()
2429
if expected, got := 1., counter.val; expected != got {
@@ -32,6 +37,13 @@ func TestCounterAdd(t *testing.T) {
3237
if expected, got := "counter cannot decrease in value", decreaseCounter(counter).Error(); expected != got {
3338
t.Errorf("Expected error %q, got %q.", expected, got)
3439
}
40+
41+
m := &dto.Metric{}
42+
counter.Write(m)
43+
44+
if expected, got := `label:<name:"a" value:"1" > label:<name:"b" value:"2" > counter:<value:43 > `, m.String(); expected != got {
45+
t.Errorf("expected %q, got %q", expected, got)
46+
}
3547
}
3648

3749
func decreaseCounter(c *counter) (err error) {

0 commit comments

Comments
 (0)