Skip to content

Commit

Permalink
fix(aws-sdk): avoid repeating MessageAttributeNames in sqs receiveMes…
Browse files Browse the repository at this point in the history
…sage (#1044)

* fix(instrumentation-aws-sdk): dedupe prop fields

Co-authored-by: Kent Quirk <[email protected]>
Co-authored-by: Purvi Kanal <[email protected]>
Co-authored-by: Mike Goldsmith <[email protected]>

* fix linting

* add test for message attributes

Co-authored-by: Purvi Kanal <[email protected]>

* test both messages

* deduplicate message attribute names in seperate function and add unit tests

Co-authored-by: Jamie Danielson <[email protected]>

* remove line to force ci checks

* appease linter

Co-authored-by: Purvi Kanal <[email protected]>

* appease linter

* move logic into dedupe function and add unit tests

* rename helper function to be more descriptive

Co-authored-by: Kent Quirk <[email protected]>
Co-authored-by: Purvi Kanal <[email protected]>
Co-authored-by: Mike Goldsmith <[email protected]>
Co-authored-by: Mike Goldsmth <[email protected]>
Co-authored-by: Purvi Kanal <[email protected]>
Co-authored-by: Jamie Danielson <[email protected]>
Co-authored-by: Purvi Kanal <[email protected]>
  • Loading branch information
8 people authored Jun 10, 2022
1 parent 23d98b1 commit 4b4ded6
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,12 @@ export const extractPropagationContext = (
}
return undefined;
};

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

export class SqsServiceExtension implements ServiceExtension {
Expand Down Expand Up @@ -67,9 +68,11 @@ export class SqsServiceExtension implements ServiceExtension {
spanAttributes[SemanticAttributes.MESSAGING_OPERATION] =
MessagingOperationValues.RECEIVE;

request.commandInput.MessageAttributeNames = (
request.commandInput.MessageAttributeNames ?? []
).concat(propagation.fields());
request.commandInput.MessageAttributeNames =
addPropagationFieldsToAttributeNames(
request.commandInput.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,
addPropagationFieldsToAttributeNames,
} from '../src/services/MessageAttributes';

describe('MessageAttributes', () => {
Expand Down Expand Up @@ -76,4 +77,24 @@ describe('MessageAttributes', () => {
expect(Object.keys(contextAttributes).length).toBe(10);
});
});

describe('addPropagationFieldsToAttributeNames', () => {
const messageAttributeNames = ['name 1', 'name 2', 'name 1'];
const propagationFields = ['traceparent'];

it('should remove duplicate message attribute names and add propagation fields', () => {
expect(
addPropagationFieldsToAttributeNames(
messageAttributeNames,
propagationFields
)
).toEqual(['name 1', 'name 2', 'traceparent']);
});

it('should return propagation fields if no message attribute names are set', () => {
expect(
addPropagationFieldsToAttributeNames(undefined, propagationFields)
).toEqual(['traceparent']);
});
});
});

0 comments on commit 4b4ded6

Please sign in to comment.