Skip to content

Commit 2ae0370

Browse files
committed
Add sqs handler test for promise style async handler
1 parent f57ecaf commit 2ae0370

File tree

2 files changed

+50
-5
lines changed

2 files changed

+50
-5
lines changed

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

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -828,7 +828,7 @@ describe('lambda handler', () => {
828828
});
829829
});
830830

831-
describe('sqs test', () => {
831+
describe('sync handler sqs propagation', () => {
832832
it('creates process span for sqs record, with lambda invocation span as parent and span link to the producer traceId and spanId', async () => {
833833
initializeHandler('lambda-test/sync.sqshandler');
834834
const producerTraceId = '1df415edd0ad7f83e573f6504381dcec';
@@ -867,4 +867,42 @@ describe('lambda handler', () => {
867867
assert.equal(spans[0].links[0].context.spanId, producerSpanId);
868868
});
869869
});
870+
871+
describe('async handler sqs propagation', () => {
872+
it('creates process span for sqs record, with lambda invocation span as parent and span link to the producer traceId and spanId', async () => {
873+
initializeHandler('lambda-test/async.sqshandler');
874+
const producerTraceId = '1df415edd0ad7f83e573f6504381dcec';
875+
const producerSpanId = '83b7424a259945cb';
876+
const event = {
877+
Records: [
878+
{
879+
messageAttributes: {
880+
traceparent: {
881+
stringValue: `00-${producerTraceId}-${producerSpanId}-01`,
882+
dataType: 'String',
883+
},
884+
},
885+
eventSource: 'aws:sqs',
886+
eventSourceARN:
887+
'arn:aws:sqs:eu-central-1:783764587482:launch-queue',
888+
},
889+
],
890+
};
891+
892+
await lambdaRequire('lambda-test/async').sqshandler(event, ctx);
893+
const spans = memoryExporter.getFinishedSpans();
894+
895+
assert.strictEqual(spans.length, 2);
896+
assert.equal(
897+
spans[0].parentSpanContext?.traceId,
898+
spans[1].spanContext().traceId
899+
);
900+
assert.equal(
901+
spans[0].parentSpanContext?.spanId,
902+
spans[1].spanContext().spanId
903+
);
904+
assert.equal(spans[0].links[0]?.context.traceId, producerTraceId);
905+
assert.equal(spans[0].links[0].context.spanId, producerSpanId);
906+
});
907+
});
870908
});

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

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,26 @@ exports.handler = async function (event, context) {
1919
return 'ok';
2020
};
2121

22+
exports.sqshandler = async function (event, context) {
23+
event.Records.forEach(r => {});
24+
return 'ok';
25+
};
26+
2227
exports.error = async function (event, context) {
2328
throw new Error('handler error');
24-
}
29+
};
2530

2631
exports.stringerror = async function (event, context) {
2732
throw 'handler error';
28-
}
33+
};
2934

3035
exports.context = async function (event, context) {
3136
return api.trace.getSpan(api.context.active()).spanContext().traceId;
3237
};
3338

3439
exports.handler_return_baggage = async function (event, context) {
35-
const [baggageEntryKey, baggageEntryValue] = api.propagation.getBaggage(api.context.active()).getAllEntries()[0];
40+
const [baggageEntryKey, baggageEntryValue] = api.propagation
41+
.getBaggage(api.context.active())
42+
.getAllEntries()[0];
3643
return `${baggageEntryKey}=${baggageEntryValue.value}`;
37-
}
44+
};

0 commit comments

Comments
 (0)