From 297effc66f8e81d91069ff8fb11425ed1bd903f9 Mon Sep 17 00:00:00 2001 From: Oleksiy Dubinin Date: Thu, 13 Apr 2023 16:18:41 +0200 Subject: [PATCH] #1034: aligned naming + unit tests for SetParentFromMessageBatch option --- .../netstandard2.0/PublicAPI.Unshipped.txt | 4 +- .../TracerProviderBuilderExtensions.cs | 2 +- .../Implementation/AWSMessagingUtilsTests.cs | 79 +++++++++++++++++++ 3 files changed, 82 insertions(+), 3 deletions(-) create mode 100644 test/OpenTelemetry.Instrumentation.AWSLambda.Tests/Implementation/AWSMessagingUtilsTests.cs diff --git a/src/OpenTelemetry.Instrumentation.AWSLambda/.publicApi/netstandard2.0/PublicAPI.Unshipped.txt b/src/OpenTelemetry.Instrumentation.AWSLambda/.publicApi/netstandard2.0/PublicAPI.Unshipped.txt index 53f2ba0765..d5ed4319ec 100644 --- a/src/OpenTelemetry.Instrumentation.AWSLambda/.publicApi/netstandard2.0/PublicAPI.Unshipped.txt +++ b/src/OpenTelemetry.Instrumentation.AWSLambda/.publicApi/netstandard2.0/PublicAPI.Unshipped.txt @@ -2,8 +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.AWSLambdaInstrumentationOptions.SetParentFromBatch.get -> bool +OpenTelemetry.Instrumentation.AWSLambda.AWSLambdaInstrumentationOptions.SetParentFromBatch.set -> void OpenTelemetry.Instrumentation.AWSLambda.AWSLambdaWrapper OpenTelemetry.Instrumentation.AWSLambda.TracerProviderBuilderExtensions static OpenTelemetry.Instrumentation.AWSLambda.AWSLambdaWrapper.Trace(OpenTelemetry.Trace.TracerProvider tracerProvider, System.Func lambdaHandler, TInput input, Amazon.Lambda.Core.ILambdaContext context, System.Diagnostics.ActivityContext parentContext = default(System.Diagnostics.ActivityContext)) -> TResult diff --git a/src/OpenTelemetry.Instrumentation.AWSLambda/TracerProviderBuilderExtensions.cs b/src/OpenTelemetry.Instrumentation.AWSLambda/TracerProviderBuilderExtensions.cs index dbc17b26d8..e543c9dc94 100644 --- a/src/OpenTelemetry.Instrumentation.AWSLambda/TracerProviderBuilderExtensions.cs +++ b/src/OpenTelemetry.Instrumentation.AWSLambda/TracerProviderBuilderExtensions.cs @@ -51,7 +51,7 @@ public static TracerProviderBuilder AddAWSLambdaConfigurations( configure?.Invoke(options); AWSLambdaWrapper.DisableAwsXRayContextExtraction = options.DisableAwsXRayContextExtraction; - AWSMessagingUtils.SetParentFromMessageBatch = options.SetParentFromMessageBatch; + AWSMessagingUtils.SetParentFromMessageBatch = options.SetParentFromBatch; builder.AddSource(AWSLambdaWrapper.ActivitySourceName); builder.SetResourceBuilder(ResourceBuilder diff --git a/test/OpenTelemetry.Instrumentation.AWSLambda.Tests/Implementation/AWSMessagingUtilsTests.cs b/test/OpenTelemetry.Instrumentation.AWSLambda.Tests/Implementation/AWSMessagingUtilsTests.cs new file mode 100644 index 0000000000..fda5c9136c --- /dev/null +++ b/test/OpenTelemetry.Instrumentation.AWSLambda.Tests/Implementation/AWSMessagingUtilsTests.cs @@ -0,0 +1,79 @@ +// +// Copyright The OpenTelemetry Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +using System.Collections.Generic; +using System.Diagnostics; +using System.Linq; +using Amazon.Lambda.SQSEvents; +using OpenTelemetry.Context.Propagation; +using OpenTelemetry.Instrumentation.AWSLambda.Implementation; +using OpenTelemetry.Trace; +using Xunit; +using static Amazon.Lambda.SQSEvents.SQSEvent; + +namespace OpenTelemetry.Instrumentation.AWSLambda.Tests.Implementation; + +public class AWSMessagingUtilsTests +{ + private const string TraceId = "0af7651916cd43dd8448eb211c80319c"; + private const string SpanId1 = "b9c7c989f97918e1"; + private const string SpanId2 = "b9c7c989f97918e2"; + + public AWSMessagingUtilsTests() + { + var tracerProvider = Sdk.CreateTracerProviderBuilder() + .Build(); + } + + [Fact] + public void ExtractParentContext_SetParentFromMessageBatchIsDisabled_ParentIsNotSet() + { + AWSMessagingUtils.SetParentFromMessageBatch = false; + var @event = CreateSqsEventWithMessages(new[] { SpanId1, SpanId2 }); + + (PropagationContext parentContext, IEnumerable links) = AWSMessagingUtils.ExtractParentContext(@event); + + Assert.Equal(default, parentContext); + Assert.Equal(2, links.Count()); + } + + [Fact] + public void ExtractParentContext_SetParentFromMessageBatchIsEnabled_ParentIsSetFromLastMessage() + { + AWSMessagingUtils.SetParentFromMessageBatch = true; + var @event = CreateSqsEventWithMessages(new[] { SpanId1, SpanId2 }); + + (PropagationContext parentContext, IEnumerable links) = AWSMessagingUtils.ExtractParentContext(@event); + + Assert.NotEqual(default, parentContext); + Assert.Equal(SpanId2, parentContext.ActivityContext.SpanId.ToHexString()); + Assert.Equal(2, links.Count()); + } + + private static SQSEvent CreateSqsEventWithMessages(string[] spans) + { + var @event = new SQSEvent { Records = new List() }; + for (var i = 0; i < spans.Length; i++) + { + var message = new SQSMessage { MessageAttributes = new Dictionary() }; + message.MessageAttributes.Add("traceparent", new MessageAttribute { StringValue = $"00-{TraceId}-{spans[i]}-01" }); + message.MessageAttributes.Add("tracestate", new MessageAttribute { StringValue = $"k1=v1,k2=v2" }); + @event.Records.Add(message); + } + + return @event; + } +}