forked from go-kit/kit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
instrumenting.go
48 lines (40 loc) · 1.14 KB
/
instrumenting.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package main
import (
"fmt"
"time"
"github.com/go-kit/kit/metrics"
)
func instrumentingMiddleware(
requestCount metrics.Counter,
requestLatency metrics.Histogram,
countResult metrics.Histogram,
) ServiceMiddleware {
return func(next StringService) StringService {
return instrmw{requestCount, requestLatency, countResult, next}
}
}
type instrmw struct {
requestCount metrics.Counter
requestLatency metrics.Histogram
countResult metrics.Histogram
StringService
}
func (mw instrmw) Uppercase(s string) (output string, err error) {
defer func(begin time.Time) {
lvs := []string{"method", "uppercase", "error", fmt.Sprint(err != nil)}
mw.requestCount.With(lvs...).Add(1)
mw.requestLatency.With(lvs...).Observe(time.Since(begin).Seconds())
}(time.Now())
output, err = mw.StringService.Uppercase(s)
return
}
func (mw instrmw) Count(s string) (n int) {
defer func(begin time.Time) {
lvs := []string{"method", "count", "error", "false"}
mw.requestCount.With(lvs...).Add(1)
mw.requestLatency.With(lvs...).Observe(time.Since(begin).Seconds())
mw.countResult.Observe(float64(n))
}(time.Now())
n = mw.StringService.Count(s)
return
}