-
Notifications
You must be signed in to change notification settings - Fork 197
/
config.go
496 lines (454 loc) · 15.3 KB
/
config.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
// 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 (
"os"
"path/filepath"
"runtime"
"strconv"
"strings"
"sync/atomic"
"time"
"unsafe"
"github.com/pkg/errors"
"go.elastic.co/apm/internal/configutil"
"go.elastic.co/apm/internal/wildcard"
"go.elastic.co/apm/model"
)
const (
envMetricsInterval = "ELASTIC_APM_METRICS_INTERVAL"
envMaxSpans = "ELASTIC_APM_TRANSACTION_MAX_SPANS"
envTransactionSampleRate = "ELASTIC_APM_TRANSACTION_SAMPLE_RATE"
envSanitizeFieldNames = "ELASTIC_APM_SANITIZE_FIELD_NAMES"
envCaptureHeaders = "ELASTIC_APM_CAPTURE_HEADERS"
envCaptureBody = "ELASTIC_APM_CAPTURE_BODY"
envServiceName = "ELASTIC_APM_SERVICE_NAME"
envServiceVersion = "ELASTIC_APM_SERVICE_VERSION"
envEnvironment = "ELASTIC_APM_ENVIRONMENT"
envSpanFramesMinDuration = "ELASTIC_APM_SPAN_FRAMES_MIN_DURATION"
envActive = "ELASTIC_APM_ACTIVE"
envRecording = "ELASTIC_APM_RECORDING"
envAPIRequestSize = "ELASTIC_APM_API_REQUEST_SIZE"
envAPIRequestTime = "ELASTIC_APM_API_REQUEST_TIME"
envAPIBufferSize = "ELASTIC_APM_API_BUFFER_SIZE"
envMetricsBufferSize = "ELASTIC_APM_METRICS_BUFFER_SIZE"
envDisableMetrics = "ELASTIC_APM_DISABLE_METRICS"
envGlobalLabels = "ELASTIC_APM_GLOBAL_LABELS"
envStackTraceLimit = "ELASTIC_APM_STACK_TRACE_LIMIT"
envCentralConfig = "ELASTIC_APM_CENTRAL_CONFIG"
envBreakdownMetrics = "ELASTIC_APM_BREAKDOWN_METRICS"
envUseElasticTraceparentHeader = "ELASTIC_APM_USE_ELASTIC_TRACEPARENT_HEADER"
envCloudProvider = "ELASTIC_APM_CLOUD_PROVIDER"
// NOTE(axw) profiling environment variables are experimental.
// They may be removed in a future minor version without being
// considered a breaking change.
envCPUProfileInterval = "ELASTIC_APM_CPU_PROFILE_INTERVAL"
envCPUProfileDuration = "ELASTIC_APM_CPU_PROFILE_DURATION"
envHeapProfileInterval = "ELASTIC_APM_HEAP_PROFILE_INTERVAL"
defaultAPIRequestSize = 750 * configutil.KByte
defaultAPIRequestTime = 10 * time.Second
defaultAPIBufferSize = 1 * configutil.MByte
defaultMetricsBufferSize = 750 * configutil.KByte
defaultMetricsInterval = 30 * time.Second
defaultMaxSpans = 500
defaultCaptureHeaders = true
defaultCaptureBody = CaptureBodyOff
defaultSpanFramesMinDuration = 5 * time.Millisecond
defaultStackTraceLimit = 50
minAPIBufferSize = 10 * configutil.KByte
maxAPIBufferSize = 100 * configutil.MByte
minAPIRequestSize = 1 * configutil.KByte
maxAPIRequestSize = 5 * configutil.MByte
minMetricsBufferSize = 10 * configutil.KByte
maxMetricsBufferSize = 100 * configutil.MByte
)
var (
defaultSanitizedFieldNames = configutil.ParseWildcardPatterns(strings.Join([]string{
"password",
"passwd",
"pwd",
"secret",
"*key",
"*token*",
"*session*",
"*credit*",
"*card*",
"authorization",
"set-cookie",
}, ","))
globalLabels = func() model.StringMap {
var labels model.StringMap
for _, kv := range configutil.ParseListEnv(envGlobalLabels, ",", nil) {
i := strings.IndexRune(kv, '=')
if i > 0 {
k, v := strings.TrimSpace(kv[:i]), strings.TrimSpace(kv[i+1:])
labels = append(labels, model.StringMapItem{
Key: cleanLabelKey(k),
Value: truncateString(v),
})
}
}
return labels
}()
)
func initialRequestDuration() (time.Duration, error) {
return configutil.ParseDurationEnv(envAPIRequestTime, defaultAPIRequestTime)
}
func initialMetricsInterval() (time.Duration, error) {
return configutil.ParseDurationEnv(envMetricsInterval, defaultMetricsInterval)
}
func initialMetricsBufferSize() (int, error) {
size, err := configutil.ParseSizeEnv(envMetricsBufferSize, defaultMetricsBufferSize)
if err != nil {
return 0, err
}
if size < minMetricsBufferSize || size > maxMetricsBufferSize {
return 0, errors.Errorf(
"%s must be at least %s and less than %s, got %s",
envMetricsBufferSize, minMetricsBufferSize, maxMetricsBufferSize, size,
)
}
return int(size), nil
}
func initialAPIBufferSize() (int, error) {
size, err := configutil.ParseSizeEnv(envAPIBufferSize, defaultAPIBufferSize)
if err != nil {
return 0, err
}
if size < minAPIBufferSize || size > maxAPIBufferSize {
return 0, errors.Errorf(
"%s must be at least %s and less than %s, got %s",
envAPIBufferSize, minAPIBufferSize, maxAPIBufferSize, size,
)
}
return int(size), nil
}
func initialAPIRequestSize() (int, error) {
size, err := configutil.ParseSizeEnv(envAPIRequestSize, defaultAPIRequestSize)
if err != nil {
return 0, err
}
if size < minAPIRequestSize || size > maxAPIRequestSize {
return 0, errors.Errorf(
"%s must be at least %s and less than %s, got %s",
envAPIRequestSize, minAPIRequestSize, maxAPIRequestSize, size,
)
}
return int(size), nil
}
func initialMaxSpans() (int, error) {
value := os.Getenv(envMaxSpans)
if value == "" {
return defaultMaxSpans, nil
}
max, err := strconv.Atoi(value)
if err != nil {
return 0, errors.Wrapf(err, "failed to parse %s", envMaxSpans)
}
return max, nil
}
// initialSampler returns a nil Sampler if all transactions should be sampled.
func initialSampler() (Sampler, error) {
value := os.Getenv(envTransactionSampleRate)
return parseSampleRate(envTransactionSampleRate, value)
}
// parseSampleRate parses a numeric sampling rate in the range [0,1.0], returning a Sampler.
func parseSampleRate(name, value string) (Sampler, error) {
if value == "" {
value = "1"
}
ratio, err := strconv.ParseFloat(value, 64)
if err != nil {
return nil, errors.Wrapf(err, "failed to parse %s", name)
}
if ratio < 0.0 || ratio > 1.0 {
return nil, errors.Errorf(
"invalid value for %s: %s (out of range [0,1.0])",
name, value,
)
}
return NewRatioSampler(ratio), nil
}
func initialSanitizedFieldNames() wildcard.Matchers {
return configutil.ParseWildcardPatternsEnv(envSanitizeFieldNames, defaultSanitizedFieldNames)
}
func initialCaptureHeaders() (bool, error) {
return configutil.ParseBoolEnv(envCaptureHeaders, defaultCaptureHeaders)
}
func initialCaptureBody() (CaptureBodyMode, error) {
value := os.Getenv(envCaptureBody)
if value == "" {
return defaultCaptureBody, nil
}
return parseCaptureBody(envCaptureBody, value)
}
func parseCaptureBody(name, value string) (CaptureBodyMode, error) {
switch strings.TrimSpace(strings.ToLower(value)) {
case "all":
return CaptureBodyAll, nil
case "errors":
return CaptureBodyErrors, nil
case "transactions":
return CaptureBodyTransactions, nil
case "off":
return CaptureBodyOff, nil
}
return -1, errors.Errorf("invalid %s value %q", name, value)
}
func initialService() (name, version, environment string) {
name = os.Getenv(envServiceName)
version = os.Getenv(envServiceVersion)
environment = os.Getenv(envEnvironment)
if name == "" {
name = filepath.Base(os.Args[0])
if runtime.GOOS == "windows" {
name = strings.TrimSuffix(name, filepath.Ext(name))
}
}
name = sanitizeServiceName(name)
return name, version, environment
}
func initialSpanFramesMinDuration() (time.Duration, error) {
return configutil.ParseDurationEnv(envSpanFramesMinDuration, defaultSpanFramesMinDuration)
}
func initialActive() (bool, error) {
return configutil.ParseBoolEnv(envActive, true)
}
func initialRecording() (bool, error) {
return configutil.ParseBoolEnv(envRecording, true)
}
func initialDisabledMetrics() wildcard.Matchers {
return configutil.ParseWildcardPatternsEnv(envDisableMetrics, nil)
}
func initialStackTraceLimit() (int, error) {
value := os.Getenv(envStackTraceLimit)
if value == "" {
return defaultStackTraceLimit, nil
}
limit, err := strconv.Atoi(value)
if err != nil {
return 0, errors.Wrapf(err, "failed to parse %s", envStackTraceLimit)
}
return limit, nil
}
func initialCentralConfigEnabled() (bool, error) {
return configutil.ParseBoolEnv(envCentralConfig, true)
}
func initialBreakdownMetricsEnabled() (bool, error) {
return configutil.ParseBoolEnv(envBreakdownMetrics, true)
}
func initialUseElasticTraceparentHeader() (bool, error) {
return configutil.ParseBoolEnv(envUseElasticTraceparentHeader, true)
}
func initialCPUProfileIntervalDuration() (time.Duration, time.Duration, error) {
interval, err := configutil.ParseDurationEnv(envCPUProfileInterval, 0)
if err != nil || interval <= 0 {
return 0, 0, err
}
duration, err := configutil.ParseDurationEnv(envCPUProfileDuration, 0)
if err != nil || duration <= 0 {
return 0, 0, err
}
return interval, duration, nil
}
func initialHeapProfileInterval() (time.Duration, error) {
return configutil.ParseDurationEnv(envHeapProfileInterval, 0)
}
// updateRemoteConfig updates t and cfg with changes held in "attrs", and reverts to local
// config for config attributes that have been removed (exist in old but not in attrs).
//
// On return from updateRemoteConfig, unapplied config will have been removed from attrs.
func (t *Tracer) updateRemoteConfig(logger WarningLogger, old, attrs map[string]string) {
warningf := func(string, ...interface{}) {}
debugf := func(string, ...interface{}) {}
errorf := func(string, ...interface{}) {}
if logger != nil {
warningf = logger.Warningf
debugf = logger.Debugf
errorf = logger.Errorf
}
envName := func(k string) string {
return "ELASTIC_APM_" + strings.ToUpper(k)
}
var updates []func(cfg *instrumentationConfig)
for k, v := range attrs {
if oldv, ok := old[k]; ok && oldv == v {
continue
}
switch envName(k) {
case envCaptureBody:
value, err := parseCaptureBody(k, v)
if err != nil {
errorf("central config failure: %s", err)
delete(attrs, k)
continue
} else {
updates = append(updates, func(cfg *instrumentationConfig) {
cfg.captureBody = value
})
}
case envMaxSpans:
value, err := strconv.Atoi(v)
if err != nil {
errorf("central config failure: failed to parse %s: %s", k, err)
delete(attrs, k)
continue
} else {
updates = append(updates, func(cfg *instrumentationConfig) {
cfg.maxSpans = value
})
}
case envRecording:
recording, err := strconv.ParseBool(v)
if err != nil {
errorf("central config failure: failed to parse %s: %s", k, err)
delete(attrs, k)
continue
} else {
updates = append(updates, func(cfg *instrumentationConfig) {
cfg.recording = recording
})
}
case envSanitizeFieldNames:
matchers := configutil.ParseWildcardPatterns(v)
updates = append(updates, func(cfg *instrumentationConfig) {
cfg.sanitizedFieldNames = matchers
})
case envSpanFramesMinDuration:
duration, err := configutil.ParseDuration(v)
if err != nil {
errorf("central config failure: failed to parse %s: %s", k, err)
delete(attrs, k)
continue
} else {
updates = append(updates, func(cfg *instrumentationConfig) {
cfg.spanFramesMinDuration = duration
})
}
case envStackTraceLimit:
limit, err := strconv.Atoi(v)
if err != nil {
errorf("central config failure: failed to parse %s: %s", k, err)
delete(attrs, k)
continue
} else {
updates = append(updates, func(cfg *instrumentationConfig) {
cfg.stackTraceLimit = limit
})
}
case envTransactionSampleRate:
sampler, err := parseSampleRate(k, v)
if err != nil {
errorf("central config failure: %s", err)
delete(attrs, k)
continue
} else {
updates = append(updates, func(cfg *instrumentationConfig) {
cfg.sampler = sampler
cfg.extendedSampler, _ = sampler.(ExtendedSampler)
})
}
default:
warningf("central config failure: unsupported config: %s", k)
delete(attrs, k)
continue
}
debugf("central config update: updated %s to %s", k, v)
}
for k := range old {
if _, ok := attrs[k]; ok {
continue
}
updates = append(updates, func(cfg *instrumentationConfig) {
if f, ok := cfg.local[envName(k)]; ok {
f(&cfg.instrumentationConfigValues)
}
})
debugf("central config update: reverted %s to local config", k)
}
if updates != nil {
remote := make(map[string]struct{})
for k := range attrs {
remote[envName(k)] = struct{}{}
}
t.updateInstrumentationConfig(func(cfg *instrumentationConfig) {
cfg.remote = remote
for _, update := range updates {
update(cfg)
}
})
}
}
// instrumentationConfig returns the current instrumentationConfig.
//
// The returned value is immutable.
func (t *Tracer) instrumentationConfig() *instrumentationConfig {
config := atomic.LoadPointer((*unsafe.Pointer)(unsafe.Pointer(&t.instrumentationConfigInternal)))
return (*instrumentationConfig)(config)
}
// setLocalInstrumentationConfig sets local transaction configuration with
// the specified environment variable key.
func (t *Tracer) setLocalInstrumentationConfig(envKey string, f func(cfg *instrumentationConfigValues)) {
t.updateInstrumentationConfig(func(cfg *instrumentationConfig) {
cfg.local[envKey] = f
if _, ok := cfg.remote[envKey]; !ok {
f(&cfg.instrumentationConfigValues)
}
})
}
func (t *Tracer) updateInstrumentationConfig(f func(cfg *instrumentationConfig)) {
for {
oldConfig := t.instrumentationConfig()
newConfig := *oldConfig
f(&newConfig)
if atomic.CompareAndSwapPointer(
(*unsafe.Pointer)(unsafe.Pointer(&t.instrumentationConfigInternal)),
unsafe.Pointer(oldConfig),
unsafe.Pointer(&newConfig),
) {
return
}
}
}
// instrumentationConfig holds current configuration values, as well as information
// required to revert from remote to local configuration.
type instrumentationConfig struct {
instrumentationConfigValues
// local holds functions for setting instrumentationConfigValues to the most
// recently, locally specified configuration.
local map[string]func(*instrumentationConfigValues)
// remote holds the environment variable keys for applied remote config.
remote map[string]struct{}
}
// instrumentationConfigValues holds configuration that is accessible outside of the
// tracer loop, for instrumentation: StartTransaction, StartSpan, CaptureError, etc.
//
// NOTE(axw) when adding configuration here, you must also update `newTracer` to
// set the initial entry in instrumentationConfig.local, in order to properly reset
// to the local value, even if the default is the zero value.
type instrumentationConfigValues struct {
recording bool
captureBody CaptureBodyMode
captureHeaders bool
extendedSampler ExtendedSampler
maxSpans int
sampler Sampler
spanFramesMinDuration time.Duration
stackTraceLimit int
propagateLegacyHeader bool
sanitizedFieldNames wildcard.Matchers
}