forked from newrelic/go-agent
/
metrics.go
264 lines (223 loc) · 6.34 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
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
260
261
262
263
264
// Copyright 2020 New Relic Corporation. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
package internal
import (
"bytes"
"time"
"github.com/newrelic/go-agent/internal/jsonx"
)
type metricForce int
const (
forced metricForce = iota
unforced
)
type metricID struct {
Name string `json:"name"`
Scope string `json:"scope,omitempty"`
}
type metricData struct {
// These values are in the units expected by the collector.
countSatisfied float64 // Seconds, or count for Apdex
totalTolerated float64 // Seconds, or count for Apdex
exclusiveFailed float64 // Seconds, or count for Apdex
min float64 // Seconds
max float64 // Seconds
sumSquares float64 // Seconds**2, or 0 for Apdex
}
func metricDataFromDuration(duration, exclusive time.Duration) metricData {
ds := duration.Seconds()
return metricData{
countSatisfied: 1,
totalTolerated: ds,
exclusiveFailed: exclusive.Seconds(),
min: ds,
max: ds,
sumSquares: ds * ds,
}
}
type metric struct {
forced metricForce
data metricData
}
type metricTable struct {
metricPeriodStart time.Time
failedHarvests int
maxTableSize int // After this max is reached, only forced metrics are added
metrics map[metricID]*metric
}
func newMetricTable(maxTableSize int, now time.Time) *metricTable {
return &metricTable{
metricPeriodStart: now,
metrics: make(map[metricID]*metric),
maxTableSize: maxTableSize,
failedHarvests: 0,
}
}
func (mt *metricTable) full() bool {
return len(mt.metrics) >= mt.maxTableSize
}
func (data *metricData) aggregate(src metricData) {
data.countSatisfied += src.countSatisfied
data.totalTolerated += src.totalTolerated
data.exclusiveFailed += src.exclusiveFailed
if src.min < data.min {
data.min = src.min
}
if src.max > data.max {
data.max = src.max
}
data.sumSquares += src.sumSquares
}
func (mt *metricTable) mergeMetric(id metricID, m metric) {
if to := mt.metrics[id]; nil != to {
to.data.aggregate(m.data)
return
}
if mt.full() && (unforced == m.forced) {
mt.addSingleCount(supportabilityDropped, forced)
return
}
// NOTE: `new` is used in place of `&m` since the latter will make `m`
// get heap allocated regardless of whether or not this line gets
// reached (running go version go1.5 darwin/amd64). See
// BenchmarkAddingSameMetrics.
alloc := new(metric)
*alloc = m
mt.metrics[id] = alloc
}
func (mt *metricTable) mergeFailed(from *metricTable) {
fails := from.failedHarvests + 1
if fails >= failedMetricAttemptsLimit {
return
}
if from.metricPeriodStart.Before(mt.metricPeriodStart) {
mt.metricPeriodStart = from.metricPeriodStart
}
mt.failedHarvests = fails
mt.merge(from, "")
}
func (mt *metricTable) merge(from *metricTable, newScope string) {
if "" == newScope {
for id, m := range from.metrics {
mt.mergeMetric(id, *m)
}
} else {
for id, m := range from.metrics {
mt.mergeMetric(metricID{Name: id.Name, Scope: newScope}, *m)
}
}
}
func (mt *metricTable) add(name, scope string, data metricData, force metricForce) {
mt.mergeMetric(metricID{Name: name, Scope: scope}, metric{data: data, forced: force})
}
func (mt *metricTable) addCount(name string, count float64, force metricForce) {
mt.add(name, "", metricData{countSatisfied: count}, force)
}
func (mt *metricTable) addSingleCount(name string, force metricForce) {
mt.addCount(name, float64(1), force)
}
func (mt *metricTable) addDuration(name, scope string, duration, exclusive time.Duration, force metricForce) {
mt.add(name, scope, metricDataFromDuration(duration, exclusive), force)
}
func (mt *metricTable) addValueExclusive(name, scope string, total, exclusive float64, force metricForce) {
data := metricData{
countSatisfied: 1,
totalTolerated: total,
exclusiveFailed: exclusive,
min: total,
max: total,
sumSquares: total * total,
}
mt.add(name, scope, data, force)
}
func (mt *metricTable) addValue(name, scope string, total float64, force metricForce) {
mt.addValueExclusive(name, scope, total, total, force)
}
func (mt *metricTable) addApdex(name, scope string, apdexThreshold time.Duration, zone ApdexZone, force metricForce) {
apdexSeconds := apdexThreshold.Seconds()
data := metricData{min: apdexSeconds, max: apdexSeconds}
switch zone {
case ApdexSatisfying:
data.countSatisfied = 1
case ApdexTolerating:
data.totalTolerated = 1
case ApdexFailing:
data.exclusiveFailed = 1
}
mt.add(name, scope, data, force)
}
func (mt *metricTable) CollectorJSON(agentRunID string, now time.Time) ([]byte, error) {
if 0 == len(mt.metrics) {
return nil, nil
}
estimatedBytesPerMetric := 128
estimatedLen := len(mt.metrics) * estimatedBytesPerMetric
buf := bytes.NewBuffer(make([]byte, 0, estimatedLen))
buf.WriteByte('[')
jsonx.AppendString(buf, agentRunID)
buf.WriteByte(',')
jsonx.AppendInt(buf, mt.metricPeriodStart.Unix())
buf.WriteByte(',')
jsonx.AppendInt(buf, now.Unix())
buf.WriteByte(',')
buf.WriteByte('[')
first := true
for id, metric := range mt.metrics {
if first {
first = false
} else {
buf.WriteByte(',')
}
buf.WriteByte('[')
buf.WriteByte('{')
buf.WriteString(`"name":`)
jsonx.AppendString(buf, id.Name)
if id.Scope != "" {
buf.WriteString(`,"scope":`)
jsonx.AppendString(buf, id.Scope)
}
buf.WriteByte('}')
buf.WriteByte(',')
jsonx.AppendFloatArray(buf,
metric.data.countSatisfied,
metric.data.totalTolerated,
metric.data.exclusiveFailed,
metric.data.min,
metric.data.max,
metric.data.sumSquares)
buf.WriteByte(']')
}
buf.WriteByte(']')
buf.WriteByte(']')
return buf.Bytes(), nil
}
func (mt *metricTable) Data(agentRunID string, harvestStart time.Time) ([]byte, error) {
return mt.CollectorJSON(agentRunID, harvestStart)
}
func (mt *metricTable) MergeIntoHarvest(h *Harvest) {
h.Metrics.mergeFailed(mt)
}
func (mt *metricTable) ApplyRules(rules metricRules) *metricTable {
if nil == rules {
return mt
}
if len(rules) == 0 {
return mt
}
applied := newMetricTable(mt.maxTableSize, mt.metricPeriodStart)
cache := make(map[string]string)
for id, m := range mt.metrics {
out, ok := cache[id.Name]
if !ok {
out = rules.Apply(id.Name)
cache[id.Name] = out
}
if "" != out {
applied.mergeMetric(metricID{Name: out, Scope: id.Scope}, *m)
}
}
return applied
}
func (mt *metricTable) EndpointMethod() string {
return cmdMetrics
}