Skip to content

Commit

Permalink
#1034: swap the order of parent context check for the SNS in SQS case
Browse files Browse the repository at this point in the history
  • Loading branch information
rypdal committed Mar 2, 2023
1 parent 222f782 commit 8bdad4d
Showing 1 changed file with 15 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
// limitations under the License.
// </copyright>

using System;
using System.Collections.Generic;
using System.Linq;
using Amazon.Lambda.SNSEvents;
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -129,7 +137,7 @@ private static SNSEvent.SNSMessage GetSnsMessage(SQSEvent.SQSMessage sqsMessage)
{
snsMessage = JsonConvert.DeserializeObject<SNSEvent.SNSMessage>(body);
}
catch (JsonException)
catch (Exception)
{
// TODO: log exception.
return null;
Expand Down

0 comments on commit 8bdad4d

Please sign in to comment.