Skip to content

Commit 297effc

Browse files
committed
#1034: aligned naming + unit tests for SetParentFromMessageBatch option
1 parent 63e7445 commit 297effc

3 files changed

Lines changed: 82 additions & 3 deletions

File tree

src/OpenTelemetry.Instrumentation.AWSLambda/.publicApi/netstandard2.0/PublicAPI.Unshipped.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ OpenTelemetry.Instrumentation.AWSLambda.AWSLambdaInstrumentationOptions
22
OpenTelemetry.Instrumentation.AWSLambda.AWSLambdaInstrumentationOptions.AWSLambdaInstrumentationOptions() -> void
33
OpenTelemetry.Instrumentation.AWSLambda.AWSLambdaInstrumentationOptions.DisableAwsXRayContextExtraction.get -> bool
44
OpenTelemetry.Instrumentation.AWSLambda.AWSLambdaInstrumentationOptions.DisableAwsXRayContextExtraction.set -> void
5-
OpenTelemetry.Instrumentation.AWSLambda.AWSLambdaInstrumentationOptions.SetParentFromMessageBatch.get -> bool
6-
OpenTelemetry.Instrumentation.AWSLambda.AWSLambdaInstrumentationOptions.SetParentFromMessageBatch.set -> void
5+
OpenTelemetry.Instrumentation.AWSLambda.AWSLambdaInstrumentationOptions.SetParentFromBatch.get -> bool
6+
OpenTelemetry.Instrumentation.AWSLambda.AWSLambdaInstrumentationOptions.SetParentFromBatch.set -> void
77
OpenTelemetry.Instrumentation.AWSLambda.AWSLambdaWrapper
88
OpenTelemetry.Instrumentation.AWSLambda.TracerProviderBuilderExtensions
99
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

src/OpenTelemetry.Instrumentation.AWSLambda/TracerProviderBuilderExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public static TracerProviderBuilder AddAWSLambdaConfigurations(
5151
configure?.Invoke(options);
5252

5353
AWSLambdaWrapper.DisableAwsXRayContextExtraction = options.DisableAwsXRayContextExtraction;
54-
AWSMessagingUtils.SetParentFromMessageBatch = options.SetParentFromMessageBatch;
54+
AWSMessagingUtils.SetParentFromMessageBatch = options.SetParentFromBatch;
5555

5656
builder.AddSource(AWSLambdaWrapper.ActivitySourceName);
5757
builder.SetResourceBuilder(ResourceBuilder
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
// <copyright file="AWSMessagingUtilsTests.cs" company="OpenTelemetry Authors">
2+
// Copyright The OpenTelemetry Authors
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
// </copyright>
16+
17+
using System.Collections.Generic;
18+
using System.Diagnostics;
19+
using System.Linq;
20+
using Amazon.Lambda.SQSEvents;
21+
using OpenTelemetry.Context.Propagation;
22+
using OpenTelemetry.Instrumentation.AWSLambda.Implementation;
23+
using OpenTelemetry.Trace;
24+
using Xunit;
25+
using static Amazon.Lambda.SQSEvents.SQSEvent;
26+
27+
namespace OpenTelemetry.Instrumentation.AWSLambda.Tests.Implementation;
28+
29+
public class AWSMessagingUtilsTests
30+
{
31+
private const string TraceId = "0af7651916cd43dd8448eb211c80319c";
32+
private const string SpanId1 = "b9c7c989f97918e1";
33+
private const string SpanId2 = "b9c7c989f97918e2";
34+
35+
public AWSMessagingUtilsTests()
36+
{
37+
var tracerProvider = Sdk.CreateTracerProviderBuilder()
38+
.Build();
39+
}
40+
41+
[Fact]
42+
public void ExtractParentContext_SetParentFromMessageBatchIsDisabled_ParentIsNotSet()
43+
{
44+
AWSMessagingUtils.SetParentFromMessageBatch = false;
45+
var @event = CreateSqsEventWithMessages(new[] { SpanId1, SpanId2 });
46+
47+
(PropagationContext parentContext, IEnumerable<ActivityLink> links) = AWSMessagingUtils.ExtractParentContext(@event);
48+
49+
Assert.Equal(default, parentContext);
50+
Assert.Equal(2, links.Count());
51+
}
52+
53+
[Fact]
54+
public void ExtractParentContext_SetParentFromMessageBatchIsEnabled_ParentIsSetFromLastMessage()
55+
{
56+
AWSMessagingUtils.SetParentFromMessageBatch = true;
57+
var @event = CreateSqsEventWithMessages(new[] { SpanId1, SpanId2 });
58+
59+
(PropagationContext parentContext, IEnumerable<ActivityLink> links) = AWSMessagingUtils.ExtractParentContext(@event);
60+
61+
Assert.NotEqual(default, parentContext);
62+
Assert.Equal(SpanId2, parentContext.ActivityContext.SpanId.ToHexString());
63+
Assert.Equal(2, links.Count());
64+
}
65+
66+
private static SQSEvent CreateSqsEventWithMessages(string[] spans)
67+
{
68+
var @event = new SQSEvent { Records = new List<SQSMessage>() };
69+
for (var i = 0; i < spans.Length; i++)
70+
{
71+
var message = new SQSMessage { MessageAttributes = new Dictionary<string, MessageAttribute>() };
72+
message.MessageAttributes.Add("traceparent", new MessageAttribute { StringValue = $"00-{TraceId}-{spans[i]}-01" });
73+
message.MessageAttributes.Add("tracestate", new MessageAttribute { StringValue = $"k1=v1,k2=v2" });
74+
@event.Records.Add(message);
75+
}
76+
77+
return @event;
78+
}
79+
}

0 commit comments

Comments
 (0)