forked from elastic/apm-agent-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
breakdown.go
320 lines (283 loc) · 8.86 KB
/
breakdown.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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
// Licensed to Elasticsearch B.V. under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. Elasticsearch B.V. licenses this file to you under
// the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package apm // import "go.elastic.co/apm/v2"
import (
"fmt"
"sync"
"sync/atomic"
"time"
"go.elastic.co/apm/v2/model"
)
const (
// breakdownMetricsLimit is the maximum number of breakdown metric
// buckets to accumulate per reporting period. Metrics are broken
// down by {transactionType, transactionName, spanType, spanSubtype}
// tuples.
breakdownMetricsLimit = 1000
// appSpanType is the special span type associated with transactions,
// for reporting transaction self-time.
appSpanType = "app"
// Breakdown metric names.
spanSelfTimeCountMetricName = "span.self_time.count"
spanSelfTimeSumMetricName = "span.self_time.sum.us"
)
var (
breakdownMetricsLimitWarning = fmt.Sprintf(`
The limit of %d breakdown metricsets has been reached, no new metricsets will be created.
Try to name your transactions so that there are less distinct transaction names.`[1:],
breakdownMetricsLimit,
)
)
// spanTimingsKey identifies a span type and subtype, for use as the key in
// spanTimingsMap.
type spanTimingsKey struct {
spanType string
spanSubtype string
}
// spanTiming records the number of times a {spanType, spanSubtype} pair
// has occurred (within the context of a transaction group), along with
// the sum of the span durations.
type spanTiming struct {
duration int64
count uint64
}
// spanTimingsMap records span timings for a transaction group.
type spanTimingsMap map[spanTimingsKey]spanTiming
// add accumulates the timing for a {spanType, spanSubtype} pair.
func (m spanTimingsMap) add(spanType, spanSubtype string, d time.Duration) {
k := spanTimingsKey{spanType: spanType, spanSubtype: spanSubtype}
timing := m[k]
timing.count++
timing.duration += int64(d)
m[k] = timing
}
// reset resets m back to its initial zero state.
func (m spanTimingsMap) reset() {
for k := range m {
delete(m, k)
}
}
// breakdownMetrics holds a pair of breakdown metrics maps. The "active" map
// accumulates new breakdown metrics, and is swapped with the "inactive" map
// just prior to when metrics gathering begins. When metrics gathering
// completes, the inactive map will be empty.
//
// breakdownMetrics may be written to concurrently by the tracer, and any
// number of other goroutines when a transaction cannot be enqueued.
type breakdownMetrics struct {
enabled bool
mu sync.RWMutex
active, inactive *breakdownMetricsMap
}
func newBreakdownMetrics() *breakdownMetrics {
return &breakdownMetrics{
active: newBreakdownMetricsMap(),
inactive: newBreakdownMetricsMap(),
}
}
type breakdownMetricsMap struct {
mu sync.RWMutex
m map[uint64][]*breakdownMetricsMapEntry
space []breakdownMetricsMapEntry
entries int
}
func newBreakdownMetricsMap() *breakdownMetricsMap {
return &breakdownMetricsMap{
m: make(map[uint64][]*breakdownMetricsMapEntry),
space: make([]breakdownMetricsMapEntry, breakdownMetricsLimit),
}
}
type breakdownMetricsMapEntry struct {
breakdownMetricsKey
breakdownTiming
}
// breakdownMetricsKey identifies a transaction group, and optionally a
// spanTimingsKey, for recording transaction and span breakdown metrics.
type breakdownMetricsKey struct {
transactionType string
transactionName string
spanTimingsKey
}
func (k breakdownMetricsKey) hash() uint64 {
h := newFnv1a()
h.add(k.transactionType)
h.add(k.transactionName)
if k.spanType != "" {
h.add(k.spanType)
}
if k.spanSubtype != "" {
h.add(k.spanSubtype)
}
return uint64(h)
}
// breakdownTiming holds breakdown metrics.
type breakdownTiming struct {
// span holds the "span.self_time" metric values.
span spanTiming
}
func (lhs *breakdownTiming) accumulate(rhs breakdownTiming) {
atomic.AddUint64(&lhs.span.count, rhs.span.count)
atomic.AddInt64(&lhs.span.duration, rhs.span.duration)
}
// recordTransaction records breakdown metrics for td into m.
//
// recordTransaction returns true if breakdown metrics were
// completely recorded, and false if any metrics were not
// recorded due to the limit being reached.
func (m *breakdownMetrics) recordTransaction(td *TransactionData) bool {
m.mu.RLock()
defer m.mu.RUnlock()
k := breakdownMetricsKey{
transactionType: td.Type,
transactionName: td.Name,
spanTimingsKey: spanTimingsKey{
spanType: appSpanType,
},
}
var transactionSpanTiming spanTiming
if td.breakdownMetricsEnabled {
endTime := td.timestamp.Add(td.Duration)
transactionSelfTime := td.Duration - td.childrenTimer.finalDuration(endTime)
transactionSpanTiming = spanTiming{count: 1, duration: int64(transactionSelfTime)}
}
if !m.active.record(k, breakdownTiming{
span: transactionSpanTiming,
}) {
// We couldn't record the transaction's metricset, so we won't
// be able to record spans for that transaction either.
return false
}
ok := true
for sk, timing := range td.spanTimings {
k.spanTimingsKey = sk
ok = ok && m.active.record(k, breakdownTiming{span: timing})
}
return ok
}
// record records a single breakdown metric, identified by k.
func (m *breakdownMetricsMap) record(k breakdownMetricsKey, bt breakdownTiming) bool {
hash := k.hash()
m.mu.RLock()
entries, ok := m.m[hash]
m.mu.RUnlock()
var offset int
if ok {
for offset = range entries {
if entries[offset].breakdownMetricsKey == k {
// The append may reallocate the entries, but the
// entries are pointers into m.activeSpace. Therefore,
// entries' timings can safely be atomically incremented
// without holding the read lock.
entries[offset].breakdownTiming.accumulate(bt)
return true
}
}
offset++ // where to start searching with the write lock below
}
m.mu.Lock()
entries, ok = m.m[hash]
if ok {
for i := range entries[offset:] {
if entries[offset+i].breakdownMetricsKey == k {
m.mu.Unlock()
entries[offset+i].breakdownTiming.accumulate(bt)
return true
}
}
} else if m.entries >= breakdownMetricsLimit {
m.mu.Unlock()
return false
}
entry := &m.space[m.entries]
*entry = breakdownMetricsMapEntry{
breakdownTiming: bt,
breakdownMetricsKey: k,
}
m.m[hash] = append(entries, entry)
m.entries++
m.mu.Unlock()
return true
}
// gather is called by builtinMetricsGatherer to gather breakdown metrics.
func (m *breakdownMetrics) gather(out *Metrics) {
// Hold m.mu only long enough to swap m.active and m.inactive.
// This will be blocked by metric updates, but that's OK; only
// metrics gathering will be delayed. After swapping we do not
// need to hold m.mu, since nothing concurrently accesses
// m.inactive while the gatherer is iterating over it.
m.mu.Lock()
m.active, m.inactive = m.inactive, m.active
m.mu.Unlock()
for hash, entries := range m.inactive.m {
for _, entry := range entries {
if entry.span.count > 0 {
out.transactionGroupMetrics = append(out.transactionGroupMetrics, &model.Metrics{
Transaction: model.MetricsTransaction{
Type: entry.transactionType,
Name: entry.transactionName,
},
Span: model.MetricsSpan{
Type: entry.spanType,
Subtype: entry.spanSubtype,
},
Samples: map[string]model.Metric{
spanSelfTimeCountMetricName: {
Value: float64(entry.span.count),
},
spanSelfTimeSumMetricName: {
Value: durationMicros(time.Duration(entry.span.duration)),
},
},
})
}
entry.breakdownMetricsKey = breakdownMetricsKey{} // release strings
}
delete(m.inactive.m, hash)
}
m.inactive.entries = 0
}
// childrenTimer tracks time spent by children of a transaction or span.
//
// childrenTimer is not goroutine-safe.
type childrenTimer struct {
// active holds the number active children.
active int
// start holds the timestamp at which active went from zero to one.
start time.Time
// totalDuration holds the total duration of time periods in which
// at least one child was active.
totalDuration time.Duration
}
func (t *childrenTimer) childStarted(start time.Time) {
t.active++
if t.active == 1 {
t.start = start
}
}
func (t *childrenTimer) childEnded(end time.Time) {
t.active--
if t.active == 0 {
t.totalDuration += end.Sub(t.start)
}
}
func (t *childrenTimer) finalDuration(end time.Time) time.Duration {
if t.active > 0 {
t.active = 0
t.totalDuration += end.Sub(t.start)
}
return t.totalDuration
}