Skip to content

Commit e83f93f

Browse files
committed
add tests
1 parent a28e3cd commit e83f93f

File tree

1 file changed

+74
-3
lines changed

1 file changed

+74
-3
lines changed

nodejs/packages/layer/test/wrapper.spec.ts

Lines changed: 74 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,33 @@
11
import type { AwsSdkInstrumentationConfig } from '@opentelemetry/instrumentation-aws-sdk';
2-
import { stub } from 'sinon';
2+
import { SDKRegistrationConfig } from '@opentelemetry/sdk-trace-base';
3+
import { TextMapPropagator } from '@opentelemetry/api';
34

45
import { wrap, unwrap } from '../src/wrapper';
56

7+
import { stub } from 'sinon';
8+
import assert from 'assert';
9+
610
declare global {
711
function configureAwsInstrumentation(
812
defaultConfig: AwsSdkInstrumentationConfig,
913
): AwsSdkInstrumentationConfig;
14+
function configureSdkRegistration(
15+
defaultSdkRegistration: SDKRegistrationConfig,
16+
): SDKRegistrationConfig;
1017
}
1118

12-
const assert = require('assert');
13-
1419
describe('wrapper', () => {
20+
let oldEnv: NodeJS.ProcessEnv;
21+
1522
beforeEach(() => {
23+
oldEnv = { ...process.env };
24+
1625
unwrap();
1726
});
1827

1928
afterEach(() => {
29+
process.env = oldEnv;
30+
2031
unwrap();
2132
});
2233

@@ -30,4 +41,64 @@ describe('wrapper', () => {
3041
assert(configureAwsInstrumentationStub.calledOnce);
3142
});
3243
});
44+
45+
describe('getPropagator', () => {
46+
const testConfiguredPropagator = (
47+
propagatorNames: string[],
48+
expectedPropagatorFields: string[],
49+
) => {
50+
if (propagatorNames && propagatorNames.length) {
51+
process.env.OTEL_PROPAGATORS = propagatorNames.join(',');
52+
}
53+
54+
const configureSdkRegistrationStub = stub().returnsArg(0);
55+
global.configureSdkRegistration = configureSdkRegistrationStub;
56+
wrap();
57+
assert(configureSdkRegistrationStub.calledOnce);
58+
59+
const sdkRegistrationConfig: SDKRegistrationConfig =
60+
configureSdkRegistrationStub.getCall(0).firstArg;
61+
assert.notEqual(sdkRegistrationConfig, null);
62+
63+
const propagator: TextMapPropagator | null | undefined =
64+
sdkRegistrationConfig.propagator;
65+
assert.notEqual(propagator, null);
66+
67+
const actualPropagatorFields: string[] | undefined = propagator?.fields();
68+
assert.notEqual(actualPropagatorFields, null);
69+
assert.deepEqual(actualPropagatorFields, expectedPropagatorFields);
70+
};
71+
72+
it('is configured by default', () => {
73+
// by default, 'W3CTraceContextPropagator' and 'W3CBaggagePropagator' propagators are added.
74+
// - 'traceparent' and 'tracestate' fields are used by the 'W3CTraceContextPropagator'
75+
// - 'baggage' field is used by the 'W3CBaggagePropagator'
76+
testConfiguredPropagator([], ['traceparent', 'tracestate', 'baggage']);
77+
});
78+
79+
it('is configured to w3c-trace-context by env var', () => {
80+
// 'traceparent' and 'tracestate' fields are used by the 'W3CTraceContextPropagator'
81+
testConfiguredPropagator(['tracecontext'], ['traceparent', 'tracestate']);
82+
});
83+
84+
it('is configured to w3c-baggage by env var', () => {
85+
// 'baggage' field is used by the 'W3CBaggagePropagator'
86+
testConfiguredPropagator(['baggage'], ['baggage']);
87+
});
88+
89+
it('is configured to xray by env var', () => {
90+
// 'x-amzn-trace-id' field is used by the 'AWSXRayPropagator'
91+
testConfiguredPropagator(['xray'], ['x-amzn-trace-id']);
92+
});
93+
94+
it('is configured to xray-lambda by env var', () => {
95+
// 'x-amzn-trace-id' field is used by the 'AWSXRayLambdaPropagator'
96+
testConfiguredPropagator(['xray'], ['x-amzn-trace-id']);
97+
});
98+
99+
it('is configured by unsupported propagator', () => {
100+
// in case of unsupported propagator, warning log is printed and empty propagator array is returned
101+
testConfiguredPropagator(['jaeger'], []);
102+
});
103+
});
33104
});

0 commit comments

Comments
 (0)