Skip to content

Commit

Permalink
#1034: added option for configuring setting parent from SQS messages …
Browse files Browse the repository at this point in the history
…batch
  • Loading branch information
rypdal committed Apr 12, 2023
1 parent 24ae92d commit d2613c4
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ OpenTelemetry.Instrumentation.AWSLambda.AWSLambdaInstrumentationOptions
OpenTelemetry.Instrumentation.AWSLambda.AWSLambdaInstrumentationOptions.AWSLambdaInstrumentationOptions() -> void
OpenTelemetry.Instrumentation.AWSLambda.AWSLambdaInstrumentationOptions.DisableAwsXRayContextExtraction.get -> bool
OpenTelemetry.Instrumentation.AWSLambda.AWSLambdaInstrumentationOptions.DisableAwsXRayContextExtraction.set -> void
OpenTelemetry.Instrumentation.AWSLambda.AWSLambdaInstrumentationOptions.SetParentFromMessageBatch.get -> bool
OpenTelemetry.Instrumentation.AWSLambda.AWSLambdaInstrumentationOptions.SetParentFromMessageBatch.set -> void
OpenTelemetry.Instrumentation.AWSLambda.AWSLambdaWrapper
OpenTelemetry.Instrumentation.AWSLambda.TracerProviderBuilderExtensions
static OpenTelemetry.Instrumentation.AWSLambda.AWSLambdaWrapper.Trace<TInput, TResult>(OpenTelemetry.Trace.TracerProvider tracerProvider, System.Func<TInput, Amazon.Lambda.Core.ILambdaContext, TResult> lambdaHandler, TInput input, Amazon.Lambda.Core.ILambdaContext context, System.Diagnostics.ActivityContext parentContext = default(System.Diagnostics.ActivityContext)) -> TResult
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,10 @@ public class AWSLambdaInstrumentationOptions
/// Gets or sets a value indicating whether AWS X-Ray context extraction should be disabled.
/// </summary>
public bool DisableAwsXRayContextExtraction { get; set; }

/// <summary>
/// Gets or sets a value indicating whether the parent Activity should be set when SQS message batch is received.
/// If option is set to true then the parent is set using the last received message otherwise the parent is not set at all.
/// </summary>
public bool SetParentFromMessageBatch { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,22 @@ internal class AWSMessagingUtils
private const string SnsAttributeTypeStringArray = "String.Array";
private const string SnsMessageAttributes = "MessageAttributes";

/// <summary>
/// Gets or sets a value indicating whether the parent Activity should be set when SQS message batch is received.
/// If option is set to true then the parent is set using the last received message otherwise the parent is not set at all.
/// </summary>
internal static bool SetParentFromMessageBatch { get; set; }

internal static (PropagationContext ParentContext, IEnumerable<ActivityLink> Links) ExtractParentContext(SQSEvent sqsEvent)
{
if (sqsEvent?.Records == null)
{
return (default, null);
}

// We assume there can be only one parent that's why we consider only a single (the last) record as the carrier.
var parentRecord = sqsEvent.Records.LastOrDefault();
var parentContext = ExtractParentContext(parentRecord);
// We choose the last message (record) as the carrier to set the parent.
var parentRecord = SetParentFromMessageBatch ? sqsEvent.Records.LastOrDefault() : null;
var parentContext = (parentRecord != null) ? ExtractParentContext(parentRecord) : default;

var links = new List<ActivityLink>();
foreach (var record in sqsEvent.Records)
Expand Down Expand Up @@ -80,7 +86,8 @@ internal static PropagationContext ExtractParentContext(SQSEvent.SQSMessage sqsM

internal static PropagationContext ExtractParentContext(SNSEvent snsEvent)
{
// We assume there can be only one parent that's why we consider only a single (the last) record as the carrier.
// We assume there can be only a single SNS record (message) and records list is kept in the model consistency.
// See https://aws.amazon.com/sns/faqs/ for details.
var record = snsEvent?.Records?.LastOrDefault();
return ExtractParentContext(record);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public static TracerProviderBuilder AddAWSLambdaConfigurations(
configure?.Invoke(options);

AWSLambdaWrapper.DisableAwsXRayContextExtraction = options.DisableAwsXRayContextExtraction;
AWSMessagingUtils.SetParentFromMessageBatch = options.SetParentFromMessageBatch;

builder.AddSource(AWSLambdaWrapper.ActivitySourceName);
builder.SetResourceBuilder(ResourceBuilder
Expand Down

0 comments on commit d2613c4

Please sign in to comment.