|
| 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