Skip to content

Commit

Permalink
deduplicate message attribute names in seperate function and add unit…
Browse files Browse the repository at this point in the history
… tests

Co-authored-by: Jamie Danielson <[email protected]>
  • Loading branch information
pkanal and JamieDanielson committed Jun 6, 2022
1 parent b747f84 commit d7db9a3
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,9 @@ export const extractPropagationContext = (
}
return undefined;
};

export const deduplicateMessageAttributeNames = (messageAttributeNames: string[]) => {
return Array.from(
new Set(messageAttributeNames)
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import {
contextGetter,
extractPropagationContext,
injectPropagationContext,
deduplicateMessageAttributeNames
} from './MessageAttributes';

export class SqsServiceExtension implements ServiceExtension {
Expand Down Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
MAX_MESSAGE_ATTRIBUTES,
contextSetter,
injectPropagationContext,
deduplicateMessageAttributeNames
} from '../src/services/MessageAttributes';

describe('MessageAttributes', () => {
Expand Down Expand Up @@ -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"]);
})
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});

Expand Down Expand Up @@ -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', () => {
Expand Down

0 comments on commit d7db9a3

Please sign in to comment.