diff --git a/plugins/node/opentelemetry-instrumentation-aws-lambda/src/instrumentation.ts b/plugins/node/opentelemetry-instrumentation-aws-lambda/src/instrumentation.ts index c113723305..6178130bb4 100644 --- a/plugins/node/opentelemetry-instrumentation-aws-lambda/src/instrumentation.ts +++ b/plugins/node/opentelemetry-instrumentation-aws-lambda/src/instrumentation.ts @@ -35,6 +35,7 @@ import { TextMapGetter, TraceFlags, TracerProvider, + ROOT_CONTEXT, } from '@opentelemetry/api'; import { AWSXRAY_TRACE_ID_HEADER, @@ -375,7 +376,7 @@ export class AwsLambdaInstrumentation extends InstrumentationBase { } if (!parent) { // No context in Lambda environment or HTTP headers. - return otelContext.active(); + return ROOT_CONTEXT; } return parent; } diff --git a/plugins/node/opentelemetry-instrumentation-aws-lambda/test/integrations/lambda-handler.test.ts b/plugins/node/opentelemetry-instrumentation-aws-lambda/test/integrations/lambda-handler.test.ts index 36ea4af4df..313f4f33ea 100644 --- a/plugins/node/opentelemetry-instrumentation-aws-lambda/test/integrations/lambda-handler.test.ts +++ b/plugins/node/opentelemetry-instrumentation-aws-lambda/test/integrations/lambda-handler.test.ts @@ -45,6 +45,7 @@ import { SpanKind, SpanStatusCode, TextMapPropagator, + ROOT_CONTEXT, } from '@opentelemetry/api'; import { AWSXRayPropagator } from '@opentelemetry/propagator-aws-xray'; import { W3CTraceContextPropagator } from '@opentelemetry/core'; @@ -624,6 +625,37 @@ describe('lambda handler', () => { ); assert.strictEqual(span.parentSpanId, sampledGenericSpanContext.spanId); }); + + it('creates trace from ROOT_CONTEXT when "disableAwsContextPropagation" is true, eventContextExtractor is provided, and no custom context is found', async () => { + process.env[traceContextEnvironmentKey] = sampledAwsHeader; + const customExtractor = (event: any): OtelContext => { + if (!event.contextCarrier) { + return ROOT_CONTEXT; + } + + return propagation.extract(context.active(), event.contextCarrier); + }; + + initializeHandler('lambda-test/async.handler', { + disableAwsContextPropagation: true, + eventContextExtractor: customExtractor, + }); + + const testSpan = provider.getTracer('test').startSpan('random_span'); + await context.with( + trace.setSpan(context.active(), testSpan), + async () => { + await lambdaRequire('lambda-test/async').handler( + { message: 'event with no context' }, + ctx + ); + } + ); + + const spans = memoryExporter.getFinishedSpans(); + const [span] = spans; + assert.strictEqual(span.parentSpanId, undefined); + }); }); describe('hooks', () => {