Skip to content

Commit 81b51aa

Browse files
committed
Add context to publish batch
1 parent 143a1f4 commit 81b51aa

File tree

3 files changed

+70
-15
lines changed

3 files changed

+70
-15
lines changed

Diff for: plugins/node/opentelemetry-instrumentation-aws-sdk/src/services/sns.ts

+30-8
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import {
2525
NormalizedResponse,
2626
AwsSdkInstrumentationConfig,
2727
} from '../types';
28+
import type { SNS } from '../aws-sdk.types';
2829
import { injectPropagationContext } from './MessageAttributes';
2930
import { RequestMetadata, ServiceExtension } from './ServiceExtension';
3031

@@ -67,14 +68,35 @@ export class SnsServiceExtension implements ServiceExtension {
6768
}
6869

6970
requestPostSpanHook(request: NormalizedRequest): void {
70-
if (request.commandName === 'Publish') {
71-
const origMessageAttributes =
72-
request.commandInput['MessageAttributes'] ?? {};
73-
if (origMessageAttributes) {
74-
request.commandInput['MessageAttributes'] = injectPropagationContext(
75-
origMessageAttributes
76-
);
77-
}
71+
switch (request.commandName) {
72+
case 'Publish':
73+
{
74+
const origMessageAttributes =
75+
request.commandInput['MessageAttributes'] ?? {};
76+
if (origMessageAttributes) {
77+
request.commandInput['MessageAttributes'] = injectPropagationContext(
78+
origMessageAttributes
79+
);
80+
}
81+
}
82+
break;
83+
84+
case 'PublishBatch':
85+
{
86+
const entries = request.commandInput?.PublishBatchRequestEntries;
87+
if (Array.isArray(entries)) {
88+
entries.forEach(
89+
(messageParams: {
90+
MessageAttributes: SNS.MessageAttributeMap;
91+
}) => {
92+
messageParams.MessageAttributes = injectPropagationContext(
93+
messageParams.MessageAttributes ?? {}
94+
);
95+
}
96+
);
97+
}
98+
}
99+
break;
78100
}
79101
}
80102

Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<?xml version="1.0"?><PublishBatchResponse xmlns="http://sns.amazonaws.com/doc/2010-03-31/"><PublishBatchResult><Successful><MessageId>90d1987b-4853-54ad-a499-c2d89c4edf3a</MessageId><Successful></PublishBatchResult><ResponseMetadata><RequestId>d81e4f4f-2d70-51f3-a040-15ecf96d5a64</RequestId></ResponseMetadata></PublishBatchResponse>

Diff for: plugins/node/opentelemetry-instrumentation-aws-sdk/test/sns.test.ts

+39-7
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ const responseMockSuccess = {
4747
const topicName = 'topic';
4848
const fakeARN = `arn:aws:sns:region:000000000:${topicName}`;
4949

50+
const hookSpy = sinon.spy(
51+
(instrumentation['servicesExtensions'] as any)['services'].get('SNS'),
52+
'requestPostSpanHook'
53+
);
54+
5055
describe('SNS - v2', () => {
5156
before(() => {
5257
AWSv2.config.credentials = {
@@ -64,6 +69,10 @@ describe('SNS - v2', () => {
6469
} as AWS.SNS.Types.PublishResponse);
6570
});
6671

72+
afterEach(() => {
73+
hookSpy.resetHistory();
74+
})
75+
6776
describe('publish', () => {
6877
it('topic arn', async () => {
6978
const sns = new AWSv2.SNS();
@@ -120,10 +129,6 @@ describe('SNS - v2', () => {
120129

121130
it('inject context propagation', async () => {
122131
const sns = new AWSv2.SNS();
123-
const hookSpy = sinon.spy(
124-
(instrumentation['servicesExtensions'] as any)['services'].get('SNS'),
125-
'requestPostSpanHook'
126-
);
127132

128133
await sns
129134
.publish({
@@ -180,16 +185,18 @@ describe('SNS - v3', () => {
180185
secretAccessKey: 'abcde',
181186
},
182187
});
188+
});
183189

184-
nock('https://sns.us-east-1.amazonaws.com/')
190+
describe('publish', () => {
191+
beforeEach(() => {
192+
nock('https://sns.us-east-1.amazonaws.com/')
185193
.post('/')
186194
.reply(
187195
200,
188196
fs.readFileSync('./test/mock-responses/sns-publish.xml', 'utf8')
189197
);
190-
});
198+
});
191199

192-
describe('publish', () => {
193200
it('topic arn', async () => {
194201
const topicV3Name = 'dummy-sns-v3-topic';
195202
const topicV3ARN = `arn:aws:sns:us-east-1:000000000:${topicV3Name}`;
@@ -236,4 +243,29 @@ describe('SNS - v3', () => {
236243
);
237244
});
238245
});
246+
247+
describe('publish batch', () => {
248+
it('inject context propagation for publish batch command', async () => {
249+
nock('https://sns.us-east-1.amazonaws.com/')
250+
.post('/')
251+
.reply(
252+
200,
253+
fs.readFileSync('./test/mock-responses/sns-publish-batch.xml', 'utf8')
254+
);
255+
256+
await sns
257+
.publishBatch({
258+
TopicArn: fakeARN,
259+
PublishBatchRequestEntries: [{ Id: '1', Message: 'sns message' }]
260+
});
261+
262+
const publishSpans = getTestSpans().filter(
263+
(s: ReadableSpan) => s.name === `${topicName} send`
264+
);
265+
expect(publishSpans.length).toBe(1);
266+
expect(
267+
hookSpy.args[0][0].commandInput.PublishBatchRequestEntries[0].MessageAttributes.traceparent
268+
).toBeDefined();
269+
});
270+
});
239271
});

0 commit comments

Comments
 (0)