Skip to content

Commit 393989b

Browse files
authored
Merge pull request #12 from InjectiveLabs/f/named-timer
feat: export combine and add pairs helpers
2 parents 0a0aa55 + 15be9f6 commit 393989b

File tree

3 files changed

+69
-10
lines changed

3 files changed

+69
-10
lines changed

metrics.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ func MergeTags(original Tags, src ...Tags) Tags {
293293
return dst
294294
}
295295

296-
func addPairs(t Tags, tags ...interface{}) Tags {
296+
func AddPairs(t Tags, tags ...interface{}) Tags {
297297
if t == nil {
298298
t = make(Tags)
299299
}
@@ -321,7 +321,7 @@ func addPairs(t Tags, tags ...interface{}) Tags {
321321
return t
322322
}
323323

324-
func combineAny(tags ...interface{}) Tags {
324+
func Combine(tags ...interface{}) Tags {
325325
tt := make(Tags)
326326
var pairs []interface{}
327327
for _, x := range tags {
@@ -334,11 +334,11 @@ func combineAny(tags ...interface{}) Tags {
334334
pairs = append(pairs, v)
335335
}
336336
}
337-
return addPairs(tt, pairs...)
337+
return AddPairs(tt, pairs...)
338338
}
339339

340340
func (t Tags) AddPairs(tags ...interface{}) Tags {
341-
return addPairs(t, tags...)
341+
return AddPairs(t, tags...)
342342
}
343343

344344
func (t Tags) With(k, v string) Tags {

metrics_test.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,65 @@ func TestIncr(t *testing.T) {
191191
})
192192
}
193193

194+
func TestAddPairs(t *testing.T) {
195+
tests := []struct {
196+
name string
197+
initial Tags
198+
input []interface{}
199+
expected Tags
200+
}{
201+
{
202+
name: "empty tags",
203+
initial: nil,
204+
input: []interface{}{},
205+
expected: Tags{},
206+
},
207+
{
208+
name: "single pair",
209+
initial: nil,
210+
input: []interface{}{"key", "value"},
211+
expected: Tags{"key": "value"},
212+
},
213+
{
214+
name: "multiple pairs",
215+
initial: nil,
216+
input: []interface{}{"key1", "value1", "key2", "value2"},
217+
expected: Tags{"key1": "value1", "key2": "value2"},
218+
},
219+
{
220+
name: "uneven tags",
221+
initial: nil,
222+
input: []interface{}{"key1", "value1", "key2"},
223+
expected: Tags{"key1": "value1"},
224+
},
225+
{
226+
name: "nil value",
227+
initial: nil,
228+
input: []interface{}{"key1", nil},
229+
expected: Tags{"key1": "nil"},
230+
},
231+
{
232+
name: "unsupported type",
233+
initial: nil,
234+
input: []interface{}{"key1", struct{}{}},
235+
expected: Tags{},
236+
},
237+
{
238+
name: "merge with existing tags",
239+
initial: Tags{"existingKey": "existingValue"},
240+
input: []interface{}{"key1", "value1"},
241+
expected: Tags{"existingKey": "existingValue", "key1": "value1"},
242+
},
243+
}
244+
245+
for _, tt := range tests {
246+
t.Run(tt.name, func(t *testing.T) {
247+
result := AddPairs(tt.initial, tt.input...)
248+
assert.Equal(t, tt.expected, result)
249+
})
250+
}
251+
}
252+
194253
func TestGauge(t *testing.T) {
195254
expectedTags := []string{"foo=bar", "baz=qux"}
196255
assertSameResults := func(t *testing.T, rec *statterRecorder) {

reports.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,17 +46,17 @@ func IndexPriceUpdatesBatchSubmitted(size int, tags ...Tags) {
4646
func Counter[T constraints.Integer](metric string, value T, tags ...interface{}) {
4747
CustomReport(func(s Statter, tagSpec []string) {
4848
s.Count(metric, int64(value), tagSpec, 1)
49-
}, combineAny(tags...))
49+
}, Combine(tags...))
5050
}
5151

5252
func CounterPositive[T constraints.Integer](metric string, value T, tags ...interface{}) {
5353
if value > 0 {
54-
Counter(metric, value, combineAny(tags...))
54+
Counter(metric, value, Combine(tags...))
5555
}
5656
}
5757

5858
func Incr(metric string, tags ...interface{}) {
59-
Counter(metric, 1, combineAny(tags...))
59+
Counter(metric, 1, Combine(tags...))
6060
}
6161

6262
func Timer(metric string, value time.Duration, tags ...Tags) {
@@ -68,9 +68,9 @@ func Timer(metric string, value time.Duration, tags ...Tags) {
6868
// Timing supports both Tags or pairs of key-value arguments.
6969
func Timing(metric string, initialTags ...interface{}) func(deferredTags ...interface{}) {
7070
start := time.Now()
71-
it := combineAny(initialTags...)
71+
it := Combine(initialTags...)
7272
return func(deferredTags ...interface{}) {
73-
dt := combineAny(deferredTags...)
73+
dt := Combine(deferredTags...)
7474
Timer(metric, time.Since(start), MergeTags(it, dt))
7575
}
7676
}
@@ -92,7 +92,7 @@ func TimingCtxWithErr(_ context.Context, metric string, initialTags ...interface
9292
func Gauge(metric string, value float64, tags ...interface{}) {
9393
CustomReport(func(s Statter, tagSpec []string) {
9494
s.Gauge(metric, value, tagSpec, 1)
95-
}, combineAny(tags...))
95+
}, Combine(tags...))
9696
}
9797

9898
func BoolTag(b bool) string {

0 commit comments

Comments
 (0)