forked from open-telemetry/opentelemetry-js
/
BasicTracerProvider.test.ts
554 lines (499 loc) · 18 KB
/
BasicTracerProvider.test.ts
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
/*
* Copyright The OpenTelemetry Authors
*
* 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
*
* https://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 {
context,
trace,
SpanContext,
TraceFlags,
ROOT_CONTEXT,
TextMapPropagator,
TextMapSetter,
Context,
TextMapGetter,
propagation,
diag,
} from '@opentelemetry/api';
import { CompositePropagator } from '@opentelemetry/core';
import {
AlwaysOnSampler,
AlwaysOffSampler,
TraceState,
} from '@opentelemetry/core';
import { Resource } from '@opentelemetry/resources';
import * as assert from 'assert';
import * as sinon from 'sinon';
import {
BasicTracerProvider,
NoopSpanProcessor,
Span,
InMemorySpanExporter,
SpanExporter,
BatchSpanProcessor,
} from '../../src';
describe('BasicTracerProvider', () => {
let removeEvent: Function | undefined;
const envSource = (typeof window !== 'undefined'
? window
: process.env) as any;
beforeEach(() => {
context.disable();
});
afterEach(() => {
sinon.restore();
if (removeEvent) {
removeEvent();
removeEvent = undefined;
}
});
describe('constructor', () => {
describe('when options not defined', () => {
it('should construct an instance', () => {
const tracer = new BasicTracerProvider();
assert.ok(tracer instanceof BasicTracerProvider);
});
it('should use noop span processor by default', () => {
const tracer = new BasicTracerProvider();
assert.ok(tracer.activeSpanProcessor instanceof NoopSpanProcessor);
});
});
describe('when "sampler" option defined', () => {
it('should have an instance with sampler', () => {
const tracer = new BasicTracerProvider({
sampler: new AlwaysOnSampler(),
});
assert.ok(tracer instanceof BasicTracerProvider);
});
});
describe('spanLimits', () => {
describe('when not defined default values', () => {
it('should have tracer with default values', () => {
const tracer = new BasicTracerProvider({}).getTracer('default');
assert.deepStrictEqual(tracer.getSpanLimits(), {
attributeValueLengthLimit: Infinity,
attributeCountLimit: 128,
eventCountLimit: 128,
linkCountLimit: 128,
});
});
});
describe('when "attributeCountLimit" is defined', () => {
it('should have tracer with defined value', () => {
const tracer = new BasicTracerProvider({
spanLimits: {
attributeCountLimit: 100,
},
}).getTracer('default');
const spanLimits = tracer.getSpanLimits();
assert.strictEqual(spanLimits.attributeCountLimit, 100);
});
});
describe('when "attributeValueLengthLimit" is defined', () => {
it('should have tracer with defined value', () => {
const tracer = new BasicTracerProvider({
spanLimits: {
attributeValueLengthLimit: 10,
},
}).getTracer('default');
const spanLimits = tracer.getSpanLimits();
assert.strictEqual(spanLimits.attributeValueLengthLimit, 10);
});
it('should have tracer with negative "attributeValueLengthLimit" value', () => {
const tracer = new BasicTracerProvider({
spanLimits: {
attributeValueLengthLimit: -10,
},
}).getTracer('default');
const spanLimits = tracer.getSpanLimits();
assert.strictEqual(spanLimits.attributeValueLengthLimit, -10);
});
});
describe('when "eventCountLimit" is defined', () => {
it('should have tracer with defined value', () => {
const tracer = new BasicTracerProvider({
spanLimits: {
eventCountLimit: 300,
},
}).getTracer('default');
const spanLimits = tracer.getSpanLimits();
assert.strictEqual(spanLimits.eventCountLimit, 300);
});
});
describe('when "linkCountLimit" is defined', () => {
it('should have tracer with defined value', () => {
const tracer = new BasicTracerProvider({
spanLimits: {
linkCountLimit: 10,
},
}).getTracer('default');
const spanLimits = tracer.getSpanLimits();
assert.strictEqual(spanLimits.linkCountLimit, 10);
});
});
});
});
describe('.register()', () => {
describe('propagator', () => {
class DummyPropagator implements TextMapPropagator {
inject(
context: Context,
carrier: any,
setter: TextMapSetter<any>
): void {
throw new Error('Method not implemented.');
}
extract(
context: Context,
carrier: any,
getter: TextMapGetter<any>
): Context {
throw new Error('Method not implemented.');
}
fields(): string[] {
throw new Error('Method not implemented.');
}
}
let setGlobalPropagatorStub: sinon.SinonSpy<
[TextMapPropagator],
boolean
>;
let originalPropagators: string | number | undefined | string[];
beforeEach(() => {
setGlobalPropagatorStub = sinon.spy(propagation, 'setGlobalPropagator');
originalPropagators = envSource.OTEL_PROPAGATORS;
});
afterEach(() => {
setGlobalPropagatorStub.restore();
// otherwise we may assign 'undefined' (a string)
if (originalPropagators !== undefined) {
envSource.OTEL_PROPAGATORS = originalPropagators;
} else {
delete envSource.OTEL_PROPAGATORS;
}
});
it('should be set to a given value if it it provided', () => {
const provider = new BasicTracerProvider();
provider.register({
propagator: new DummyPropagator(),
});
assert.ok(
setGlobalPropagatorStub.calledOnceWithExactly(
sinon.match.instanceOf(DummyPropagator)
)
);
});
it('should be composite if 2 or more propagators provided in an environment variable', () => {
const provider = new BasicTracerProvider();
provider.register();
assert.ok(
setGlobalPropagatorStub.calledOnceWithExactly(
sinon.match.instanceOf(CompositePropagator)
)
);
assert.deepStrictEqual(setGlobalPropagatorStub.args[0][0].fields(), [
'traceparent',
'tracestate',
'baggage',
]);
});
it('warns if there is no propagator registered with a given name', () => {
const warnStub = sinon.spy(diag, 'warn');
envSource.OTEL_PROPAGATORS = 'missing-propagator';
const provider = new BasicTracerProvider({});
provider.register();
assert.ok(
warnStub.calledOnceWithExactly(
'Propagator "missing-propagator" requested through environment variable is unavailable.'
)
);
warnStub.restore();
});
});
describe('exporter', () => {
class CustomTracerProvider extends BasicTracerProvider {
protected override _getSpanExporter(name: string): SpanExporter | undefined {
return name === 'memory'
? new InMemorySpanExporter()
: BasicTracerProvider._registeredExporters.get(name)?.();
}
}
afterEach(() => {
delete envSource.OTEL_TRACES_EXPORTER;
});
it('logs error if there is no exporter registered with a given name', () => {
const errorStub = sinon.spy(diag, 'error');
envSource.OTEL_TRACES_EXPORTER = 'missing-exporter';
const provider = new BasicTracerProvider({});
provider.register();
assert.ok(
errorStub.getCall(0).args[0] ===
'Exporter "missing-exporter" requested through environment variable is unavailable.'
);
errorStub.restore();
});
it('registers trace exporter from environment variable', () => {
envSource.OTEL_TRACES_EXPORTER = 'memory';
const provider = new CustomTracerProvider({});
provider.register();
const processor = provider.getActiveSpanProcessor();
assert(processor instanceof BatchSpanProcessor);
// @ts-expect-error access configured to verify its the correct one
const exporter = processor._exporter;
assert(exporter instanceof InMemorySpanExporter);
});
});
});
describe('.startSpan()', () => {
it('should start a span with name only', () => {
const tracer = new BasicTracerProvider().getTracer('default');
const span = tracer.startSpan('my-span');
assert.ok(span);
assert.ok(span instanceof Span);
});
it('should propagate resources', () => {
const tracerProvider = new BasicTracerProvider();
const tracer = tracerProvider.getTracer('default');
const span = tracer.startSpan('my-span') as Span;
assert.strictEqual(tracer.resource, tracerProvider.resource);
assert.strictEqual(span.resource, tracerProvider.resource);
});
it('should start a span with name and options', () => {
const tracer = new BasicTracerProvider().getTracer('default');
const span = tracer.startSpan('my-span', {});
assert.ok(span);
assert.ok(span instanceof Span);
const context = span.spanContext();
assert.ok(context.traceId.match(/[a-f0-9]{32}/));
assert.ok(context.spanId.match(/[a-f0-9]{16}/));
assert.strictEqual(context.traceFlags, TraceFlags.SAMPLED);
assert.deepStrictEqual(context.traceState, undefined);
span.end();
});
it('should start a span with given attributes', () => {
const tracer = new BasicTracerProvider().getTracer('default');
const span = tracer.startSpan('my-span', {
attributes: { foo: 'foo', bar: 'bar' },
}) as Span;
assert.deepStrictEqual(span.attributes, { bar: 'bar', foo: 'foo' });
span.end();
});
it('should start a span with spanoptions->attributes', () => {
const tracer = new BasicTracerProvider().getTracer('default');
const span = tracer.startSpan('my-span', {
attributes: { foo: 'foo', bar: 'bar' },
}) as Span;
assert.deepStrictEqual(span.attributes, { foo: 'foo', bar: 'bar' });
span.end();
});
it('should start a span with name and parent spancontext', () => {
const tracer = new BasicTracerProvider().getTracer('default');
const state = new TraceState('a=1,b=2');
const span = tracer.startSpan(
'my-span',
{},
trace.setSpanContext(ROOT_CONTEXT, {
traceId: 'd4cda95b652f4a1592b449d5929fda1b',
spanId: '6e0c63257de34c92',
traceFlags: TraceFlags.SAMPLED,
traceState: state,
})
);
assert.ok(span instanceof Span);
const context = span.spanContext();
assert.strictEqual(context.traceId, 'd4cda95b652f4a1592b449d5929fda1b');
assert.strictEqual(context.traceFlags, TraceFlags.SAMPLED);
assert.deepStrictEqual(context.traceState, state);
span.end();
});
it('should start a span with name and parent span', () => {
const tracer = new BasicTracerProvider().getTracer('default');
const span = tracer.startSpan('my-span');
const childSpan = tracer.startSpan(
'child-span',
{},
trace.setSpan(ROOT_CONTEXT, span)
);
const context = childSpan.spanContext();
assert.strictEqual(context.traceId, span.spanContext().traceId);
assert.strictEqual(context.traceFlags, TraceFlags.SAMPLED);
span.end();
childSpan.end();
});
it('should create a root span when root is true', () => {
const tracer = new BasicTracerProvider().getTracer('default');
const span = tracer.startSpan('my-span');
const overrideParent = tracer.startSpan('my-parent-override-span');
const rootSpan = tracer.startSpan(
'root-span',
{ root: true },
trace.setSpan(ROOT_CONTEXT, span)
);
const context = rootSpan.spanContext();
assert.notStrictEqual(context.traceId, overrideParent.spanContext().traceId);
span.end();
rootSpan.end();
});
it('should start a span with name and with invalid parent span', () => {
const tracer = new BasicTracerProvider({
sampler: new AlwaysOnSampler(),
}).getTracer('default');
const span = tracer.startSpan(
'my-span',
{},
trace.setSpanContext(
ROOT_CONTEXT,
('invalid-parent' as unknown) as SpanContext
)
);
assert.ok(span instanceof Span);
assert.deepStrictEqual((span as Span).parentSpanId, undefined);
});
it('should start a span with name and with invalid spancontext', () => {
const tracer = new BasicTracerProvider().getTracer('default');
const span = tracer.startSpan(
'my-span',
{},
trace.setSpanContext(ROOT_CONTEXT, {
traceId: '0',
spanId: '0',
traceFlags: TraceFlags.SAMPLED,
})
);
assert.ok(span instanceof Span);
const context = span.spanContext();
assert.ok(context.traceId.match(/[a-f0-9]{32}/));
assert.ok(context.spanId.match(/[a-f0-9]{16}/));
assert.strictEqual(context.traceFlags, TraceFlags.SAMPLED);
assert.deepStrictEqual(context.traceState, undefined);
});
it('should return a non recording span when never sampling', () => {
const tracer = new BasicTracerProvider({
sampler: new AlwaysOffSampler(),
}).getTracer('default');
const span = tracer.startSpan('my-span');
assert.ok(!span.isRecording());
const context = span.spanContext();
assert.ok(context.traceId.match(/[a-f0-9]{32}/));
assert.ok(context.spanId.match(/[a-f0-9]{16}/));
assert.strictEqual(context.traceFlags, TraceFlags.NONE);
assert.deepStrictEqual(context.traceState, undefined);
span.end();
});
it('should create real span when sampled', () => {
const tracer = new BasicTracerProvider({
sampler: new AlwaysOnSampler(),
}).getTracer('default');
const span = tracer.startSpan('my-span');
assert.ok(span instanceof Span);
assert.strictEqual(span.spanContext().traceFlags, TraceFlags.SAMPLED);
assert.strictEqual(span.isRecording(), true);
});
it('should assign a resource', () => {
const tracer = new BasicTracerProvider().getTracer('default');
const span = tracer.startSpan('my-span') as Span;
assert.ok(span);
assert.ok(span.resource instanceof Resource);
});
});
describe('.withSpan()', () => {
it('should run context with NoopContextManager context manager', done => {
const tracer = new BasicTracerProvider().getTracer('default');
const span = tracer.startSpan('my-span');
context.with(trace.setSpan(context.active(), span), () => {
assert.deepStrictEqual(trace.getSpan(context.active()), undefined);
return done();
});
});
});
describe('.forceFlush()', () => {
it('should call forceFlush on all registered span processors', done => {
sinon.restore();
const forceFlushStub = sinon.stub(
NoopSpanProcessor.prototype,
'forceFlush'
);
forceFlushStub.resolves();
const tracerProvider = new BasicTracerProvider();
const spanProcessorOne = new NoopSpanProcessor();
const spanProcessorTwo = new NoopSpanProcessor();
tracerProvider.addSpanProcessor(spanProcessorOne);
tracerProvider.addSpanProcessor(spanProcessorTwo);
tracerProvider
.forceFlush()
.then(() => {
sinon.restore();
assert(forceFlushStub.calledTwice);
done();
})
.catch(error => {
sinon.restore();
done(error);
});
});
it('should throw error when calling forceFlush on all registered span processors fails', done => {
sinon.restore();
const forceFlushStub = sinon.stub(
NoopSpanProcessor.prototype,
'forceFlush'
);
forceFlushStub.returns(Promise.reject('Error'));
const tracerProvider = new BasicTracerProvider();
const spanProcessorOne = new NoopSpanProcessor();
const spanProcessorTwo = new NoopSpanProcessor();
tracerProvider.addSpanProcessor(spanProcessorOne);
tracerProvider.addSpanProcessor(spanProcessorTwo);
tracerProvider
.forceFlush()
.then(() => {
sinon.restore();
done(new Error('Successful forceFlush not expected'));
})
.catch(_error => {
sinon.restore();
sinon.assert.calledTwice(forceFlushStub);
done();
});
});
});
describe('.bind()', () => {
it('should bind context with NoopContextManager context manager', done => {
const tracer = new BasicTracerProvider().getTracer('default');
const span = tracer.startSpan('my-span');
const fn = () => {
assert.deepStrictEqual(trace.getSpan(context.active()), undefined);
return done();
};
const patchedFn = context.bind(trace.setSpan(context.active(), span), fn);
return patchedFn();
});
});
describe('.resource', () => {
it('should return a Resource', () => {
const tracerProvider = new BasicTracerProvider();
assert.ok(tracerProvider.resource instanceof Resource);
});
});
describe('.shutdown()', () => {
it('should trigger shutdown when manually invoked', () => {
const tracerProvider = new BasicTracerProvider();
const shutdownStub = sinon.stub(
tracerProvider.getActiveSpanProcessor(),
'shutdown'
);
tracerProvider.shutdown();
sinon.assert.calledOnce(shutdownStub);
});
});
});