-
Notifications
You must be signed in to change notification settings - Fork 104
/
thresholdlogtracer.go
415 lines (365 loc) · 11.6 KB
/
thresholdlogtracer.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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
package gocb
import (
"encoding/json"
"sort"
"sync"
"sync/atomic"
"time"
)
type thresholdLogGroup struct {
name string
floor time.Duration
ops []*thresholdLogSpan
lock sync.RWMutex
}
func (g *thresholdLogGroup) init(name string, floor time.Duration, size uint32) {
g.name = name
g.floor = floor
g.ops = make([]*thresholdLogSpan, 0, size)
}
func (g *thresholdLogGroup) recordOp(span *thresholdLogSpan) {
if span.duration < g.floor {
return
}
// Preemptively check that we actually need to be inserted using a read lock first
// this is a performance improvement measure to avoid locking the mutex all the time.
g.lock.RLock()
if len(g.ops) == cap(g.ops) && span.duration < g.ops[0].duration {
// we are at capacity and we are faster than the fastest slow op
g.lock.RUnlock()
return
}
g.lock.RUnlock()
g.lock.Lock()
if len(g.ops) == cap(g.ops) && span.duration < g.ops[0].duration {
// we are at capacity and we are faster than the fastest slow op
g.lock.Unlock()
return
}
l := len(g.ops)
i := sort.Search(l, func(i int) bool { return span.duration < g.ops[i].duration })
// i represents the slot where it should be inserted
if len(g.ops) < cap(g.ops) {
if i == l {
g.ops = append(g.ops, span)
} else {
g.ops = append(g.ops, nil)
copy(g.ops[i+1:], g.ops[i:])
g.ops[i] = span
}
} else {
if i == 0 {
g.ops[i] = span
} else {
copy(g.ops[0:i-1], g.ops[1:i])
g.ops[i-1] = span
}
}
g.lock.Unlock()
}
type thresholdLogItem struct {
OperationName string `json:"operation_name,omitempty"`
TotalTimeUs uint64 `json:"total_us,omitempty"`
EncodeDurationUs uint64 `json:"encode_us,omitempty"`
DispatchDurationUs uint64 `json:"dispatch_us,omitempty"`
ServerDurationUs uint64 `json:"server_us,omitempty"`
LastRemoteAddress string `json:"last_remote_address,omitempty"`
LastLocalAddress string `json:"last_local_address,omitempty"`
LastDispatchDurationUs uint64 `json:"last_dispatch_us,omitempty"`
LastOperationID string `json:"last_operation_id,omitempty"`
LastLocalID string `json:"last_local_id,omitempty"`
DocumentKey string `json:"document_key,omitempty"`
}
type thresholdLogService struct {
Service string `json:"service"`
Count int `json:"count"`
Top []thresholdLogItem `json:"top"`
}
func (g *thresholdLogGroup) logRecordedRecords(sampleSize uint32) {
// Preallocate space to copy the ops into...
oldOps := make([]*thresholdLogSpan, sampleSize)
g.lock.Lock()
// Escape early if we have no ops to log...
if len(g.ops) == 0 {
g.lock.Unlock()
return
}
// Copy out our ops so we can cheaply print them out without blocking
// our ops from actually being recorded in other goroutines (which would
// effectively slow down the op pipeline for logging).
oldOps = oldOps[0:len(g.ops)]
copy(oldOps, g.ops)
g.ops = g.ops[:0]
g.lock.Unlock()
jsonData := thresholdLogService{
Service: g.name,
}
for i := len(oldOps) - 1; i >= 0; i-- {
op := oldOps[i]
jsonData.Top = append(jsonData.Top, thresholdLogItem{
OperationName: op.opName,
TotalTimeUs: uint64(op.duration / time.Microsecond),
DispatchDurationUs: uint64(op.totalDispatchDuration / time.Microsecond),
ServerDurationUs: uint64(op.totalServerDuration / time.Microsecond),
EncodeDurationUs: uint64(op.totalEncodeDuration / time.Microsecond),
LastRemoteAddress: op.lastDispatchPeer,
LastDispatchDurationUs: uint64(op.lastDispatchDuration / time.Microsecond),
LastOperationID: op.lastOperationID,
LastLocalID: op.lastLocalID,
DocumentKey: op.documentKey,
})
}
jsonData.Count = len(jsonData.Top)
jsonBytes, err := json.Marshal(jsonData)
if err != nil {
logDebugf("Failed to generate threshold logging service JSON: %s", err)
}
logInfof("Threshold Log: %s", jsonBytes)
}
// ThresholdLoggingOptions is the set of options available for configuring threshold logging.
type ThresholdLoggingOptions struct {
ServerDurationDisabled bool
Interval time.Duration
SampleSize uint32
KVThreshold time.Duration
ViewsThreshold time.Duration
QueryThreshold time.Duration
SearchThreshold time.Duration
AnalyticsThreshold time.Duration
ManagementThreshold time.Duration
}
// thresholdLoggingTracer is a specialized Tracer implementation which will automatically
// log operations which fall outside of a set of thresholds. Note that this tracer is
// only safe for use within the Couchbase SDK, uses by external event sources are
// likely to fail.
type thresholdLoggingTracer struct {
Interval time.Duration
SampleSize uint32
KVThreshold time.Duration
ViewsThreshold time.Duration
QueryThreshold time.Duration
SearchThreshold time.Duration
AnalyticsThreshold time.Duration
ManagementThreshold time.Duration
killCh chan struct{}
refCount int32
nextTick time.Time
kvGroup thresholdLogGroup
viewsGroup thresholdLogGroup
queryGroup thresholdLogGroup
searchGroup thresholdLogGroup
analyticsGroup thresholdLogGroup
managementGroup thresholdLogGroup
mgmtGroup thresholdLogGroup
}
func newThresholdLoggingTracer(opts *ThresholdLoggingOptions) *thresholdLoggingTracer {
if opts == nil {
opts = &ThresholdLoggingOptions{}
}
if opts.Interval == 0 {
opts.Interval = 10 * time.Second
}
if opts.SampleSize == 0 {
opts.SampleSize = 10
}
if opts.KVThreshold == 0 {
opts.KVThreshold = 500 * time.Millisecond
}
if opts.ViewsThreshold == 0 {
opts.ViewsThreshold = 1 * time.Second
}
if opts.QueryThreshold == 0 {
opts.QueryThreshold = 1 * time.Second
}
if opts.SearchThreshold == 0 {
opts.SearchThreshold = 1 * time.Second
}
if opts.AnalyticsThreshold == 0 {
opts.AnalyticsThreshold = 1 * time.Second
}
if opts.ManagementThreshold == 0 {
opts.ManagementThreshold = 1 * time.Second
}
t := &thresholdLoggingTracer{
Interval: opts.Interval,
SampleSize: opts.SampleSize,
KVThreshold: opts.KVThreshold,
ViewsThreshold: opts.ViewsThreshold,
QueryThreshold: opts.QueryThreshold,
SearchThreshold: opts.SearchThreshold,
AnalyticsThreshold: opts.AnalyticsThreshold,
ManagementThreshold: opts.ManagementThreshold,
}
t.kvGroup.init("kv", t.KVThreshold, t.SampleSize)
t.viewsGroup.init("views", t.ViewsThreshold, t.SampleSize)
t.queryGroup.init("query", t.QueryThreshold, t.SampleSize)
t.searchGroup.init("search", t.SearchThreshold, t.SampleSize)
t.analyticsGroup.init("analytics", t.AnalyticsThreshold, t.SampleSize)
t.managementGroup.init("management", t.ManagementThreshold, t.SampleSize)
if t.killCh == nil {
t.killCh = make(chan struct{})
}
if t.nextTick.IsZero() {
t.nextTick = time.Now().Add(t.Interval)
}
return t
}
// AddRef is used internally to keep track of the number of Cluster instances referring to it.
// This is used to correctly shut down the aggregation routines once there are no longer any
// instances tracing to it.
func (t *thresholdLoggingTracer) AddRef() int32 {
newRefCount := atomic.AddInt32(&t.refCount, 1)
if newRefCount == 1 {
t.startLoggerRoutine()
}
return newRefCount
}
// DecRef is the counterpart to AddRef (see AddRef for more information).
func (t *thresholdLoggingTracer) DecRef() int32 {
newRefCount := atomic.AddInt32(&t.refCount, -1)
if newRefCount == 0 {
t.killCh <- struct{}{}
}
return newRefCount
}
func (t *thresholdLoggingTracer) logRecordedRecords() {
t.kvGroup.logRecordedRecords(t.SampleSize)
t.viewsGroup.logRecordedRecords(t.SampleSize)
t.queryGroup.logRecordedRecords(t.SampleSize)
t.searchGroup.logRecordedRecords(t.SampleSize)
t.analyticsGroup.logRecordedRecords(t.SampleSize)
t.managementGroup.logRecordedRecords(t.SampleSize)
}
func (t *thresholdLoggingTracer) startLoggerRoutine() {
go t.loggerRoutine()
}
func (t *thresholdLoggingTracer) loggerRoutine() {
for {
select {
case <-time.After(t.nextTick.Sub(time.Now())):
t.nextTick = t.nextTick.Add(t.Interval)
t.logRecordedRecords()
case <-t.killCh:
t.logRecordedRecords()
return
}
}
}
func (t *thresholdLoggingTracer) recordOp(span *thresholdLogSpan) {
switch span.serviceName {
case "mgmt":
t.managementGroup.recordOp(span)
case "kv":
t.kvGroup.recordOp(span)
case "views":
t.viewsGroup.recordOp(span)
case "query":
t.queryGroup.recordOp(span)
case "search":
t.searchGroup.recordOp(span)
case "analytics":
t.analyticsGroup.recordOp(span)
}
}
// StartSpan belongs to the Tracer interface.
func (t *thresholdLoggingTracer) StartSpan(operationName string, parentContext requestSpanContext) requestSpan {
span := &thresholdLogSpan{
tracer: t,
opName: operationName,
startTime: time.Now(),
}
if context, ok := parentContext.(*thresholdLogSpanContext); ok {
span.parent = context.span
}
return span
}
type thresholdLogSpan struct {
tracer *thresholdLoggingTracer
parent *thresholdLogSpan
opName string
startTime time.Time
serviceName string
peerAddress string
serverDuration time.Duration
duration time.Duration
totalServerDuration time.Duration
totalDispatchDuration time.Duration
totalEncodeDuration time.Duration
lastDispatchPeer string
lastDispatchDuration time.Duration
lastOperationID string
lastLocalID string
documentKey string
lock sync.Mutex
}
func (n *thresholdLogSpan) Context() requestSpanContext {
return &thresholdLogSpanContext{n}
}
func (n *thresholdLogSpan) SetTag(key string, value interface{}) requestSpan {
var ok bool
switch key {
case "server_duration":
if n.serverDuration, ok = value.(time.Duration); !ok {
logDebugf("Failed to cast span server_duration tag")
}
case "couchbase.service":
if n.serviceName, ok = value.(string); !ok {
logDebugf("Failed to cast span couchbase.service tag")
}
case "peer.address":
if n.peerAddress, ok = value.(string); !ok {
logDebugf("Failed to cast span peer.address tag")
}
case "couchbase.operation_id":
if n.lastOperationID, ok = value.(string); !ok {
logDebugf("Failed to cast span couchbase.operation_id tag")
}
case "couchbase.document_key":
if n.documentKey, ok = value.(string); !ok {
logDebugf("Failed to cast span couchbase.document_key tag")
}
case "couchbase.local_id":
if n.lastLocalID, ok = value.(string); !ok {
logDebugf("Failed to cast span couchbase.local_id tag")
}
}
return n
}
func (n *thresholdLogSpan) Finish() {
n.duration = time.Now().Sub(n.startTime)
n.totalServerDuration += n.serverDuration
if n.opName == "dispatch" {
n.totalDispatchDuration += n.duration
n.lastDispatchPeer = n.peerAddress
n.lastDispatchDuration = n.duration
}
if n.opName == "encode" {
n.totalEncodeDuration += n.duration
}
if n.parent != nil {
n.parent.lock.Lock()
n.parent.totalServerDuration += n.totalServerDuration
n.parent.totalDispatchDuration += n.totalDispatchDuration
n.parent.totalEncodeDuration += n.totalEncodeDuration
if n.lastDispatchPeer != "" || n.lastDispatchDuration > 0 {
n.parent.lastDispatchPeer = n.lastDispatchPeer
n.parent.lastDispatchDuration = n.lastDispatchDuration
}
if n.lastOperationID != "" {
n.parent.lastOperationID = n.lastOperationID
}
if n.lastLocalID != "" {
n.parent.lastLocalID = n.lastLocalID
}
if n.documentKey != "" {
n.parent.documentKey = n.documentKey
}
n.parent.lock.Unlock()
}
if n.serviceName != "" {
n.tracer.recordOp(n)
}
}
type thresholdLogSpanContext struct {
span *thresholdLogSpan
}