forked from elastic/apm-agent-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tracer.go
1286 lines (1191 loc) · 38.5 KB
/
tracer.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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// 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"
import (
"bytes"
"compress/zlib"
"context"
"io"
"log"
"math/rand"
"sync"
"sync/atomic"
"time"
"go.elastic.co/apm/apmconfig"
"go.elastic.co/apm/internal/apmlog"
"go.elastic.co/apm/internal/configutil"
"go.elastic.co/apm/internal/iochan"
"go.elastic.co/apm/internal/ringbuffer"
"go.elastic.co/apm/internal/wildcard"
"go.elastic.co/apm/model"
"go.elastic.co/apm/stacktrace"
"go.elastic.co/apm/transport"
"go.elastic.co/fastjson"
)
const (
defaultPreContext = 3
defaultPostContext = 3
gracePeriodJitter = 0.1 // +/- 10%
tracerEventChannelCap = 1000
)
var (
// DefaultTracer is the default global Tracer, set at package
// initialization time, configured via environment variables.
//
// This will always be initialized to a non-nil value. If any
// of the environment variables are invalid, the corresponding
// errors will be logged to stderr and the default values will
// be used instead.
DefaultTracer *Tracer
)
func init() {
var opts TracerOptions
opts.initDefaults(true)
DefaultTracer = newTracer(opts)
}
// TracerOptions holds initial tracer options, for passing to NewTracerOptions.
type TracerOptions struct {
// ServiceName holds the service name.
//
// If ServiceName is empty, the service name will be defined using the
// ELASTIC_APM_SERVICE_NAME environment variable, or if that is not set,
// the executable name.
ServiceName string
// ServiceVersion holds the service version.
//
// If ServiceVersion is empty, the service version will be defined using
// the ELASTIC_APM_SERVICE_VERSION environment variable.
ServiceVersion string
// ServiceEnvironment holds the service environment.
//
// If ServiceEnvironment is empty, the service environment will be defined
// using the ELASTIC_APM_ENVIRONMENT environment variable.
ServiceEnvironment string
// Transport holds the transport to use for sending events.
//
// If Transport is nil, transport.Default will be used.
//
// If Transport implements apmconfig.Watcher, the tracer will begin watching
// for remote changes immediately. This behaviour can be disabled by setting
// the environment variable ELASTIC_APM_CENTRAL_CONFIG=false.
Transport transport.Transport
requestDuration time.Duration
metricsInterval time.Duration
maxSpans int
requestSize int
bufferSize int
metricsBufferSize int
sampler Sampler
sanitizedFieldNames wildcard.Matchers
disabledMetrics wildcard.Matchers
ignoreTransactionURLs wildcard.Matchers
captureHeaders bool
captureBody CaptureBodyMode
spanFramesMinDuration time.Duration
stackTraceLimit int
active bool
recording bool
configWatcher apmconfig.Watcher
breakdownMetrics bool
propagateLegacyHeader bool
profileSender profileSender
cpuProfileInterval time.Duration
cpuProfileDuration time.Duration
heapProfileInterval time.Duration
}
// initDefaults updates opts with default values.
func (opts *TracerOptions) initDefaults(continueOnError bool) error {
var errs []error
failed := func(err error) bool {
if err == nil {
return false
}
errs = append(errs, err)
return true
}
requestDuration, err := initialRequestDuration()
if failed(err) {
requestDuration = defaultAPIRequestTime
}
metricsInterval, err := initialMetricsInterval()
if err != nil {
metricsInterval = defaultMetricsInterval
errs = append(errs, err)
}
requestSize, err := initialAPIRequestSize()
if err != nil {
requestSize = int(defaultAPIRequestSize)
errs = append(errs, err)
}
bufferSize, err := initialAPIBufferSize()
if err != nil {
bufferSize = int(defaultAPIBufferSize)
errs = append(errs, err)
}
metricsBufferSize, err := initialMetricsBufferSize()
if err != nil {
metricsBufferSize = int(defaultMetricsBufferSize)
errs = append(errs, err)
}
maxSpans, err := initialMaxSpans()
if failed(err) {
maxSpans = defaultMaxSpans
}
sampler, err := initialSampler()
if failed(err) {
sampler = nil
}
captureHeaders, err := initialCaptureHeaders()
if failed(err) {
captureHeaders = defaultCaptureHeaders
}
captureBody, err := initialCaptureBody()
if failed(err) {
captureBody = CaptureBodyOff
}
spanFramesMinDuration, err := initialSpanFramesMinDuration()
if failed(err) {
spanFramesMinDuration = defaultSpanFramesMinDuration
}
stackTraceLimit, err := initialStackTraceLimit()
if failed(err) {
stackTraceLimit = defaultStackTraceLimit
}
active, err := initialActive()
if failed(err) {
active = true
}
recording, err := initialRecording()
if failed(err) {
recording = true
}
centralConfigEnabled, err := initialCentralConfigEnabled()
if failed(err) {
centralConfigEnabled = true
}
breakdownMetricsEnabled, err := initialBreakdownMetricsEnabled()
if failed(err) {
breakdownMetricsEnabled = true
}
propagateLegacyHeader, err := initialUseElasticTraceparentHeader()
if failed(err) {
propagateLegacyHeader = true
}
cpuProfileInterval, cpuProfileDuration, err := initialCPUProfileIntervalDuration()
if failed(err) {
cpuProfileInterval = 0
cpuProfileDuration = 0
}
heapProfileInterval, err := initialHeapProfileInterval()
if failed(err) {
heapProfileInterval = 0
}
if opts.ServiceName != "" {
err := validateServiceName(opts.ServiceName)
if failed(err) {
opts.ServiceName = ""
}
}
if len(errs) != 0 && !continueOnError {
return errs[0]
}
for _, err := range errs {
log.Printf("[apm]: %s", err)
}
opts.requestDuration = requestDuration
opts.metricsInterval = metricsInterval
opts.requestSize = requestSize
opts.bufferSize = bufferSize
opts.metricsBufferSize = metricsBufferSize
opts.maxSpans = maxSpans
opts.sampler = sampler
opts.sanitizedFieldNames = initialSanitizedFieldNames()
opts.disabledMetrics = initialDisabledMetrics()
opts.ignoreTransactionURLs = initialIgnoreTransactionURLs()
opts.breakdownMetrics = breakdownMetricsEnabled
opts.captureHeaders = captureHeaders
opts.captureBody = captureBody
opts.spanFramesMinDuration = spanFramesMinDuration
opts.stackTraceLimit = stackTraceLimit
opts.active = active
opts.recording = recording
opts.propagateLegacyHeader = propagateLegacyHeader
if opts.Transport == nil {
opts.Transport = transport.Default
}
if centralConfigEnabled {
if cw, ok := opts.Transport.(apmconfig.Watcher); ok {
opts.configWatcher = cw
}
}
if ps, ok := opts.Transport.(profileSender); ok {
opts.profileSender = ps
opts.cpuProfileInterval = cpuProfileInterval
opts.cpuProfileDuration = cpuProfileDuration
opts.heapProfileInterval = heapProfileInterval
}
serviceName, serviceVersion, serviceEnvironment := initialService()
if opts.ServiceName == "" {
opts.ServiceName = serviceName
}
if opts.ServiceVersion == "" {
opts.ServiceVersion = serviceVersion
}
if opts.ServiceEnvironment == "" {
opts.ServiceEnvironment = serviceEnvironment
}
return nil
}
// Tracer manages the sampling and sending of transactions to
// Elastic APM.
//
// Transactions are buffered until they are flushed (forcibly
// with a Flush call, or when the flush timer expires), or when
// the maximum transaction queue size is reached. Failure to
// send will be periodically retried. Once the queue limit has
// been reached, new transactions will replace older ones in
// the queue.
//
// Errors are sent as soon as possible, but will buffered and
// later sent in bulk if the tracer is busy, or otherwise cannot
// send to the server, e.g. due to network failure. There is
// a limit to the number of errors that will be buffered, and
// once that limit has been reached, new errors will be dropped
// until the queue is drained.
//
// The exported fields be altered or replaced any time up until
// any Tracer methods have been invoked.
type Tracer struct {
Transport transport.Transport
Service struct {
Name string
Version string
Environment string
}
process *model.Process
system *model.System
active int32
bufferSize int
metricsBufferSize int
closing chan struct{}
closed chan struct{}
forceFlush chan chan<- struct{}
forceSendMetrics chan chan<- struct{}
configCommands chan tracerConfigCommand
configWatcher chan apmconfig.Watcher
events chan tracerEvent
breakdownMetrics *breakdownMetrics
profileSender profileSender
statsMu sync.Mutex
stats TracerStats
// instrumentationConfig_ must only be accessed and mutated
// using Tracer.instrumentationConfig() and Tracer.setInstrumentationConfig().
instrumentationConfigInternal *instrumentationConfig
errorDataPool sync.Pool
spanDataPool sync.Pool
transactionDataPool sync.Pool
}
// NewTracer returns a new Tracer, using the default transport,
// and with the specified service name and version if specified.
// This is equivalent to calling NewTracerOptions with a
// TracerOptions having ServiceName and ServiceVersion set to
// the provided arguments.
//
// NOTE when this package is imported, DefaultTracer is initialised
// using environment variables for configuration. When creating a
// tracer with NewTracer or NewTracerOptions, you should close
// apm.DefaultTracer if it is not needed, e.g. by calling
// apm.DefaultTracer.Close() in an init function.
func NewTracer(serviceName, serviceVersion string) (*Tracer, error) {
return NewTracerOptions(TracerOptions{
ServiceName: serviceName,
ServiceVersion: serviceVersion,
})
}
// NewTracerOptions returns a new Tracer using the provided options.
// See TracerOptions for details on the options, and their default
// values.
//
// NOTE when this package is imported, DefaultTracer is initialised
// using environment variables for configuration. When creating a
// tracer with NewTracer or NewTracerOptions, you should close
// apm.DefaultTracer if it is not needed, e.g. by calling
// apm.DefaultTracer.Close() in an init function.
func NewTracerOptions(opts TracerOptions) (*Tracer, error) {
if err := opts.initDefaults(false); err != nil {
return nil, err
}
return newTracer(opts), nil
}
func newTracer(opts TracerOptions) *Tracer {
t := &Tracer{
Transport: opts.Transport,
process: ¤tProcess,
system: &localSystem,
closing: make(chan struct{}),
closed: make(chan struct{}),
forceFlush: make(chan chan<- struct{}),
forceSendMetrics: make(chan chan<- struct{}),
configCommands: make(chan tracerConfigCommand),
configWatcher: make(chan apmconfig.Watcher),
events: make(chan tracerEvent, tracerEventChannelCap),
active: 1,
breakdownMetrics: newBreakdownMetrics(),
bufferSize: opts.bufferSize,
metricsBufferSize: opts.metricsBufferSize,
profileSender: opts.profileSender,
instrumentationConfigInternal: &instrumentationConfig{
local: make(map[string]func(*instrumentationConfigValues)),
},
}
t.Service.Name = opts.ServiceName
t.Service.Version = opts.ServiceVersion
t.Service.Environment = opts.ServiceEnvironment
t.breakdownMetrics.enabled = opts.breakdownMetrics
// Initialise local transaction config.
t.setLocalInstrumentationConfig(envRecording, func(cfg *instrumentationConfigValues) {
cfg.recording = opts.recording
})
t.setLocalInstrumentationConfig(envCaptureBody, func(cfg *instrumentationConfigValues) {
cfg.captureBody = opts.captureBody
})
t.setLocalInstrumentationConfig(envCaptureHeaders, func(cfg *instrumentationConfigValues) {
cfg.captureHeaders = opts.captureHeaders
})
t.setLocalInstrumentationConfig(envMaxSpans, func(cfg *instrumentationConfigValues) {
cfg.maxSpans = opts.maxSpans
})
t.setLocalInstrumentationConfig(envTransactionSampleRate, func(cfg *instrumentationConfigValues) {
cfg.sampler = opts.sampler
cfg.extendedSampler, _ = opts.sampler.(ExtendedSampler)
})
t.setLocalInstrumentationConfig(envSpanFramesMinDuration, func(cfg *instrumentationConfigValues) {
cfg.spanFramesMinDuration = opts.spanFramesMinDuration
})
t.setLocalInstrumentationConfig(envStackTraceLimit, func(cfg *instrumentationConfigValues) {
cfg.stackTraceLimit = opts.stackTraceLimit
})
t.setLocalInstrumentationConfig(envUseElasticTraceparentHeader, func(cfg *instrumentationConfigValues) {
cfg.propagateLegacyHeader = opts.propagateLegacyHeader
})
t.setLocalInstrumentationConfig(envSanitizeFieldNames, func(cfg *instrumentationConfigValues) {
cfg.sanitizedFieldNames = opts.sanitizedFieldNames
})
t.setLocalInstrumentationConfig(envIgnoreURLs, func(cfg *instrumentationConfigValues) {
cfg.ignoreTransactionURLs = opts.ignoreTransactionURLs
})
if apmlog.DefaultLogger != nil {
defaultLogLevel := apmlog.DefaultLogger.Level()
t.setLocalInstrumentationConfig(apmlog.EnvLogLevel, func(cfg *instrumentationConfigValues) {
// Revert to the original, local, log level when
// the centrally defined log level is removed.
apmlog.DefaultLogger.SetLevel(defaultLogLevel)
})
}
if !opts.active {
t.active = 0
close(t.closed)
return t
}
go t.loop()
t.configCommands <- func(cfg *tracerConfig) {
cfg.recording = opts.recording
cfg.cpuProfileInterval = opts.cpuProfileInterval
cfg.cpuProfileDuration = opts.cpuProfileDuration
cfg.heapProfileInterval = opts.heapProfileInterval
cfg.metricsInterval = opts.metricsInterval
cfg.requestDuration = opts.requestDuration
cfg.requestSize = opts.requestSize
cfg.disabledMetrics = opts.disabledMetrics
cfg.preContext = defaultPreContext
cfg.postContext = defaultPostContext
cfg.metricsGatherers = []MetricsGatherer{newBuiltinMetricsGatherer(t)}
if apmlog.DefaultLogger != nil {
cfg.logger = apmlog.DefaultLogger
}
}
if opts.configWatcher != nil {
t.configWatcher <- opts.configWatcher
}
return t
}
// tracerConfig holds the tracer's runtime configuration, which may be modified
// by sending a tracerConfigCommand to the tracer's configCommands channel.
type tracerConfig struct {
recording bool
requestSize int
requestDuration time.Duration
metricsInterval time.Duration
logger WarningLogger
metricsGatherers []MetricsGatherer
contextSetter stacktrace.ContextSetter
preContext, postContext int
disabledMetrics wildcard.Matchers
cpuProfileDuration time.Duration
cpuProfileInterval time.Duration
heapProfileInterval time.Duration
}
type tracerConfigCommand func(*tracerConfig)
// Close closes the Tracer, preventing transactions from being
// sent to the APM server.
func (t *Tracer) Close() {
select {
case <-t.closing:
default:
close(t.closing)
}
<-t.closed
}
// Flush waits for the Tracer to flush any transactions and errors it currently
// has queued to the APM server, the tracer is stopped, or the abort channel
// is signaled.
func (t *Tracer) Flush(abort <-chan struct{}) {
flushed := make(chan struct{}, 1)
select {
case t.forceFlush <- flushed:
select {
case <-abort:
case <-flushed:
case <-t.closed:
}
case <-t.closed:
}
}
// Recording reports whether the tracer is recording events. Instrumentation
// may use this to avoid creating transactions, spans, and metrics when the
// tracer is configured to not record.
//
// Recording will also return false if the tracer is inactive.
func (t *Tracer) Recording() bool {
return t.instrumentationConfig().recording && t.Active()
}
// Active reports whether the tracer is active. If the tracer is inactive,
// no transactions or errors will be sent to the Elastic APM server.
func (t *Tracer) Active() bool {
return atomic.LoadInt32(&t.active) == 1
}
// ShouldPropagateLegacyHeader reports whether instrumentation should
// propagate the legacy "Elastic-Apm-Traceparent" header in addition to
// the standard W3C "traceparent" header.
//
// This method will be removed in a future major version when we remove
// support for propagating the legacy header.
func (t *Tracer) ShouldPropagateLegacyHeader() bool {
return t.instrumentationConfig().propagateLegacyHeader
}
// SetRequestDuration sets the maximum amount of time to keep a request open
// to the APM server for streaming data before closing the stream and starting
// a new request.
func (t *Tracer) SetRequestDuration(d time.Duration) {
t.sendConfigCommand(func(cfg *tracerConfig) {
cfg.requestDuration = d
})
}
// SetMetricsInterval sets the metrics interval -- the amount of time in
// between metrics samples being gathered.
func (t *Tracer) SetMetricsInterval(d time.Duration) {
t.sendConfigCommand(func(cfg *tracerConfig) {
cfg.metricsInterval = d
})
}
// SetContextSetter sets the stacktrace.ContextSetter to be used for
// setting stacktrace source context. If nil (which is the initial
// value), no context will be set.
func (t *Tracer) SetContextSetter(setter stacktrace.ContextSetter) {
t.sendConfigCommand(func(cfg *tracerConfig) {
cfg.contextSetter = setter
})
}
// SetLogger sets the Logger to be used for logging the operation of
// the tracer.
//
// If logger implements WarningLogger, its Warningf method will be used
// for logging warnings. Otherwise, warnings will logged using Debugf.
//
// The tracer is initialized with a default logger configured with the
// environment variables ELASTIC_APM_LOG_FILE and ELASTIC_APM_LOG_LEVEL.
// Calling SetLogger will replace the default logger.
func (t *Tracer) SetLogger(logger Logger) {
t.sendConfigCommand(func(cfg *tracerConfig) {
cfg.logger = makeWarningLogger(logger)
})
}
// SetSanitizedFieldNames sets the wildcard patterns that will be used to
// match cookie and form field names for sanitization. Fields matching any
// of the the supplied patterns will have their values redacted. If
// SetSanitizedFieldNames is called with no arguments, then no fields
// will be redacted.
//
// Configuration via Kibana takes precedence over local configuration, so
// if sanitized_field_names has been configured via Kibana, this call will
// not have any effect until/unless that configuration has been removed.
func (t *Tracer) SetSanitizedFieldNames(patterns ...string) error {
var matchers wildcard.Matchers
if len(patterns) != 0 {
matchers = make(wildcard.Matchers, len(patterns))
for i, p := range patterns {
matchers[i] = configutil.ParseWildcardPattern(p)
}
}
t.setLocalInstrumentationConfig(envSanitizeFieldNames, func(cfg *instrumentationConfigValues) {
cfg.sanitizedFieldNames = matchers
})
return nil
}
// SetIgnoreTransactionURLs sets the wildcard patterns that will be used to
// ignore transactions with matching URLs.
func (t *Tracer) SetIgnoreTransactionURLs(pattern string) error {
t.setLocalInstrumentationConfig(envIgnoreURLs, func(cfg *instrumentationConfigValues) {
cfg.ignoreTransactionURLs = configutil.ParseWildcardPatterns(pattern)
})
return nil
}
// RegisterMetricsGatherer registers g for periodic (or forced) metrics
// gathering by t.
//
// RegisterMetricsGatherer returns a function which will deregister g.
// It may safely be called multiple times.
func (t *Tracer) RegisterMetricsGatherer(g MetricsGatherer) func() {
// Wrap g in a pointer-to-struct, so we can safely compare.
wrapped := &struct{ MetricsGatherer }{MetricsGatherer: g}
t.sendConfigCommand(func(cfg *tracerConfig) {
cfg.metricsGatherers = append(cfg.metricsGatherers, wrapped)
})
deregister := func(cfg *tracerConfig) {
for i, g := range cfg.metricsGatherers {
if g != wrapped {
continue
}
cfg.metricsGatherers = append(cfg.metricsGatherers[:i], cfg.metricsGatherers[i+1:]...)
}
}
var once sync.Once
return func() {
once.Do(func() {
t.sendConfigCommand(deregister)
})
}
}
// SetConfigWatcher sets w as the config watcher.
//
// By default, the tracer will be configured to use the transport for
// watching config, if the transport implements apmconfig.Watcher. This
// can be overridden by calling SetConfigWatcher.
//
// If w is nil, config watching will be stopped.
//
// Calling SetConfigWatcher will discard any previously observed remote
// config, reverting to local config until a config change from w is
// observed.
func (t *Tracer) SetConfigWatcher(w apmconfig.Watcher) {
select {
case t.configWatcher <- w:
case <-t.closing:
case <-t.closed:
}
}
func (t *Tracer) sendConfigCommand(cmd tracerConfigCommand) {
select {
case t.configCommands <- cmd:
case <-t.closing:
case <-t.closed:
}
}
// SetRecording enables or disables recording of future events.
//
// SetRecording does not affect in-flight events.
func (t *Tracer) SetRecording(r bool) {
t.setLocalInstrumentationConfig(envRecording, func(cfg *instrumentationConfigValues) {
// Update instrumentation config to disable transactions and errors.
cfg.recording = r
})
t.sendConfigCommand(func(cfg *tracerConfig) {
// Consult t.instrumentationConfig() as local config may not be in effect,
// or there may have been a concurrent change to instrumentation config.
cfg.recording = t.instrumentationConfig().recording
})
}
// SetSampler sets the sampler the tracer.
//
// It is valid to pass nil, in which case all transactions will be sampled.
//
// Configuration via Kibana takes precedence over local configuration, so
// if sampling has been configured via Kibana, this call will not have any
// effect until/unless that configuration has been removed.
func (t *Tracer) SetSampler(s Sampler) {
t.setLocalInstrumentationConfig(envTransactionSampleRate, func(cfg *instrumentationConfigValues) {
cfg.sampler = s
cfg.extendedSampler, _ = s.(ExtendedSampler)
})
}
// SetMaxSpans sets the maximum number of spans that will be added
// to a transaction before dropping spans.
//
// Passing in zero will disable all spans, while negative values will
// permit an unlimited number of spans.
func (t *Tracer) SetMaxSpans(n int) {
t.setLocalInstrumentationConfig(envMaxSpans, func(cfg *instrumentationConfigValues) {
cfg.maxSpans = n
})
}
// SetSpanFramesMinDuration sets the minimum duration for a span after which
// we will capture its stack frames.
func (t *Tracer) SetSpanFramesMinDuration(d time.Duration) {
t.setLocalInstrumentationConfig(envMaxSpans, func(cfg *instrumentationConfigValues) {
cfg.spanFramesMinDuration = d
})
}
// SetStackTraceLimit sets the the maximum number of stack frames to collect
// for each stack trace. If limit is negative, then all frames will be collected.
func (t *Tracer) SetStackTraceLimit(limit int) {
t.setLocalInstrumentationConfig(envMaxSpans, func(cfg *instrumentationConfigValues) {
cfg.stackTraceLimit = limit
})
}
// SetCaptureHeaders enables or disables capturing of HTTP headers.
func (t *Tracer) SetCaptureHeaders(capture bool) {
t.setLocalInstrumentationConfig(envMaxSpans, func(cfg *instrumentationConfigValues) {
cfg.captureHeaders = capture
})
}
// SetCaptureBody sets the HTTP request body capture mode.
func (t *Tracer) SetCaptureBody(mode CaptureBodyMode) {
t.setLocalInstrumentationConfig(envMaxSpans, func(cfg *instrumentationConfigValues) {
cfg.captureBody = mode
})
}
// SendMetrics forces the tracer to gather and send metrics immediately,
// blocking until the metrics have been sent or the abort channel is
// signalled.
func (t *Tracer) SendMetrics(abort <-chan struct{}) {
sent := make(chan struct{}, 1)
select {
case t.forceSendMetrics <- sent:
select {
case <-abort:
case <-sent:
case <-t.closed:
}
case <-t.closed:
}
}
// Stats returns the current TracerStats. This will return the most
// recent values even after the tracer has been closed.
func (t *Tracer) Stats() TracerStats {
t.statsMu.Lock()
stats := t.stats
t.statsMu.Unlock()
return stats
}
func (t *Tracer) loop() {
ctx, cancelContext := context.WithCancel(context.Background())
defer cancelContext()
defer close(t.closed)
defer atomic.StoreInt32(&t.active, 0)
var req iochan.ReadRequest
var requestBuf bytes.Buffer
var metadata []byte
var gracePeriod time.Duration = -1
var flushed chan<- struct{}
var requestBufTransactions, requestBufSpans, requestBufErrors, requestBufMetricsets uint64
zlibWriter, _ := zlib.NewWriterLevel(&requestBuf, zlib.BestSpeed)
zlibFlushed := true
zlibClosed := false
iochanReader := iochan.NewReader()
requestBytesRead := 0
requestActive := false
closeRequest := false
flushRequest := false
requestResult := make(chan error, 1)
requestTimer := time.NewTimer(0)
requestTimerActive := false
if !requestTimer.Stop() {
<-requestTimer.C
}
// Run another goroutine to perform the blocking requests,
// communicating with the tracer loop to obtain stream data.
sendStreamRequest := make(chan time.Duration)
done := make(chan struct{})
defer func() {
close(sendStreamRequest)
<-done
}()
go func() {
defer close(done)
jitterRand := rand.New(rand.NewSource(time.Now().UnixNano()))
for gracePeriod := range sendStreamRequest {
if gracePeriod > 0 {
select {
case <-time.After(jitterDuration(gracePeriod, jitterRand, gracePeriodJitter)):
case <-ctx.Done():
}
}
requestResult <- t.Transport.SendStream(ctx, iochanReader)
}
}()
var breakdownMetricsLimitWarningLogged bool
var stats TracerStats
var metrics Metrics
var sentMetrics chan<- struct{}
var gatheringMetrics bool
var metricsTimerStart time.Time
metricsBuffer := ringbuffer.New(t.metricsBufferSize)
gatheredMetrics := make(chan struct{}, 1)
metricsTimer := time.NewTimer(0)
if !metricsTimer.Stop() {
<-metricsTimer.C
}
var lastConfigChange map[string]string
var configChanges <-chan apmconfig.Change
var stopConfigWatcher func()
defer func() {
if stopConfigWatcher != nil {
stopConfigWatcher()
}
}()
cpuProfilingState := newCPUProfilingState(t.profileSender)
heapProfilingState := newHeapProfilingState(t.profileSender)
var cfg tracerConfig
buffer := ringbuffer.New(t.bufferSize)
buffer.Evicted = func(h ringbuffer.BlockHeader) {
switch h.Tag {
case errorBlockTag:
stats.ErrorsDropped++
case spanBlockTag:
stats.SpansDropped++
case transactionBlockTag:
stats.TransactionsDropped++
}
}
modelWriter := modelWriter{
buffer: buffer,
metricsBuffer: metricsBuffer,
cfg: &cfg,
stats: &stats,
}
handleTracerConfigCommand := func(cmd tracerConfigCommand) {
var oldMetricsInterval time.Duration
if cfg.recording {
oldMetricsInterval = cfg.metricsInterval
}
cmd(&cfg)
var metricsInterval, cpuProfileInterval, cpuProfileDuration, heapProfileInterval time.Duration
if cfg.recording {
metricsInterval = cfg.metricsInterval
cpuProfileInterval = cfg.cpuProfileInterval
cpuProfileDuration = cfg.cpuProfileDuration
heapProfileInterval = cfg.heapProfileInterval
}
cpuProfilingState.updateConfig(cpuProfileInterval, cpuProfileDuration)
heapProfilingState.updateConfig(heapProfileInterval, 0)
if !gatheringMetrics && metricsInterval != oldMetricsInterval {
if metricsTimerStart.IsZero() {
if metricsInterval > 0 {
metricsTimer.Reset(metricsInterval)
metricsTimerStart = time.Now()
}
} else {
if metricsInterval <= 0 {
metricsTimerStart = time.Time{}
if !metricsTimer.Stop() {
<-metricsTimer.C
}
} else {
alreadyPassed := time.Since(metricsTimerStart)
if alreadyPassed >= metricsInterval {
metricsTimer.Reset(0)
} else {
metricsTimer.Reset(metricsInterval - alreadyPassed)
}
}
}
}
}
for {
var gatherMetrics bool
select {
case <-t.closing:
cancelContext() // informs transport that EOF is expected
iochanReader.CloseRead(io.EOF)
return
case cmd := <-t.configCommands:
handleTracerConfigCommand(cmd)
continue
case cw := <-t.configWatcher:
if configChanges != nil {
stopConfigWatcher()
t.updateRemoteConfig(cfg.logger, lastConfigChange, nil)
lastConfigChange = nil
configChanges = nil
}
if cw == nil {
continue
}
var configWatcherContext context.Context
var watchParams apmconfig.WatchParams
watchParams.Service.Name = t.Service.Name
watchParams.Service.Environment = t.Service.Environment
configWatcherContext, stopConfigWatcher = context.WithCancel(ctx)
configChanges = cw.WatchConfig(configWatcherContext, watchParams)
// Silence go vet's "possible context leak" false positive.
// We call a previous stopConfigWatcher before reassigning
// the variable, and we have a defer at the top level of the
// loop method that will call the final stopConfigWatcher
// value on method exit.
_ = stopConfigWatcher
continue
case change, ok := <-configChanges:
if !ok {
configChanges = nil
continue
}
if change.Err != nil {
if cfg.logger != nil {
cfg.logger.Errorf("config request failed: %s", change.Err)
}
} else {
t.updateRemoteConfig(cfg.logger, lastConfigChange, change.Attrs)
lastConfigChange = change.Attrs
handleTracerConfigCommand(func(cfg *tracerConfig) {
cfg.recording = t.instrumentationConfig().recording
})
}
continue
case event := <-t.events:
switch event.eventType {
case transactionEvent:
if !t.breakdownMetrics.recordTransaction(event.tx.TransactionData) {
if !breakdownMetricsLimitWarningLogged && cfg.logger != nil {
cfg.logger.Warningf("%s", breakdownMetricsLimitWarning)
breakdownMetricsLimitWarningLogged = true
}
}
modelWriter.writeTransaction(event.tx.Transaction, event.tx.TransactionData)
case spanEvent:
modelWriter.writeSpan(event.span.Span, event.span.SpanData)
case errorEvent:
modelWriter.writeError(event.err)
// Flush the buffer to transmit the error immediately.
flushRequest = true
}
case <-requestTimer.C:
requestTimerActive = false
closeRequest = true
case <-metricsTimer.C:
metricsTimerStart = time.Time{}
gatherMetrics = !gatheringMetrics
case sentMetrics = <-t.forceSendMetrics:
if cfg.recording {
if !metricsTimerStart.IsZero() {
if !metricsTimer.Stop() {
<-metricsTimer.C
}
metricsTimerStart = time.Time{}
}
gatherMetrics = !gatheringMetrics
}
case <-gatheredMetrics:
modelWriter.writeMetrics(&metrics)
gatheringMetrics = false
flushRequest = true
if cfg.recording && cfg.metricsInterval > 0 {
metricsTimerStart = time.Now()
metricsTimer.Reset(cfg.metricsInterval)
}
case <-cpuProfilingState.timer.C:
cpuProfilingState.start(ctx, cfg.logger, t.metadataReader())
case <-cpuProfilingState.finished:
cpuProfilingState.resetTimer()
case <-heapProfilingState.timer.C:
heapProfilingState.start(ctx, cfg.logger, t.metadataReader())
case <-heapProfilingState.finished:
heapProfilingState.resetTimer()
case flushed = <-t.forceFlush:
// Drain any objects buffered in the channels.
for n := len(t.events); n > 0; n-- {
event := <-t.events