From 577a291cd527606b331d6732ba0eccc75422a0fc Mon Sep 17 00:00:00 2001 From: Ben Vidulich Date: Sat, 13 Jan 2024 18:27:37 +1100 Subject: [PATCH] fix(instrumentation-aws-sdk): make empty context when SQS message has no propagation fields (#1889) * fix(instrumentation-aws-sdk): make empty context when SQS message has no propagation fields * style: lint fix --------- Co-authored-by: Amir Blum --- .../src/services/MessageAttributes.ts | 3 +++ .../test/MessageAttributes.test.ts | 26 +++++++++++++++++++ 2 files changed, 29 insertions(+) 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 a894d5ad86..1f7e6213c7 100644 --- a/plugins/node/opentelemetry-instrumentation-aws-sdk/src/services/MessageAttributes.ts +++ b/plugins/node/opentelemetry-instrumentation-aws-sdk/src/services/MessageAttributes.ts @@ -55,6 +55,9 @@ class ContextGetter keys( carrier: SQS.MessageBodyAttributeMap | SNS.MessageAttributeMap ): string[] { + if (carrier == null) { + return []; + } return Object.keys(carrier); } 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 51d63442f0..826fa3f15e 100644 --- a/plugins/node/opentelemetry-instrumentation-aws-sdk/test/MessageAttributes.test.ts +++ b/plugins/node/opentelemetry-instrumentation-aws-sdk/test/MessageAttributes.test.ts @@ -17,10 +17,12 @@ import { expect } from 'expect'; import { MAX_MESSAGE_ATTRIBUTES, + contextGetter, contextSetter, injectPropagationContext, addPropagationFieldsToAttributeNames, } from '../src/services/MessageAttributes'; +import { SNS, SQS } from '../src/aws-sdk.types'; describe('MessageAttributes', () => { describe('MAX_MESSAGE_ATTRIBUTES', () => { @@ -29,6 +31,30 @@ describe('MessageAttributes', () => { }); }); + describe('contextGetter', () => { + it('returns context keys if there are available attributes', () => { + const contextCarrier = { + key1: { DataType: 'String', StringValue: 'value1' }, + }; + const expectedKeys = ['key1']; + + expect(contextGetter.keys(contextCarrier)).toEqual(expectedKeys); + }); + + it('returns empty context keys if there are no available attributes', () => { + const contextCarrier = undefined; + const expectedKeys: string[] = []; + + expect( + contextGetter.keys( + contextCarrier as unknown as + | SQS.MessageBodyAttributeMap + | SNS.MessageAttributeMap + ) + ).toEqual(expectedKeys); + }); + }); + describe('contextSetter', () => { it('should set parent context in sqs receive callback', () => { const contextKey = 'key';