From 8bdad4d601f92231943037e0b4128cae5202edc1 Mon Sep 17 00:00:00 2001 From: Oleksiy Dubinin Date: Thu, 2 Mar 2023 10:53:08 +0100 Subject: [PATCH] #1034: swap the order of parent context check for the SNS in SQS case --- .../Implementation/AWSMessagingUtils.cs | 22 +++++++++++++------ 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/src/OpenTelemetry.Instrumentation.AWSLambda/Implementation/AWSMessagingUtils.cs b/src/OpenTelemetry.Instrumentation.AWSLambda/Implementation/AWSMessagingUtils.cs index f9663b9f88..9ba1f1561f 100644 --- a/src/OpenTelemetry.Instrumentation.AWSLambda/Implementation/AWSMessagingUtils.cs +++ b/src/OpenTelemetry.Instrumentation.AWSLambda/Implementation/AWSMessagingUtils.cs @@ -14,6 +14,7 @@ // limitations under the License. // +using System; using System.Collections.Generic; using System.Linq; using Amazon.Lambda.SNSEvents; @@ -49,12 +50,19 @@ internal static PropagationContext ExtractParentContext(SQSEvent.SQSMessage sqsM return default; } - // SQS subscribed to SNS topic with raw delivery disabled case, i.e. SNS record serialized into SQS body. - // https://docs.aws.amazon.com/sns/latest/dg/sns-large-payload-raw-message-delivery.html - SNSEvent.SNSMessage snsMessage = GetSnsMessage(sqsMessage); - return snsMessage != null ? - ExtractParentContext(snsMessage) : - Propagators.DefaultTextMapPropagator.Extract(default, sqsMessage.MessageAttributes, SqsMessageAttributeGetter); + var parentContext = Propagators.DefaultTextMapPropagator.Extract(default, sqsMessage.MessageAttributes, SqsMessageAttributeGetter); + if (parentContext == default) + { + // SQS subscribed to SNS topic with raw delivery disabled case, i.e. SNS record serialized into SQS body. + // https://docs.aws.amazon.com/sns/latest/dg/sns-large-payload-raw-message-delivery.html + SNSEvent.SNSMessage snsMessage = GetSnsMessage(sqsMessage); + if (snsMessage != null) + { + parentContext = ExtractParentContext(snsMessage); + } + } + + return parentContext; } internal static PropagationContext ExtractParentContext(SNSEvent snsEvent) @@ -129,7 +137,7 @@ private static SNSEvent.SNSMessage GetSnsMessage(SQSEvent.SQSMessage sqsMessage) { snsMessage = JsonConvert.DeserializeObject(body); } - catch (JsonException) + catch (Exception) { // TODO: log exception. return null;