Skip to content

Commit da792fe

Browse files
nozikNathanielRNrauno56
authored
feat: support baggage propagation in aws lambda custom context extraction (#843)
Co-authored-by: Nathaniel Ruiz Nowell <[email protected]> Co-authored-by: Rauno Viskus <[email protected]>
1 parent 78717b6 commit da792fe

File tree

3 files changed

+37
-1
lines changed

3 files changed

+37
-1
lines changed

plugins/node/opentelemetry-instrumentation-aws-lambda/src/instrumentation.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ export class AwsLambdaInstrumentation extends InstrumentationBase {
190190
);
191191
}
192192

193-
return otelContext.with(trace.setSpan(otelContext.active(), span), () => {
193+
return otelContext.with(trace.setSpan(parent, span), () => {
194194
// Lambda seems to pass a callback even if handler is of Promise form, so we wrap all the time before calling
195195
// the handler and see if the result is a Promise or not. In such a case, the callback is usually ignored. If
196196
// the handler happened to both call the callback and complete a returned Promise, whichever happens first will

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -626,6 +626,37 @@ describe('lambda handler', () => {
626626
assert.strictEqual(span.parentSpanId, sampledGenericSpanContext.spanId);
627627
});
628628

629+
it('prefers to extract baggage over sampled lambda context if "eventContextExtractor" is defined', async () => {
630+
process.env[traceContextEnvironmentKey] = sampledAwsHeader;
631+
const customExtractor = (event: any): OtelContext => {
632+
return propagation.extract(
633+
context.active(),
634+
event.customContextCarrier
635+
);
636+
};
637+
638+
initializeHandler('lambda-test/async.handler_return_baggage', {
639+
disableAwsContextPropagation: true,
640+
eventContextExtractor: customExtractor,
641+
});
642+
643+
const baggage = 'abcd=1234';
644+
const customRemoteEvent = {
645+
customContextCarrier: {
646+
traceparent: sampledGenericSpan,
647+
baggage,
648+
},
649+
};
650+
651+
const lambdaTestAsync = lambdaRequire('lambda-test/async');
652+
const actual = await lambdaTestAsync.handler_return_baggage(
653+
customRemoteEvent,
654+
ctx
655+
);
656+
657+
assert.strictEqual(actual, baggage);
658+
});
659+
629660
it('creates trace from ROOT_CONTEXT when "disableAwsContextPropagation" is true, eventContextExtractor is provided, and no custom context is found', async () => {
630661
process.env[traceContextEnvironmentKey] = sampledAwsHeader;
631662
const customExtractor = (event: any): OtelContext => {

plugins/node/opentelemetry-instrumentation-aws-lambda/test/lambda-test/async.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,8 @@ exports.stringerror = async function (event, context) {
3030
exports.context = async function (event, context) {
3131
return api.trace.getSpan(api.context.active()).spanContext().traceId;
3232
};
33+
34+
exports.handler_return_baggage = async function (event, context) {
35+
const [baggageEntryKey, baggageEntryValue] = api.propagation.getBaggage(api.context.active()).getAllEntries()[0];
36+
return `${baggageEntryKey}=${baggageEntryValue.value}`;
37+
}

0 commit comments

Comments
 (0)