-
Notifications
You must be signed in to change notification settings - Fork 224
/
OTelBridgeNonRecordingSpan.test.js
136 lines (121 loc) · 4.32 KB
/
OTelBridgeNonRecordingSpan.test.js
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
/*
* Copyright Elasticsearch B.V. and other contributors where applicable.
* Licensed under the BSD 2-Clause License; you may not use this file except in
* compliance with the BSD 2-Clause License.
*/
'use strict';
const apm = require('../..').start({
opentelemetryBridgeEnabled: true,
// Make the agent quiet.
disableSend: true,
centralConfig: false,
cloudProvider: 'none',
metricsInterval: '0s',
captureExceptions: false,
logLevel: 'off',
});
const otel = require('@opentelemetry/api');
const tape = require('tape');
const { OUTCOME_UNKNOWN } = require('../../lib/constants');
const {
OTelBridgeNonRecordingSpan,
} = require('../../lib/opentelemetry-bridge/OTelBridgeNonRecordingSpan');
tape.test('OTelBridgeNonRecordingSpan', (suite) => {
const parentSpanContext = {
traceId: 'd4cda95b652f4a1592b449dd92ffda3b',
spanId: '6e0c63ffe4e34c42',
traceFlags: otel.TraceFlags.SAMPLED,
};
let nrsOTelSpan;
let nrsTrans;
suite.test('setup', (t) => {
// `nrs` is an instance of the @opentelemetry/api NonRecordingSpan.
const nrs = otel.trace.wrapSpanContext(parentSpanContext);
// If we set that span on a Context and use the ContextManager to run in that
// context (via `otel.context.with()`) ...
otel.context.with(otel.trace.setSpan(otel.context.active(), nrs), () => {
// ... then that span passes through the OTel Bridge context management
// and comes out as an `OTelBridgeNonRecordingSpan` instance.
nrsOTelSpan = otel.trace.getSpan(otel.context.active());
t.ok(nrsOTelSpan instanceof OTelBridgeNonRecordingSpan);
// In Elastic APM terms, that "span" is the current transaction.
nrsTrans = apm.currentTransaction;
t.ok(nrsTrans instanceof OTelBridgeNonRecordingSpan);
t.strictEqual(nrsOTelSpan, nrsTrans);
t.end();
// This object must handle:
// - the full OTel `interface Span`
// - the full Elastic APM public API `interface Transaction`, and
// - any internal `class Transaction` API used by the APM agent.
});
});
suite.test('OTel interface Span', (t) => {
t.deepEqual(nrsOTelSpan.spanContext(), parentSpanContext, 'spanContext');
t.equal(
nrsOTelSpan.setAttribute('foo', 'bar'),
nrsOTelSpan,
'setAttribute',
);
t.equal(
nrsOTelSpan.setAttributes({ foo: 'bar' }),
nrsOTelSpan,
'setAttributes',
);
t.equal(nrsOTelSpan.addEvent('anEvent'), nrsOTelSpan, 'addEvent');
t.equal(
nrsOTelSpan.setStatus(otel.SpanStatusCode.OK),
nrsOTelSpan,
'setStatus',
);
t.equal(nrsOTelSpan.updateName('anotherName'), nrsOTelSpan, 'updateName');
const linkContext = {
traceId: '8b46594050c89c3d87248476ed8e0c57',
spanId: 'ffe4cfa94865ee2a',
traceFlags: otel.TraceFlags.SAMPLED,
};
t.equal(nrsOTelSpan.addLink(linkContext), nrsOTelSpan, 'addLink');
t.equal(nrsOTelSpan.addLinks([linkContext]), nrsOTelSpan, 'addLinks');
t.equal(nrsOTelSpan.end(), undefined, 'end');
t.equal(nrsOTelSpan.isRecording(), false, 'isRecording');
t.equal(
nrsOTelSpan.recordException(new Error('boom')),
undefined,
'recordException',
);
t.end();
});
suite.test('public interface Transaction API', (t) => {
t.equal(nrsTrans.name, '', 'name');
t.equal(nrsTrans.type, null, 'type');
t.equal(nrsTrans.subtype, null, 'subtype');
t.equal(nrsTrans.action, null, 'action');
t.equal(
nrsTrans.traceparent,
'00-d4cda95b652f4a1592b449dd92ffda3b-6e0c63ffe4e34c42-01',
'traceparent',
);
t.equal(nrsTrans.outcome, OUTCOME_UNKNOWN, 'outcome');
t.equal(nrsTrans.result, '', 'result');
t.deepEqual(
nrsTrans.ids,
{
'trace.id': 'd4cda95b652f4a1592b449dd92ffda3b',
'transaction.id': '6e0c63ffe4e34c42',
},
'ids',
);
t.equal(
nrsTrans.setType('aType', 'aSubtype', 'anAction'),
undefined,
'setType',
);
t.equal(nrsTrans.setLabel('foo', 'bar'), false, 'setLabel');
t.equal(nrsTrans.addLabels({ foo: 'bar' }), false, 'addLabels');
t.equal(nrsTrans.setOutcome(OUTCOME_UNKNOWN), undefined, 'setOutcome');
t.equal(nrsTrans.startSpan('aSpan', {}), null, 'setSpan');
t.equal(nrsTrans.ensureParentId(), '', 'ensureParentId');
t.equal(nrsTrans.end('aResult', 42), undefined, 'end');
t.end();
});
suite.end();
});