This repository has been archived by the owner on Feb 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
metrics.go
107 lines (89 loc) · 2.37 KB
/
metrics.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package executor
import (
"context"
"time"
)
// StatSource creates metrics with the given name. The returned metrics must be
// concurrency-safe.
type StatSource interface {
Timer(name string) Timer
Counter(name string) Counter
}
// Timer emits the duration of a particular event. The duration value is
// typically used to measure latencies and create histograms thereof.
type Timer func(duration time.Duration)
// Counter emits any number of events happening at a given time. For example,
// Counters are often used to measure RPS.
type Counter func(delta int)
type metrics struct {
ex Interface
statCache
}
// Metrics decorates the passed in executor and emits stats for all Actions
// executed, capturing success/failure counters as well as a latency timer for
// the each Action. If a NamedAction is passed in, per Action Type stats are
// emitted as well.
func Metrics(e Interface, src StatSource) Interface {
return &metrics{
ex: e,
statCache: newMutexCache(src),
}
}
func (m *metrics) Execute(ctx context.Context, actions ...Action) error {
wrapped := make([]Action, len(actions))
global := m.get("all_actions")
for i, a := range actions {
if na, ok := a.(NamedAction); ok {
wrapped[i] = namedStatAction{
NamedAction: na,
global: global,
stats: m.get(na.Type()),
}
} else {
wrapped[i] = statAction{
Action: a,
global: global,
}
}
}
return m.ex.Execute(ctx, wrapped...)
}
type namedStatAction struct {
NamedAction
global *statSet
stats *statSet
}
func (a namedStatAction) Execute(ctx context.Context) error {
return captureMetrics(ctx, a.NamedAction, a.global, a.stats)
}
type statAction struct {
Action
global *statSet
}
func (a statAction) Execute(ctx context.Context) error {
return captureMetrics(ctx, a.Action, a.global, nil)
}
func captureMetrics(ctx context.Context, a Action, global, stats *statSet) error {
// execute the action, timing its latency
start := time.Now()
err := a.Execute(ctx)
lat := time.Since(start)
// create our counter values for error/success
var errored, succeeded int
if err != nil {
errored = 1
} else {
succeeded = 1
}
// emit the global stats
global.Latency(lat)
global.Success(succeeded)
global.Error(errored)
// if there are name-scoped stats, emit those, too
if stats != nil {
stats.Latency(lat)
stats.Success(succeeded)
stats.Error(errored)
}
return err
}