Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(aws-sdk): sns-sqs extract the correct context key from message payload #761

Merged
merged 17 commits into from
Nov 30, 2021
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