Skip to content

Commit ab400e8

Browse files
committed
add tests for getting parent span context
1 parent 06ed787 commit ab400e8

File tree

4 files changed

+111
-39
lines changed

4 files changed

+111
-39
lines changed

package-lock.json

+10-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

plugins/node/opentelemetry-instrumentation-aws-lambda/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ provider.register({
107107
});
108108
```
109109

110-
Alternatively, you can use the [auto-configuration-propagators](https://github.com/open-telemetry/opentelemetry-js-contrib/blob/main/metapackages/auto-configuration-propagators/README.md) package, which makes it possible to configure propgators via the `OTEL_PROPAGATORS` environment variable. In order to use the `AWSXRayLambdaPropagator`, set the env variable value to `xray-lambda`.
110+
Alternatively, you can use the [auto-configuration-propagators](https://github.com/open-telemetry/opentelemetry-js-contrib/blob/main/metapackages/auto-configuration-propagators/README.md) package, which makes it possible to configure propagators via the `OTEL_PROPAGATORS` environment variable. In order to use the `AWSXRayLambdaPropagator`, set the env variable value to `xray-lambda`.
111111

112112
```js
113113
const { NodeTracerProvider } = require('@opentelemetry/sdk-trace-node');

plugins/node/opentelemetry-instrumentation-aws-lambda/package.json

+2
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@
4545
"devDependencies": {
4646
"@opentelemetry/api": "^1.3.0",
4747
"@opentelemetry/core": "^1.8.0",
48+
"@opentelemetry/propagator-aws-xray": "^1.25.1",
49+
"@opentelemetry/propagator-aws-xray-lambda": "^0.52.1",
4850
"@opentelemetry/sdk-metrics": "^1.8.0",
4951
"@opentelemetry/sdk-trace-base": "^1.8.0",
5052
"@opentelemetry/sdk-trace-node": "^1.8.0",

plugins/node/opentelemetry-instrumentation-aws-lambda/test/integrations/lambda-handler.test.ts

+98-34
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,12 @@ import {
4646
SpanKind,
4747
SpanStatusCode,
4848
TextMapPropagator,
49+
ROOT_CONTEXT,
50+
defaultTextMapGetter,
4951
} from '@opentelemetry/api';
5052
import { AWSXRayPropagator } from '@opentelemetry/propagator-aws-xray';
5153
import { W3CTraceContextPropagator } from '@opentelemetry/core';
54+
import { AWSXRayLambdaPropagator } from '@opentelemetry/propagator-aws-xray-lambda';
5255

5356
const memoryExporter = new InMemorySpanExporter();
5457

@@ -405,49 +408,81 @@ describe('lambda handler', () => {
405408
});
406409

407410
describe('with remote parent', () => {
408-
it('passes the lambda context object into the eventContextExtractor for scenarios where it is the otel context carrier', async () => {
411+
beforeEach(() => {
412+
propagation.disable();
413+
});
414+
415+
it('uses globally registered propagator', async () => {
416+
propagation.setGlobalPropagator(new AWSXRayPropagator());
417+
initializeHandler('lambda-test/async.handler');
418+
419+
const proxyEvent = {
420+
headers: {
421+
'x-amzn-trace-id': sampledAwsHeader,
422+
},
423+
};
424+
425+
const result = await lambdaRequire('lambda-test/async').handler(
426+
proxyEvent,
427+
ctx
428+
);
429+
assert.strictEqual(result, 'ok');
430+
const spans = memoryExporter.getFinishedSpans();
431+
432+
assert.strictEqual(spans.length, 1);
433+
assert.equal(
434+
spans[0].spanContext().traceId,
435+
sampledAwsSpanContext.traceId
436+
);
437+
assert.equal(spans[0].parentSpanId, sampledAwsSpanContext.spanId);
438+
});
439+
440+
it('can extract context from lambda context env variable using a global propagator', async () => {
409441
process.env[traceContextEnvironmentKey] = sampledAwsHeader;
410-
const customExtractor = (
411-
event: any,
412-
handlerContext: Context
413-
): OtelContext => {
414-
const contextCarrier = handlerContext.clientContext?.Custom ?? {};
415-
return propagation.extract(context.active(), contextCarrier);
442+
propagation.setGlobalPropagator(new AWSXRayLambdaPropagator());
443+
initializeHandler('lambda-test/async.handler');
444+
445+
const result = await lambdaRequire('lambda-test/async').handler(
446+
'arg',
447+
ctx
448+
);
449+
450+
assert.strictEqual(result, 'ok');
451+
const spans = memoryExporter.getFinishedSpans();
452+
453+
assert.strictEqual(spans.length, 1);
454+
assert.equal(
455+
spans[0].spanContext().traceId,
456+
sampledAwsSpanContext.traceId
457+
);
458+
assert.equal(spans[0].parentSpanId, sampledAwsSpanContext.spanId);
459+
});
460+
461+
it('used custom eventContextExtractor over global propagator if defined', async () => {
462+
propagation.setGlobalPropagator(new W3CTraceContextPropagator());
463+
const customExtractor = (event: any): OtelContext => {
464+
const propagator = new AWSXRayPropagator();
465+
return propagator.extract(
466+
context.active(),
467+
event.contextCarrier,
468+
defaultTextMapGetter
469+
);
416470
};
417471

418472
initializeHandler('lambda-test/async.handler', {
419473
eventContextExtractor: customExtractor,
420474
});
421475

422-
const otherEvent = {};
423-
const ctxWithCustomData = {
424-
functionName: 'my_function',
425-
invokedFunctionArn: 'my_arn',
426-
awsRequestId: 'aws_request_id',
427-
clientContext: {
428-
client: {
429-
installationId: '',
430-
appTitle: '',
431-
appVersionName: '',
432-
appVersionCode: '',
433-
appPackageName: '',
434-
},
435-
Custom: {
436-
traceparent: sampledGenericSpan,
437-
},
438-
env: {
439-
platformVersion: '',
440-
platform: '',
441-
make: '',
442-
model: '',
443-
locale: '',
444-
},
476+
const otherEvent = {
477+
contextCarrier: {
478+
traceparent: sampledGenericSpan,
479+
'x-amzn-trace-id': sampledAwsHeader,
445480
},
446-
} as Context;
481+
};
447482

448483
const result = await lambdaRequire('lambda-test/async').handler(
449484
otherEvent,
450-
ctxWithCustomData
485+
ctx
451486
);
452487

453488
assert.strictEqual(result, 'ok');
@@ -457,9 +492,38 @@ describe('lambda handler', () => {
457492
assertSpanSuccess(span);
458493
assert.strictEqual(
459494
span.spanContext().traceId,
460-
sampledGenericSpanContext.traceId
495+
sampledAwsSpanContext.traceId
461496
);
462-
assert.strictEqual(span.parentSpanId, sampledGenericSpanContext.spanId);
497+
assert.strictEqual(span.parentSpanId, sampledAwsSpanContext.spanId);
498+
});
499+
500+
it('creates trace from ROOT_CONTEXT eventContextExtractor is provided, and no custom context is found', async () => {
501+
const customExtractor = (event: any): OtelContext => {
502+
if (!event.contextCarrier) {
503+
return ROOT_CONTEXT;
504+
}
505+
506+
return propagation.extract(context.active(), event.contextCarrier);
507+
};
508+
509+
const provider = initializeHandler('lambda-test/async.handler', {
510+
eventContextExtractor: customExtractor,
511+
});
512+
513+
const testSpan = provider.getTracer('test').startSpan('random_span');
514+
await context.with(
515+
trace.setSpan(context.active(), testSpan),
516+
async () => {
517+
await lambdaRequire('lambda-test/async').handler(
518+
{ message: 'event with no context' },
519+
ctx
520+
);
521+
}
522+
);
523+
524+
const spans = memoryExporter.getFinishedSpans();
525+
const [span] = spans;
526+
assert.strictEqual(span.parentSpanId, undefined);
463527
});
464528
});
465529

0 commit comments

Comments
 (0)