Skip to content

Commit 835bf7b

Browse files
committed
take variadic args for labels
1 parent 7929d88 commit 835bf7b

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

metriks/metriks.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,12 @@ func L(name string, value string) metrics.Label {
127127
//
128128
// metriks.Inc("publisher.errors", 1)
129129
//
130-
func Inc(name string, val int64) {
131-
metrics.IncrCounter([]string{name}, float32(val))
130+
func Inc(name string, val int64, labels ...metrics.Label) {
131+
if len(labels) > 0 {
132+
metrics.IncrCounterWithLabels([]string{name}, float32(val), labels)
133+
} else {
134+
metrics.IncrCounter([]string{name}, float32(val))
135+
}
132136
}
133137

134138
// IncLabels increments a counter with additional labels

metriks/metriks_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77

88
"github.com/armon/go-metrics"
99
"github.com/armon/go-metrics/datadog"
10+
"github.com/stretchr/testify/assert"
1011
"github.com/stretchr/testify/require"
1112
"golang.org/x/net/nettest"
1213
)
@@ -69,3 +70,20 @@ func TestInMemorySink(t *testing.T) {
6970
require.Equal(t, "test.test_counter", met[0].Counters["test.test_counter"].Name)
7071
require.Equal(t, 1, met[0].Counters["test.test_counter"].Count)
7172
}
73+
74+
func TestIncWithLabels(t *testing.T) {
75+
sink, err := InitWithURL("test", "inmem://?interval=1s&retain=2s")
76+
require.NoError(t, err)
77+
require.IsType(t, &metrics.InmemSink{}, sink)
78+
79+
Inc("test_counter", 1, L("tag", "value"))
80+
met := sink.(*metrics.InmemSink).Data()
81+
require.Len(t, met, 1)
82+
83+
require.Len(t, met[0].Counters, 1)
84+
require.Contains(t, met[0].Counters, "test.test_counter;tag=value")
85+
incr := met[0].Counters["test.test_counter;tag=value"]
86+
assert.Len(t, incr.Labels, 1)
87+
assert.Equal(t, "value", incr.Labels[0].Value)
88+
assert.Equal(t, "tag", incr.Labels[0].Name)
89+
}

0 commit comments

Comments
 (0)