-
Notifications
You must be signed in to change notification settings - Fork 2
/
instrumenting.go
71 lines (60 loc) · 2.87 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package main
import (
"fmt"
"time"
"github.com/go-kit/kit/metrics"
"github.com/mre/edgecast"
)
/*
* instrumentingMiddleware wraps a given EdgecastInterface an creates metrics for its invoked functions
* The following metrics are created per function:
* - requestCount: incremented on every invocation of that function
* - requestLatency: time in seconds that function took from invocation to return
* - requestLatencyDistribution: histogram distribution of all invocations so far including phi-quantiles, total, sum
*/
type instrumentingMiddleware struct {
requestCount metrics.Counter // positive/incrementing only value
requestLatencyDistribution metrics.Histogram // bucket sampling
requestLatency metrics.Gauge // positive and negative counting value
next EdgecastInterface
}
func (mw instrumentingMiddleware) Bandwidth(platform int) (bandwidthData *edgecast.BandwidthData, err error) {
defer func(begin time.Time) {
lvs := []string{"method", "Bandwidth", "error", fmt.Sprint(err != nil)}
mw.requestCount.With(lvs...).Add(1)
mw.requestLatencyDistribution.With(lvs...).Observe(time.Since(begin).Seconds())
mw.requestLatency.With(lvs...).Set(time.Since(begin).Seconds())
}(time.Now())
bandwidthData, err = mw.next.Bandwidth(platform) // hand request to logged service
return
}
func (mw instrumentingMiddleware) Connections(platform int) (connectionData *edgecast.ConnectionData, err error) {
defer func(begin time.Time) {
lvs := []string{"method", "Connections", "error", fmt.Sprint(err != nil)}
mw.requestCount.With(lvs...).Add(1)
mw.requestLatencyDistribution.With(lvs...).Observe(time.Since(begin).Seconds())
mw.requestLatency.With(lvs...).Set(time.Since(begin).Seconds())
}(time.Now())
connectionData, err = mw.next.Connections(platform) // hand request to logged service
return
}
func (mw instrumentingMiddleware) CacheStatus(platform int) (cacheStatusData *edgecast.CacheStatusData, err error) {
defer func(begin time.Time) {
lvs := []string{"method", "CacheStatus", "error", fmt.Sprint(err != nil)}
mw.requestCount.With(lvs...).Add(1)
mw.requestLatencyDistribution.With(lvs...).Observe(time.Since(begin).Seconds())
mw.requestLatency.With(lvs...).Set(time.Since(begin).Seconds())
}(time.Now())
cacheStatusData, err = mw.next.CacheStatus(platform) // hand request to logged service
return
}
func (mw instrumentingMiddleware) StatusCodes(platform int) (statusCodeData *edgecast.StatusCodeData, err error) {
defer func(begin time.Time) {
lvs := []string{"method", "StatusCodes", "error", fmt.Sprint(err != nil)}
mw.requestCount.With(lvs...).Add(1)
mw.requestLatencyDistribution.With(lvs...).Observe(time.Since(begin).Seconds())
mw.requestLatency.With(lvs...).Set(time.Since(begin).Seconds())
}(time.Now())
statusCodeData, err = mw.next.StatusCodes(platform) // hand request to logged service
return
}