Skip to content

Commit

Permalink
fix(aws-sdk): sns-sqs extract the correct context key from message pa…
Browse files Browse the repository at this point in the history
…yload (#761)
  • Loading branch information
habmic authored Nov 30, 2021
1 parent 0463f27 commit e5cae76
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import {
diag,
} from '@opentelemetry/api';
import type { SQS, SNS } from 'aws-sdk';
import type { MessageBodyAttributeMap } from 'aws-sdk/clients/sqs';

// https://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/sqs-quotas.html
export const MAX_MESSAGE_ATTRIBUTES = 10;
Expand All @@ -42,6 +41,13 @@ class ContextSetter
}
export const contextSetter = new ContextSetter();

export interface AwsSdkContextObject {
[key: string]: {
StringValue?: string;
Value?: string;
};
}

class ContextGetter
implements
TextMapGetter<SQS.MessageBodyAttributeMap | SNS.MessageAttributeMap>
Expand All @@ -53,10 +59,10 @@ class ContextGetter
}

get(
carrier: SQS.MessageBodyAttributeMap | SNS.MessageAttributeMap,
carrier: AwsSdkContextObject,
key: string
): undefined | string | string[] {
return carrier?.[key]?.StringValue;
return carrier?.[key]?.StringValue || carrier?.[key]?.Value;
}
}
export const contextGetter = new ContextGetter();
Expand All @@ -78,7 +84,7 @@ export const injectPropagationContext = (
export const extractPropagationContext = (
message: SQS.Message,
sqsExtractContextPropagationFromPayload: boolean | undefined
): MessageBodyAttributeMap | undefined => {
): AwsSdkContextObject | undefined => {
const propagationFields = propagation.fields();
const hasPropagationFields = Object.keys(
message.MessageAttributes || []
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -458,9 +458,8 @@ describe('SQS', () => {
it('should extract from payload', async () => {
const traceparent = {
traceparent: {
StringValue:
'00-a1d050b7c8ad93c405e7a0d94cda5b03-23a485dc98b24027-01',
DataType: 'String',
Value: '00-a1d050b7c8ad93c405e7a0d94cda5b03-23a485dc98b24027-01',
Type: 'String',
},
};
instrumentation.setConfig({
Expand All @@ -478,7 +477,6 @@ describe('SQS', () => {
QueueUrl: 'queue/url/for/unittests',
})
.promise();

expect(extractContextSpy.returnValues[0]?.traceparent).toStrictEqual(
traceparent
);
Expand Down

0 comments on commit e5cae76

Please sign in to comment.