Skip to content

Commit fedb27b

Browse files
authored
Merge pull request #131 from netlify/usability-for-metrics
take variadic args for labels
2 parents 7929d88 + 79e8c05 commit fedb27b

File tree

2 files changed

+58
-12
lines changed

2 files changed

+58
-12
lines changed

metriks/metriks.go

Lines changed: 28 additions & 12 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
@@ -138,7 +142,7 @@ func Inc(name string, val int64) {
138142
// metriks.IncLabels("publisher.errors", metriks.Labels(metriks.L("status_class", "4xx")), 1)
139143
//
140144
func IncLabels(name string, labels []metrics.Label, val int64) {
141-
metrics.IncrCounterWithLabels([]string{name}, float32(val), labels)
145+
Inc(name, val, labels...)
142146
}
143147

144148
// MeasureSince records the time from start until the invocation of the function
@@ -153,13 +157,17 @@ func IncLabels(name string, labels []metrics.Label, val int64) {
153157
// return db.Execute(query)
154158
// }
155159
//
156-
func MeasureSince(name string, start time.Time) {
157-
metrics.MeasureSince([]string{name}, start)
160+
func MeasureSince(name string, start time.Time, labels ...metrics.Label) {
161+
if len(labels) > 0 {
162+
metrics.MeasureSinceWithLabels([]string{name}, start, labels)
163+
} else {
164+
metrics.MeasureSince([]string{name}, start)
165+
}
158166
}
159167

160168
// MeasureSinceLabels is the same as MeasureSince, but with additional labels
161169
func MeasureSinceLabels(name string, labels []metrics.Label, start time.Time) {
162-
metrics.MeasureSinceWithLabels([]string{name}, start, labels)
170+
MeasureSince(name, start, labels...)
163171
}
164172

165173
// Sample records a float32 sample as part of a histogram. This will get histogram
@@ -169,25 +177,33 @@ func MeasureSinceLabels(name string, labels []metrics.Label, start time.Time) {
169177
//
170178
// metriks.Sample("publisher-payload-size", float32(len(payload)))
171179
//
172-
func Sample(name string, val float32) {
173-
metrics.AddSample([]string{name}, val)
180+
func Sample(name string, val float32, labels ...metrics.Label) {
181+
if len(labels) > 0 {
182+
metrics.AddSampleWithLabels([]string{name}, val, labels)
183+
} else {
184+
metrics.AddSample([]string{name}, val)
185+
}
174186
}
175187

176188
// SampleLabels is the same as Sample but with additional labels
177189
func SampleLabels(name string, labels []metrics.Label, val float32) {
178-
metrics.AddSampleWithLabels([]string{name}, val, labels)
190+
Sample(name, val, labels...)
179191
}
180192

181193
// Gauge is used to report a single float32 value. It is most often used during a
182194
// periodic update or timer to report the current size of a queue or how many
183195
// connections are currently connected.
184-
func Gauge(name string, val float32) {
185-
metrics.SetGauge([]string{name}, val)
196+
func Gauge(name string, val float32, labels ...metrics.Label) {
197+
if len(labels) > 0 {
198+
metrics.SetGaugeWithLabels([]string{name}, val, labels)
199+
} else {
200+
metrics.SetGauge([]string{name}, val)
201+
}
186202
}
187203

188204
// GaugeLabels is the same as Gauge but with additional labels
189205
func GaugeLabels(name string, labels []metrics.Label, val float32) {
190-
metrics.SetGaugeWithLabels([]string{name}, val, labels)
206+
Gauge(name, val, labels...)
191207
}
192208

193209
func statsdAddr(conf Config) string {

metriks/metriks_test.go

Lines changed: 30 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,32 @@ 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"), L("tag2", "value2"))
80+
met := sink.(*metrics.InmemSink).Data()
81+
require.Len(t, met, 1)
82+
83+
require.Len(t, met[0].Counters, 1)
84+
var incr metrics.SampledValue
85+
for _, v := range met[0].Counters {
86+
incr = v
87+
break
88+
}
89+
90+
assert.Len(t, incr.Labels, 2)
91+
for _, l := range incr.Labels {
92+
switch l.Name {
93+
case "tag":
94+
assert.Equal(t, "value", l.Value)
95+
case "tag2":
96+
assert.Equal(t, "value2", l.Value)
97+
default:
98+
assert.Fail(t, "unexpected label value")
99+
}
100+
}
101+
}

0 commit comments

Comments
 (0)