diff --git a/plugins/node/opentelemetry-instrumentation-aws-sdk/src/services/MessageAttributes.ts b/plugins/node/opentelemetry-instrumentation-aws-sdk/src/services/MessageAttributes.ts index 45aa2b617f..182f250b4c 100644 --- a/plugins/node/opentelemetry-instrumentation-aws-sdk/src/services/MessageAttributes.ts +++ b/plugins/node/opentelemetry-instrumentation-aws-sdk/src/services/MessageAttributes.ts @@ -106,3 +106,9 @@ export const extractPropagationContext = ( } return undefined; }; + +export const deduplicateMessageAttributeNames = (messageAttributeNames: string[]) => { + return Array.from( + new Set(messageAttributeNames) + ); +}; diff --git a/plugins/node/opentelemetry-instrumentation-aws-sdk/src/services/sqs.ts b/plugins/node/opentelemetry-instrumentation-aws-sdk/src/services/sqs.ts index 1a54c12241..58c0511b98 100644 --- a/plugins/node/opentelemetry-instrumentation-aws-sdk/src/services/sqs.ts +++ b/plugins/node/opentelemetry-instrumentation-aws-sdk/src/services/sqs.ts @@ -39,6 +39,7 @@ import { contextGetter, extractPropagationContext, injectPropagationContext, + deduplicateMessageAttributeNames } from './MessageAttributes'; export class SqsServiceExtension implements ServiceExtension { @@ -67,12 +68,13 @@ export class SqsServiceExtension implements ServiceExtension { spanAttributes[SemanticAttributes.MESSAGING_OPERATION] = MessagingOperationValues.RECEIVE; - request.commandInput.MessageAttributeNames = Array.from( - new Set([ - ...(request.commandInput.MessageAttributeNames ?? []), - ...propagation.fields(), - ]) - ); + const messageAttributeNames = request.commandInput.MessageAttributeNames ? + deduplicateMessageAttributeNames(request.commandInput.MessageAttributeNames) : []; + + request.commandInput.MessageAttributeNames = [ + ...messageAttributeNames, + ...propagation.fields() + ]; } break; diff --git a/plugins/node/opentelemetry-instrumentation-aws-sdk/test/MessageAttributes.test.ts b/plugins/node/opentelemetry-instrumentation-aws-sdk/test/MessageAttributes.test.ts index 9cb8cfdde5..f164672adb 100644 --- a/plugins/node/opentelemetry-instrumentation-aws-sdk/test/MessageAttributes.test.ts +++ b/plugins/node/opentelemetry-instrumentation-aws-sdk/test/MessageAttributes.test.ts @@ -19,6 +19,7 @@ import { MAX_MESSAGE_ATTRIBUTES, contextSetter, injectPropagationContext, + deduplicateMessageAttributeNames } from '../src/services/MessageAttributes'; describe('MessageAttributes', () => { @@ -76,4 +77,12 @@ describe('MessageAttributes', () => { expect(Object.keys(contextAttributes).length).toBe(10); }); }); + + describe.only('deduplicateMessageAttributeNames', () => { + it('should remove duplicate message attribute names', () => { + const messageAttributeNames = ["name 1", "name 2", "name 1"]; + + expect(deduplicateMessageAttributeNames(messageAttributeNames)).toEqual(["name 1", "name 2"]); + }) + }); }); diff --git a/plugins/node/opentelemetry-instrumentation-aws-sdk/test/sqs.test.ts b/plugins/node/opentelemetry-instrumentation-aws-sdk/test/sqs.test.ts index 9563af1de9..901b4fca1d 100644 --- a/plugins/node/opentelemetry-instrumentation-aws-sdk/test/sqs.test.ts +++ b/plugins/node/opentelemetry-instrumentation-aws-sdk/test/sqs.test.ts @@ -67,17 +67,8 @@ describe('SQS', () => { }); beforeEach(() => { - const customMessageAttribute = { - TestMessageAttribute: { - DataType: 'String', - StringValue: 'test value', - }, - }; - mockV2AwsSend(responseMockSuccess, { - Messages: [ - { Body: 'msg 1 payload', MessageAttributes: customMessageAttribute }, - { Body: 'msg 2 payload', MessageAttributes: customMessageAttribute }], + Messages: [{ Body: 'msg 1 payload' }, { Body: 'msg 2 payload' }], } as AWS.SQS.Types.ReceiveMessageResult); }); @@ -370,15 +361,6 @@ describe('SQS', () => { ); }); }); - - it('should have the custom added message attributes', async () => { - receivedMessages. - forEach(msg => { - expect(msg.MessageAttributes?.TestMessageAttribute?.StringValue).toEqual( - 'test value' - ); - }) - }); }); describe('hooks', () => {