-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathoptions.ts
More file actions
518 lines (452 loc) · 15.2 KB
/
options.ts
File metadata and controls
518 lines (452 loc) · 15.2 KB
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
/*
* Copyright Splunk Inc.
*
* Licensed 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.
*/
import * as util from 'util';
import {
AlwaysOnSampler,
ConsoleSpanExporter,
SimpleSpanProcessor,
SpanExporter,
SpanProcessor,
} from '@opentelemetry/sdk-trace-base';
import { createRuleBasedSampler } from './RuleBasedSampler';
import { B3Propagator, B3InjectEncoding } from '@opentelemetry/propagator-b3';
import { AWSXRayPropagator } from '@opentelemetry/propagator-aws-xray';
import { getInstrumentations } from '../instrumentations';
import { OTLPTraceExporter as OTLPHttpTraceExporter } from '@opentelemetry/exporter-trace-otlp-proto';
import type * as OtlpGrpc from '@opentelemetry/exporter-trace-otlp-grpc';
import type * as grpc from '@grpc/grpc-js';
import { getDetectedResource } from '../resource';
import {
defaultServiceName,
getEnvArray,
getEnvValueByPrecedence,
getNonEmptyEnvVar,
ensureResourcePath,
readFileContent,
} from '../utils';
import { ATTR_SERVICE_NAME } from '@opentelemetry/semantic-conventions';
import { diag, TextMapPropagator } from '@opentelemetry/api';
import {
CompositePropagator,
W3CBaggagePropagator,
W3CTraceContextPropagator,
} from '@opentelemetry/core';
import { SplunkBatchSpanProcessor } from './SplunkBatchSpanProcessor';
import { Resource, resourceFromAttributes } from '@opentelemetry/resources';
import { NextJsSpanProcessor } from './NextJsSpanProcessor';
import type {
SpanExporterFactory,
StartTracingOptions,
TracingOptions,
} from './types';
import { NodeTracerConfig } from '@opentelemetry/sdk-trace-node';
import {
configGetPropagators,
configGetResource,
configGetSampler,
configGetUriParameterCapture,
getConfigBoolean,
getConfigNumber,
getConfigTracerProvider,
getNonEmptyConfigVar,
} from '../configuration';
import type { SpanExporter as ConfigSpanExporter } from '../configuration/schema';
import { toCompression } from '../configuration/convert';
import {
DEFAULT_ATTRIBUTE_VALUE_LENGTH_LIMIT,
DEFAULT_COUNT_LIMIT,
DEFAULT_SPAN_LINK_COUNT_LIMIT,
} from '../defaults';
function createSampler(userConfig: NodeTracerConfig) {
if (userConfig.sampler !== undefined) {
return userConfig.sampler;
}
const configSampler = configGetSampler();
if (configSampler === undefined) {
const envSampler = getNonEmptyEnvVar('OTEL_TRACES_SAMPLER');
if (envSampler === undefined) {
return new AlwaysOnSampler();
}
if (envSampler === 'rules') {
return createRuleBasedSampler(
getNonEmptyEnvVar('OTEL_TRACES_SAMPLER_ARG')
);
}
}
return configSampler;
}
export function _setDefaultOptions(
options: StartTracingOptions = {}
): TracingOptions {
const accessToken =
options.accessToken || getNonEmptyConfigVar('SPLUNK_ACCESS_TOKEN') || '';
const realm = options.realm || getNonEmptyConfigVar('SPLUNK_REALM');
if (realm) {
if (!accessToken) {
throw new Error(
'Splunk realm is set, but access token is unset. To send traces to the Observability Cloud, both need to be set'
);
}
}
const envResource = getDetectedResource();
const resourceFactory = options.resourceFactory || ((r: Resource) => r);
let resource = resourceFactory(
resourceFromAttributes(envResource.attributes || {}).merge(
configGetResource()
)
);
const serviceName =
options.serviceName ||
getNonEmptyConfigVar('OTEL_SERVICE_NAME') ||
resource.attributes[ATTR_SERVICE_NAME];
resource = resource.merge(
resourceFromAttributes({
[ATTR_SERVICE_NAME]: serviceName || defaultServiceName(),
})
);
const extraTracerConfig = options.tracerConfig || {};
const sampler = createSampler(extraTracerConfig);
const tracerConfig: NodeTracerConfig = {
resource,
sampler,
generalLimits: {
attributeCountLimit: getConfigNumber(
'OTEL_ATTRIBUTE_COUNT_LIMIT',
DEFAULT_COUNT_LIMIT
),
attributeValueLengthLimit: getConfigNumber(
'OTEL_ATTRIBUTE_VALUE_LENGTH_LIMIT',
DEFAULT_ATTRIBUTE_VALUE_LENGTH_LIMIT
),
},
spanLimits: {
attributeValueLengthLimit: getConfigNumber(
'OTEL_SPAN_ATTRIBUTE_VALUE_LENGTH_LIMIT',
DEFAULT_ATTRIBUTE_VALUE_LENGTH_LIMIT
),
attributeCountLimit: getConfigNumber(
'OTEL_SPAN_ATTRIBUTE_COUNT_LIMIT',
DEFAULT_COUNT_LIMIT
),
linkCountLimit: getConfigNumber(
'OTEL_SPAN_LINK_COUNT_LIMIT',
DEFAULT_SPAN_LINK_COUNT_LIMIT
),
eventCountLimit: getConfigNumber(
'OTEL_SPAN_EVENT_COUNT_LIMIT',
DEFAULT_COUNT_LIMIT
),
attributePerEventCountLimit: getConfigNumber(
'OTEL_EVENT_ATTRIBUTE_COUNT_LIMIT',
DEFAULT_COUNT_LIMIT
),
attributePerLinkCountLimit: getConfigNumber(
'OTEL_LINK_ATTRIBUTE_COUNT_LIMIT',
DEFAULT_COUNT_LIMIT
),
},
...extraTracerConfig,
};
const instrumentations = options.instrumentations || getInstrumentations();
if (instrumentations.length === 0) {
diag.warn(
'No instrumentations set to be loaded. Install an instrumentation package to enable auto-instrumentation.'
);
}
return {
realm,
endpoint: options.endpoint,
serviceName: String(resource.attributes[ATTR_SERVICE_NAME]),
accessToken,
serverTimingEnabled:
options.serverTimingEnabled ||
getConfigBoolean('SPLUNK_TRACE_RESPONSE_HEADER_ENABLED', true),
captureHttpRequestUriParams:
options.captureHttpRequestUriParams || configGetUriParameterCapture(),
instrumentations,
tracerConfig,
databaseTraceContextPropagationEnabled:
options.databaseTraceContextPropagationEnabled ||
getConfigBoolean('SPLUNK_DB_TRACE_CONTEXT_PROPAGATION_ENABLED', false),
spanExporterFactory:
options.spanExporterFactory || defaultSpanExporterFactory(realm),
spanProcessorFactory:
options.spanProcessorFactory || defaultSpanProcessorFactory,
propagatorFactory: options.propagatorFactory || defaultPropagatorFactory,
};
}
const SUPPORTED_EXPORTER_TYPES = ['console', 'otlp', 'none'];
export type ExporterType = (typeof SUPPORTED_EXPORTER_TYPES)[number];
const SpanExporterMap: Record<ExporterType, SpanExporterFactory> = {
console: consoleSpanExporterFactory,
otlp: otlpSpanExporterFactory,
none: () => [],
};
function containsSupportedRealmExporter(exporterTypes: string[]) {
return exporterTypes.includes('otlp');
}
function areValidExporterTypes(types: string[]): boolean {
for (const t of types) {
if (!SUPPORTED_EXPORTER_TYPES.includes(t)) {
return false;
}
}
return true;
}
function getExporterTypes(realm: string | undefined): ExporterType[] {
const traceExporters: string[] = getEnvArray('OTEL_TRACES_EXPORTER') || [
'otlp',
];
if (realm) {
if (!containsSupportedRealmExporter(traceExporters)) {
throw new Error(
'Setting the Splunk realm with an explicit OTEL_TRACES_EXPORTER requires OTEL_TRACES_EXPORTER to be either otlp or be left undefined'
);
}
}
if (!areValidExporterTypes(traceExporters)) {
throw new Error(
`Invalid value for OTEL_TRACES_EXPORTER env variable: ${util.inspect(
process.env.OTEL_TRACES_EXPORTER
)}. Choose from ${util.inspect(SUPPORTED_EXPORTER_TYPES, {
compact: true,
})} or leave undefined.`
);
}
return traceExporters;
}
export function defaultSpanExporterFactory(
realm: string | undefined
): SpanExporterFactory {
const configTracerProvider = getConfigTracerProvider();
if (configTracerProvider === undefined) {
const exporterTypes = getExporterTypes(realm);
return (options) => {
const factories = exporterTypes.map((t) => SpanExporterMap[t]);
return factories.flatMap((factory) => factory(options));
};
}
return () => [];
}
export function otlpSpanExporterFactory(options: TracingOptions): SpanExporter {
let protocol = getEnvValueByPrecedence([
'OTEL_EXPORTER_OTLP_TRACES_PROTOCOL',
'OTEL_EXPORTER_OTLP_PROTOCOL',
]);
let endpoint = options.endpoint;
const accessToken = options.accessToken;
if (options.realm !== undefined) {
if (protocol !== undefined && protocol !== 'http/protobuf') {
diag.warn(
`OTLP span exporter factory: defaulting protocol to 'http/protobuf' instead of ${protocol} due to realm being defined.`
);
}
const envEndpoint = getEnvValueByPrecedence([
'OTEL_EXPORTER_OTLP_TRACES_ENDPOINT',
'OTEL_EXPORTER_OTLP_ENDPOINT',
]);
if (endpoint === undefined && envEndpoint === undefined) {
endpoint = `https://ingest.${options.realm}.signalfx.com/v2/trace/otlp`;
protocol = 'http/protobuf';
} else {
diag.warn(
'OTLP span exporter factory: Realm value ignored (full endpoint URL has been specified).'
);
}
}
protocol = protocol ?? 'http/protobuf';
switch (protocol) {
case 'grpc': {
const grpcModule: typeof grpc = require('@grpc/grpc-js');
const otlpGrpc: typeof OtlpGrpc = require('@opentelemetry/exporter-trace-otlp-grpc');
const metadata = new grpcModule.Metadata();
if (accessToken) {
// for forward compatibility, is not currently supported
metadata.set('X-SF-TOKEN', accessToken);
}
return new otlpGrpc.OTLPTraceExporter({
url: endpoint,
metadata,
});
}
case 'http/protobuf': {
const headers = accessToken
? {
'X-SF-TOKEN': accessToken,
}
: undefined;
const url = ensureResourcePath(endpoint, '/v1/traces');
return new OTLPHttpTraceExporter({
url,
headers,
});
}
default:
throw new Error(
`Expected OTLP protocol to be either grpc or http/protobuf, got ${protocol}.`
);
}
}
export function consoleSpanExporterFactory(): SpanExporter {
return new ConsoleSpanExporter();
}
function toSpanExporter(
configExporter: ConfigSpanExporter
): SpanExporter | undefined {
if (configExporter.otlp_http !== undefined) {
const otlpHttp = configExporter.otlp_http;
const configHeaders = otlpHttp.headers || [];
const headers: Record<string, string> = {};
for (const header of configHeaders) {
if (header.value !== null) {
headers[header.name] = header.value;
}
}
const url = ensureResourcePath(otlpHttp.endpoint, '/v1/traces');
return new OTLPHttpTraceExporter({
url,
headers,
timeoutMillis: otlpHttp.timeout ?? undefined,
compression: toCompression(otlpHttp.compression),
httpAgentOptions: {
ca: readFileContent(otlpHttp.tls?.ca_file),
cert: readFileContent(otlpHttp.tls?.cert_file),
key: readFileContent(otlpHttp.tls?.key_file),
},
});
} else if (configExporter.otlp_grpc !== undefined) {
const cfgGrpc = configExporter.otlp_grpc;
const GrpcModule: typeof grpc = require('@grpc/grpc-js');
const otlpGrpc: typeof OtlpGrpc = require('@opentelemetry/exporter-trace-otlp-grpc');
const metadata = new GrpcModule.Metadata();
for (const header of cfgGrpc.headers || []) {
if (header.value !== null) {
metadata.set(header.name, header.value);
}
}
const credentials =
cfgGrpc.tls === undefined
? undefined
: GrpcModule.credentials.createSsl(
readFileContent(cfgGrpc.tls?.cert_file),
readFileContent(cfgGrpc.tls?.key_file),
readFileContent(cfgGrpc.tls?.ca_file)
);
return new otlpGrpc.OTLPTraceExporter({
url: cfgGrpc.endpoint ?? undefined,
metadata,
timeoutMillis: cfgGrpc.timeout ?? undefined,
compression: toCompression(cfgGrpc.compression),
credentials,
});
} else if (configExporter.console !== undefined) {
return new ConsoleSpanExporter();
} else if (configExporter['otlp_file/development'] !== undefined) {
diag.warn('span exporter "otlp_file/development" is not supported');
} else if (configExporter.zipkin !== undefined) {
diag.warn('span exporter "zipkin" is not supported');
}
return undefined;
}
export function defaultSpanProcessorFactory(
options: TracingOptions
): SpanProcessor[] {
const configTracerProvider = getConfigTracerProvider();
const nextJsFixEnabled = getConfigBoolean('SPLUNK_NEXTJS_FIX_ENABLED', false);
if (configTracerProvider === undefined) {
let exporters: SpanExporter | SpanExporter[] = [];
const spanExporters = options.spanExporterFactory(options);
if (!Array.isArray(spanExporters)) {
exporters = [spanExporters];
} else {
exporters = spanExporters;
}
const processors: SpanProcessor[] = [];
if (nextJsFixEnabled) {
processors.push(new NextJsSpanProcessor());
}
for (const exporter of exporters) {
processors.push(new SplunkBatchSpanProcessor(exporter));
}
return processors;
}
const processors: SpanProcessor[] = [];
for (const processor of configTracerProvider.processors) {
if (processor.batch !== undefined) {
const batch = processor.batch;
const configExporter = batch.exporter;
const exporter = toSpanExporter(configExporter);
if (exporter !== undefined) {
processors.push(
new SplunkBatchSpanProcessor(exporter, {
maxExportBatchSize: batch.max_export_batch_size ?? undefined,
scheduledDelayMillis: batch.schedule_delay ?? undefined,
exportTimeoutMillis: batch.export_timeout ?? undefined,
maxQueueSize: batch.max_queue_size ?? undefined,
})
);
}
} else if (processor.simple !== undefined) {
const exporter = toSpanExporter(processor.simple?.exporter);
if (exporter !== undefined) {
processors.push(new SimpleSpanProcessor(exporter));
}
}
}
return processors;
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
export function defaultPropagatorFactory(
_options: TracingOptions
): TextMapPropagator {
let propagatorKeys: string[] = [];
const configPropagators = configGetPropagators();
if (configPropagators === undefined) {
propagatorKeys = getEnvArray('OTEL_PROPAGATORS') || [
'tracecontext',
'baggage',
];
} else {
propagatorKeys = configPropagators;
}
const propagators = [];
for (const propagator of propagatorKeys) {
switch (propagator) {
case 'baggage':
propagators.push(new W3CBaggagePropagator());
break;
case 'tracecontext':
propagators.push(new W3CTraceContextPropagator());
break;
case 'b3multi':
propagators.push(
new B3Propagator({ injectEncoding: B3InjectEncoding.MULTI_HEADER })
);
break;
case 'b3':
propagators.push(new B3Propagator());
break;
case 'xray':
propagators.push(new AWSXRayPropagator());
break;
default:
break;
}
}
return new CompositePropagator({
propagators,
});
}