Skip to content

Commit 6e9c82a

Browse files
authored
Merge pull request #7 from InjectiveLabs/f/stop-fn-tags
feat(metrics): be able to send new tags on stop functions
2 parents 815d3fe + 1e09411 commit 6e9c82a

2 files changed

Lines changed: 35 additions & 15 deletions

File tree

metrics.go

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -83,14 +83,14 @@ func ReportFuncCallAndTimingCtxWithErr(ctx context.Context, tags ...Tags) func(e
8383
}
8484
}
8585

86-
func ReportFuncCallAndTimingWithErr(tags ...Tags) func(err *error) {
86+
func ReportFuncCallAndTimingWithErr(tags ...Tags) func(err *error, tags ...Tags) {
8787
fn := CallerFuncName(1)
8888
reportFunc(fn, "called", tags...)
8989
_, stop := reportTiming(context.Background(), tags...)
90-
return func(err *error) {
91-
stop()
90+
return func(err *error, stopTags ...Tags) {
91+
stop(stopTags...)
9292
if err != nil && *err != nil {
93-
ReportClosureFuncError(fn, tags...)
93+
ReportClosureFuncError(fn, MergeTags(MergeTags(nil, tags...), stopTags...))
9494
}
9595
}
9696
}
@@ -111,7 +111,7 @@ func reportFunc(fn, action string, tags ...Tags) {
111111
client.Incr(fmt.Sprintf("func.%v", action), tagArray, 0.77)
112112
}
113113

114-
type StopTimerFunc func()
114+
type StopTimerFunc func(tags ...Tags)
115115

116116
func ReportFuncTiming(tags ...Tags) StopTimerFunc {
117117
_, stopFn := reportTiming(context.Background(), tags...)
@@ -127,7 +127,7 @@ func reportTiming(ctx context.Context, tags ...Tags) (context.Context, StopTimer
127127
defer clientMux.RUnlock()
128128

129129
if client == nil {
130-
return ctx, func() {}
130+
return ctx, func(...Tags) {}
131131
}
132132
t := time.Now()
133133
fn := CallerFuncName(2)
@@ -170,13 +170,15 @@ func reportTiming(ctx context.Context, tags ...Tags) (context.Context, StopTimer
170170
}
171171
}(fn, t)
172172

173-
return spanCtx, func() {
173+
return spanCtx, func(stopTags ...Tags) {
174174
d := time.Since(t)
175175
close(doneC)
176176

177+
stopTagArray := append(tagArray, JoinTags(stopTags...)...)
178+
177179
clientMux.RLock()
178180
defer clientMux.RUnlock()
179-
client.Timing("func.timing", d, tagArray, 1)
181+
client.Timing("func.timing", d, stopTagArray, 1)
180182
if span != nil {
181183
span.End()
182184
}
@@ -187,7 +189,7 @@ func ReportClosureFuncTiming(name string, tags ...Tags) StopTimerFunc {
187189
clientMux.RLock()
188190
defer clientMux.RUnlock()
189191
if client == nil {
190-
return func() {}
192+
return func(...Tags) {}
191193
}
192194
t := time.Now()
193195
tagArray := JoinTags(tags...)
@@ -212,13 +214,14 @@ func ReportClosureFuncTiming(name string, tags ...Tags) StopTimerFunc {
212214
}
213215
}(name, t)
214216

215-
return func() {
217+
return func(stopTags ...Tags) {
216218
d := time.Since(t)
217219
close(doneC)
220+
stopTagArray := append(tagArray, JoinTags(stopTags...)...)
218221

219222
clientMux.RLock()
220223
defer clientMux.RUnlock()
221-
client.Timing("func.timing", d, tagArray, 1)
224+
client.Timing("func.timing", d, stopTagArray, 1)
222225
}
223226
}
224227

metrics_test.go

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ func Test_ReportTimedFuncWithError(t *testing.T) {
4343
t.Run("can be deferred and report the error", func(t *testing.T) {
4444
rec.reset()
4545
exec := func() (err error) {
46-
defer ReportFuncCallAndTimingWithErr(Tags{"foo": "bar"})(&err)
46+
defer ReportFuncCallAndTimingWithErr(Tags{"foo": "bar"})(&err, Tags{"stop": "error"})
4747

4848
time.Sleep(5 * time.Millisecond)
4949
return errors.New("this error should be recorder")
@@ -55,15 +55,16 @@ func Test_ReportTimedFuncWithError(t *testing.T) {
5555
expectedTags := []string{"foo=bar", "func_name=1"}
5656
assert.Equal(t, "Incr", rec.calls[0][0])
5757
assert.Equal(t, "func.called", rec.calls[0][1])
58-
assert.Equal(t, expectedTags, rec.calls[0][2])
58+
assert.ElementsMatch(t, expectedTags, rec.calls[0][2])
5959

60+
expectedTags = []string{"foo=bar", "stop=error", "func_name=1"}
6061
assert.Equal(t, "Count", rec.calls[1][0])
6162
assert.Equal(t, "func.timing", rec.calls[1][1])
62-
assert.Equal(t, expectedTags, rec.calls[1][3])
63+
assert.ElementsMatch(t, expectedTags, rec.calls[1][3])
6364

6465
assert.Equal(t, "Incr", rec.calls[2][0])
6566
assert.Equal(t, "func.error", rec.calls[2][1])
66-
assert.Equal(t, expectedTags, rec.calls[2][2])
67+
assert.ElementsMatch(t, expectedTags, rec.calls[2][2])
6768
})
6869

6970
t.Run("can be deferred and skip error reporting if nil", func(t *testing.T) {
@@ -79,6 +80,22 @@ func Test_ReportTimedFuncWithError(t *testing.T) {
7980
assert.Equal(t, "func.called", rec.calls[0][1])
8081
assert.Equal(t, "func.timing", rec.calls[1][1])
8182
})
83+
84+
t.Run("can be deferred with new tags and skip error reporting", func(t *testing.T) {
85+
rec.reset()
86+
exec := func() {
87+
var err error
88+
defer ReportFuncCallAndTimingWithErr(Tags{"foo": "bar"})(&err, Tags{"something": "new"})
89+
}
90+
91+
exec()
92+
require.Len(t, rec.calls, 2)
93+
94+
expectedTags := []string{"foo=bar", "something=new", "func_name=1"}
95+
assert.Equal(t, "func.called", rec.calls[0][1])
96+
assert.Equal(t, "func.timing", rec.calls[1][1])
97+
assert.ElementsMatch(t, expectedTags, rec.calls[1][3])
98+
})
8299
}
83100

84101
type statterRecorder struct {

0 commit comments

Comments
 (0)