Skip to content

Commit

Permalink
fix: prevent invalid context propagation in lambda functions (#677)
Browse files Browse the repository at this point in the history
  • Loading branch information
nozik authored Oct 12, 2021
1 parent bc11f3d commit 25c0e30
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import {
TextMapGetter,
TraceFlags,
TracerProvider,
ROOT_CONTEXT,
} from '@opentelemetry/api';
import {
AWSXRAY_TRACE_ID_HEADER,
Expand Down Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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', () => {
Expand Down

0 comments on commit 25c0e30

Please sign in to comment.