-
Notifications
You must be signed in to change notification settings - Fork 32
/
engine.go
259 lines (214 loc) · 7.48 KB
/
engine.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
package stats
import (
"os"
"path/filepath"
"reflect"
"sync"
"time"
)
// An Engine carries the context for producing metrics, it is configured by
// setting the exported fields or using the helper methods to create sub-engines
// that inherit the configuration of the base they were created from.
//
// The program must not modify the engine's handler, prefix, or tags after it
// started using it. If changes need to be made new engines must be created by
// calls to WithPrefix or WithTags.
type Engine struct {
// The measure handler that the engine forwards measures to.
Handler Handler
// A prefix set on all metric names produced by the engine.
Prefix string
// A list of tags set on all metrics produced by the engine.
//
// The list of tags has to be sorted. This is automatically managed by the
// helper methods WithPrefix, WithTags and the NewEngine function. A program
// that manipulates this field directly has to respect this requirement.
Tags []Tag
// This cache keeps track of the generated measure structures to avoid
// rebuilding them every time a same measure type is seen by the engine.
//
// The cached values include the engine prefix in the measure names, which
// is why the cache must be local to the engine.
cache measureCache
}
// NewEngine creates and returns a new engine configured with prefix, handler,
// and tags.
func NewEngine(prefix string, handler Handler, tags ...Tag) *Engine {
return &Engine{
Handler: handler,
Prefix: prefix,
Tags: SortTags(copyTags(tags)),
}
}
// Register adds handler to eng.
func (eng *Engine) Register(handler Handler) {
if eng.Handler == Discard {
eng.Handler = handler
} else {
eng.Handler = MultiHandler(eng.Handler, handler)
}
}
// Flush flushes eng's handler (if it implements the Flusher interface).
func (eng *Engine) Flush() {
flush(eng.Handler)
}
// WithPrefix returns a copy of the engine with prefix appended to eng's current
// prefix and tags set to the merge of eng's current tags and those passed as
// argument. Both eng and the returned engine share the same handler.
func (eng *Engine) WithPrefix(prefix string, tags ...Tag) *Engine {
return &Engine{
Handler: eng.Handler,
Prefix: eng.makeName(prefix),
Tags: eng.makeTags(tags),
}
}
// WithTags returns a copy of the engine with tags set to the merge of eng's
// current tags and those passed as arguments. Both eng and the returned engine
// share the same handler.
func (eng *Engine) WithTags(tags ...Tag) *Engine {
return eng.WithPrefix("", tags...)
}
// Incr increments by one the counter identified by name and tags.
func (eng *Engine) Incr(name string, tags ...Tag) {
eng.Add(name, 1, tags...)
}
// Add increments by value the counter identified by name and tags.
func (eng *Engine) Add(name string, value interface{}, tags ...Tag) {
eng.measure(name, value, Counter, tags...)
}
// Set sets to value the gauge identified by name and tags.
func (eng *Engine) Set(name string, value interface{}, tags ...Tag) {
eng.measure(name, value, Gauge, tags...)
}
// Observe reports value for the histogram identified by name and tags.
func (eng *Engine) Observe(name string, value interface{}, tags ...Tag) {
eng.measure(name, value, Histogram, tags...)
}
// Clock returns a new clock identified by name and tags.
func (eng *Engine) Clock(name string, tags ...Tag) *Clock {
cpy := make([]Tag, len(tags), len(tags)+1) // clock always appends a stamp.
copy(cpy, tags)
now := time.Now()
return &Clock{
name: name,
first: now,
last: now,
tags: cpy,
eng: eng,
}
}
func (eng *Engine) measure(name string, value interface{}, ftype FieldType, tags ...Tag) {
name, field := splitMeasureField(name)
mp := measureArrayPool.Get().(*[1]Measure)
m := &(*mp)[0]
m.Name = eng.makeName(name) // TODO: figure out how to optimize this
m.Fields = append(m.Fields[:0], MakeField(field, value, ftype))
m.Tags = append(m.Tags[:0], eng.Tags...)
m.Tags = append(m.Tags, tags...)
if len(tags) != 0 && !TagsAreSorted(m.Tags) {
SortTags(m.Tags)
}
eng.Handler.HandleMeasures(time.Now(), (*mp)[:]...)
for i := range m.Fields {
m.Fields[i] = Field{}
}
for i := range m.Tags {
m.Tags[i] = Tag{}
}
m.Name = ""
measureArrayPool.Put(mp)
}
func (eng *Engine) makeName(name string) string {
return concat(eng.Prefix, name)
}
func (eng *Engine) makeTags(tags []Tag) []Tag {
return SortTags(concatTags(eng.Tags, tags))
}
var measureArrayPool = sync.Pool{
New: func() interface{} { return new([1]Measure) },
}
// Report calls ReportAt with time.Now() as first argument.
func (eng *Engine) Report(metrics interface{}, tags ...Tag) {
eng.ReportAt(time.Now(), metrics, tags...)
}
// ReportAt reports a set of metrics for a given time. The metrics must be of
// type struct, pointer to struct, or a slice or array to one of those. See
// MakeMeasures for details about how to make struct types exposing metrics.
func (eng *Engine) ReportAt(time time.Time, metrics interface{}, tags ...Tag) {
var tb *tagsBuffer
if len(tags) == 0 {
// fast path for the common case where there are no dynamic tags
tags = eng.Tags
} else {
tb = tagsPool.Get().(*tagsBuffer)
tb.append(tags...)
tb.append(eng.Tags...)
tb.sort()
tags = tb.tags
}
mb := measurePool.Get().(*measuresBuffer)
mb.measures = appendMeasures(mb.measures[:0], &eng.cache, eng.Prefix, reflect.ValueOf(metrics), tags...)
ms := mb.measures
eng.Handler.HandleMeasures(time, ms...)
for i := range ms {
ms[i].reset()
}
if tb != nil {
tb.reset()
tagsPool.Put(tb)
}
measurePool.Put(mb)
}
// DefaultEngine is the engine used by global helper functions.
var DefaultEngine = NewEngine(progname(), Discard)
// Register adds handler to the default engine.
func Register(handler Handler) {
DefaultEngine.Register(handler)
}
// Flush flushes the default engine.
func Flush() {
DefaultEngine.Flush()
}
// WithPrefix returns a copy of the engine with prefix appended to default
// engine's current prefix and tags set to the merge of eng's current tags and
// those passed as argument. Both the default engine and the returned engine
// share the same handler.
func WithPrefix(prefix string, tags ...Tag) *Engine {
return DefaultEngine.WithPrefix(prefix, tags...)
}
// WithTags returns a copy of the engine with tags set to the merge of the
// default engine's current tags and those passed as arguments. Both the default
// engine and the returned engine share the same handler.
func WithTags(tags ...Tag) *Engine {
return DefaultEngine.WithTags(tags...)
}
// Incr increments by one the counter identified by name and tags.
func Incr(name string, tags ...Tag) {
DefaultEngine.Incr(name, tags...)
}
// Add increments by value the counter identified by name and tags.
func Add(name string, value interface{}, tags ...Tag) {
DefaultEngine.Add(name, value, tags...)
}
// Set sets to value the gauge identified by name and tags.
func Set(name string, value interface{}, tags ...Tag) {
DefaultEngine.Set(name, value, tags...)
}
// Observe reports value for the histogram identified by name and tags.
func Observe(name string, value interface{}, tags ...Tag) {
DefaultEngine.Observe(name, value, tags...)
}
// Report is a helper function that delegates to DefaultEngine.
func Report(metrics interface{}, tags ...Tag) {
DefaultEngine.Report(metrics, tags...)
}
// ReportAt is a helper function that delegates to DefaultEngine.
func ReportAt(time time.Time, metrics interface{}, tags ...Tag) {
DefaultEngine.ReportAt(time, metrics, tags...)
}
func progname() (name string) {
if args := os.Args; len(args) != 0 {
name = filepath.Base(args[0])
}
return
}