diff --git a/src/OpenTelemetry.Contrib.Instrumentation.AWS/Implementation/AWSTracingPipelineHandler.cs b/src/OpenTelemetry.Contrib.Instrumentation.AWS/Implementation/AWSTracingPipelineHandler.cs index 1cac4490f7..8bbb6f3435 100644 --- a/src/OpenTelemetry.Contrib.Instrumentation.AWS/Implementation/AWSTracingPipelineHandler.cs +++ b/src/OpenTelemetry.Contrib.Instrumentation.AWS/Implementation/AWSTracingPipelineHandler.cs @@ -202,13 +202,13 @@ private void AddRequestSpecificInformation(Activity activity, IRequestContext re } else if (AWSServiceType.IsSqsService(service)) { - var addAttributes = SqsRequestContextAdapter.CreateAddAttributesAction(requestContext); - addAttributes?.Invoke(AWSMessagingUtils.InjectIntoDictionary(new PropagationContext(activity.Context, Baggage.Current))); + SqsRequestContextHelper.AddAttributes( + requestContext, AWSMessagingUtils.InjectIntoDictionary(new PropagationContext(activity.Context, Baggage.Current))); } else if (AWSServiceType.IsSnsService(service)) { - var addAttributes = SnsRequestContextAdapter.CreateAddAttributesAction(requestContext); - addAttributes?.Invoke(AWSMessagingUtils.InjectIntoDictionary(new PropagationContext(activity.Context, Baggage.Current))); + SnsRequestContextHelper.AddAttributes( + requestContext, AWSMessagingUtils.InjectIntoDictionary(new PropagationContext(activity.Context, Baggage.Current))); } } diff --git a/src/OpenTelemetry.Contrib.Instrumentation.AWS/Implementation/SnsRequestContextAdapter.cs b/src/OpenTelemetry.Contrib.Instrumentation.AWS/Implementation/SnsRequestContextHelper.cs similarity index 56% rename from src/OpenTelemetry.Contrib.Instrumentation.AWS/Implementation/SnsRequestContextAdapter.cs rename to src/OpenTelemetry.Contrib.Instrumentation.AWS/Implementation/SnsRequestContextHelper.cs index cddb830340..5b8b09da87 100644 --- a/src/OpenTelemetry.Contrib.Instrumentation.AWS/Implementation/SnsRequestContextAdapter.cs +++ b/src/OpenTelemetry.Contrib.Instrumentation.AWS/Implementation/SnsRequestContextHelper.cs @@ -1,4 +1,4 @@ -// +// // Copyright The OpenTelemetry Authors // // Licensed under the Apache License, Version 2.0 (the "License"); @@ -14,7 +14,6 @@ // limitations under the License. // -using System; using System.Collections.Generic; using System.Linq; using Amazon.Runtime; @@ -22,31 +21,29 @@ using Amazon.SimpleNotificationService.Model; namespace OpenTelemetry.Contrib.Instrumentation.AWS.Implementation; -internal class SnsRequestContextAdapter +internal class SnsRequestContextHelper { // SQS/SNS message attributes collection size limit according to // https://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/sqs-message-metadata.html and // https://docs.aws.amazon.com/sns/latest/dg/sns-message-attributes.html private const int MaxMessageAttributes = 10; - private readonly ParameterCollection parameters; - private readonly PublishRequest originalRequest; - - private SnsRequestContextAdapter(ParameterCollection parameters, PublishRequest originalRequest) + internal static void AddAttributes(IRequestContext context, IReadOnlyDictionary attributes) { - this.parameters = parameters; - this.originalRequest = originalRequest; - } + var parameters = context.Request?.ParameterCollection; + var originalRequest = context.OriginalRequest as PublishRequest; + if (originalRequest?.MessageAttributes == null || parameters == null) + { + return; + } - public void AddAttributes(IReadOnlyDictionary attributes) - { - if (attributes.Keys.Any(k => this.ContainsAttribute(k))) + if (attributes.Keys.Any(k => originalRequest.MessageAttributes.ContainsKey(k))) { // If at least one attribute is already present in the request then we skip the injection. return; } - int attributesCount = this.originalRequest.MessageAttributes.Count; + int attributesCount = originalRequest.MessageAttributes.Count; if (attributes.Count + attributesCount > MaxMessageAttributes) { // TODO: add logging (event source). @@ -56,36 +53,20 @@ public void AddAttributes(IReadOnlyDictionary attributes) int nextAttributeIndex = attributesCount + 1; foreach (var param in attributes) { - this.AddAttribute(param.Key, param.Value, nextAttributeIndex); + AddAttribute(parameters, originalRequest, param.Key, param.Value, nextAttributeIndex); nextAttributeIndex++; } } - internal static Action>? CreateAddAttributesAction(IRequestContext context) + private static void AddAttribute(ParameterCollection parameters, PublishRequest originalRequest, string name, string value, int attributeIndex) { - var parameters = context.Request?.ParameterCollection; - var originalRequest = context.OriginalRequest as PublishRequest; - if (originalRequest?.MessageAttributes == null || parameters == null) - { - return null; - } - - var request = new SnsRequestContextAdapter(parameters, originalRequest); - return request.AddAttributes; - } - - private void AddAttribute(string name, string value, int nextAttributeIndex) - { - var prefix = "MessageAttributes.entry." + nextAttributeIndex; - this.parameters.Add(prefix + ".Name", name); - this.parameters.Add(prefix + ".Value.DataType", "String"); - this.parameters.Add(prefix + ".Value.StringValue", value); + var prefix = "MessageAttributes.entry." + attributeIndex; + parameters.Add(prefix + ".Name", name); + parameters.Add(prefix + ".Value.DataType", "String"); + parameters.Add(prefix + ".Value.StringValue", value); // Add injected attributes to the original request as well. // This dictionary must be in sync with parameters collection to pass through the MD5 hash matching check. - this.originalRequest.MessageAttributes.Add(name, new MessageAttributeValue { DataType = "String", StringValue = value }); + originalRequest.MessageAttributes.Add(name, new MessageAttributeValue { DataType = "String", StringValue = value }); } - - private bool ContainsAttribute(string name) - => this.originalRequest.MessageAttributes.ContainsKey(name); } diff --git a/src/OpenTelemetry.Contrib.Instrumentation.AWS/Implementation/SqsRequestContextAdapter.cs b/src/OpenTelemetry.Contrib.Instrumentation.AWS/Implementation/SqsRequestContextHelper.cs similarity index 57% rename from src/OpenTelemetry.Contrib.Instrumentation.AWS/Implementation/SqsRequestContextAdapter.cs rename to src/OpenTelemetry.Contrib.Instrumentation.AWS/Implementation/SqsRequestContextHelper.cs index 574160fa48..aad44cd023 100644 --- a/src/OpenTelemetry.Contrib.Instrumentation.AWS/Implementation/SqsRequestContextAdapter.cs +++ b/src/OpenTelemetry.Contrib.Instrumentation.AWS/Implementation/SqsRequestContextHelper.cs @@ -1,4 +1,4 @@ -// +// // Copyright The OpenTelemetry Authors // // Licensed under the Apache License, Version 2.0 (the "License"); @@ -14,7 +14,6 @@ // limitations under the License. // -using System; using System.Collections.Generic; using System.Linq; using Amazon.Runtime; @@ -22,31 +21,29 @@ using Amazon.SQS.Model; namespace OpenTelemetry.Contrib.Instrumentation.AWS.Implementation; -internal class SqsRequestContextAdapter +internal class SqsRequestContextHelper { // SQS/SNS message attributes collection size limit according to // https://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/sqs-message-metadata.html and // https://docs.aws.amazon.com/sns/latest/dg/sns-message-attributes.html private const int MaxMessageAttributes = 10; - private readonly ParameterCollection parameters; - private readonly SendMessageRequest originalRequest; - - private SqsRequestContextAdapter(ParameterCollection parameters, SendMessageRequest originalRequest) + internal static void AddAttributes(IRequestContext context, IReadOnlyDictionary attributes) { - this.parameters = parameters; - this.originalRequest = originalRequest; - } + var parameters = context.Request?.ParameterCollection; + var originalRequest = context.OriginalRequest as SendMessageRequest; + if (originalRequest?.MessageAttributes == null || parameters == null) + { + return; + } - public void AddAttributes(IReadOnlyDictionary attributes) - { - if (attributes.Keys.Any(k => this.ContainsAttribute(k))) + if (attributes.Keys.Any(k => originalRequest.MessageAttributes.ContainsKey(k))) { // If at least one attribute is already present in the request then we skip the injection. return; } - int attributesCount = this.originalRequest.MessageAttributes.Count; + int attributesCount = originalRequest.MessageAttributes.Count; if (attributes.Count + attributesCount > MaxMessageAttributes) { // TODO: add logging (event source). @@ -56,36 +53,20 @@ public void AddAttributes(IReadOnlyDictionary attributes) int nextAttributeIndex = attributesCount + 1; foreach (var param in attributes) { - this.AddAttribute(param.Key, param.Value, nextAttributeIndex); + AddAttribute(parameters, originalRequest, param.Key, param.Value, nextAttributeIndex); nextAttributeIndex++; } } - internal static Action>? CreateAddAttributesAction(IRequestContext context) - { - var parameters = context.Request?.ParameterCollection; - var originalRequest = context.OriginalRequest as SendMessageRequest; - if (originalRequest?.MessageAttributes == null || parameters == null) - { - return null; - } - - var request = new SqsRequestContextAdapter(parameters, originalRequest); - return request.AddAttributes; - } - - private void AddAttribute(string name, string value, int attributeIndex) + private static void AddAttribute(ParameterCollection parameters, SendMessageRequest originalRequest, string name, string value, int attributeIndex) { var prefix = "MessageAttribute." + attributeIndex; - this.parameters.Add(prefix + ".Name", name); - this.parameters.Add(prefix + ".Value.DataType", "String"); - this.parameters.Add(prefix + ".Value.StringValue", value); + parameters.Add(prefix + ".Name", name); + parameters.Add(prefix + ".Value.DataType", "String"); + parameters.Add(prefix + ".Value.StringValue", value); // Add injected attributes to the original request as well. // This dictionary must be in sync with parameters collection to pass through the MD5 hash matching check. - this.originalRequest.MessageAttributes.Add(name, new MessageAttributeValue { DataType = "String", StringValue = value }); + originalRequest.MessageAttributes.Add(name, new MessageAttributeValue { DataType = "String", StringValue = value }); } - - private bool ContainsAttribute(string name) => - this.originalRequest.MessageAttributes.ContainsKey(name); } diff --git a/test/OpenTelemetry.Contrib.Instrumentation.AWS.Tests/Implementation/AWSMessagingUtilsTests.cs b/test/OpenTelemetry.Contrib.Instrumentation.AWS.Tests/Implementation/RequestContextHelperTests.cs similarity index 85% rename from test/OpenTelemetry.Contrib.Instrumentation.AWS.Tests/Implementation/AWSMessagingUtilsTests.cs rename to test/OpenTelemetry.Contrib.Instrumentation.AWS.Tests/Implementation/RequestContextHelperTests.cs index 894a0c5fe8..41426d2a61 100644 --- a/test/OpenTelemetry.Contrib.Instrumentation.AWS.Tests/Implementation/AWSMessagingUtilsTests.cs +++ b/test/OpenTelemetry.Contrib.Instrumentation.AWS.Tests/Implementation/RequestContextHelperTests.cs @@ -1,4 +1,4 @@ -// +// // Copyright The OpenTelemetry Authors // // Licensed under the Apache License, Version 2.0 (the "License"); @@ -27,13 +27,13 @@ namespace OpenTelemetry.Contrib.Instrumentation.AWS.Tests.Implementation; -public class AWSMessagingUtilsTests +public class RequestContextHelperTests { private const string TraceId = "5759e988bd862e3fe1be46a994272793"; private const string ParentId = "53995c3f42cd8ad8"; private const string TraceState = "trace-state"; - public AWSMessagingUtilsTests() + public RequestContextHelperTests() { Sdk.CreateTracerProviderBuilder() .Build(); @@ -42,7 +42,7 @@ public AWSMessagingUtilsTests() [Theory] [InlineData(AWSServiceType.SQSService)] [InlineData(AWSServiceType.SNSService)] - public void InjectIntoDictionary_ParametersCollectionSizeReachesLimit_TraceDataNotInjected(string serviceType) + public void AddAttributes_ParametersCollectionSizeReachesLimit_TraceDataNotInjected(string serviceType) { AmazonWebServiceRequest originalRequest = TestsHelper.CreateOriginalRequest(serviceType, 10); var parameters = new ParameterCollection(); @@ -59,7 +59,7 @@ public void InjectIntoDictionary_ParametersCollectionSizeReachesLimit_TraceDataN .Returns(request.Object); var addAttributes = TestsHelper.CreateAddAttributesAction(serviceType, context.Object); - addAttributes?.Invoke(AWSMessagingUtils.InjectIntoDictionary(CreatePropagationContext())); + addAttributes?.Invoke(context.Object, AWSMessagingUtils.InjectIntoDictionary(CreatePropagationContext())); Assert.Equal(30, parameters.Count); } @@ -67,7 +67,7 @@ public void InjectIntoDictionary_ParametersCollectionSizeReachesLimit_TraceDataN [Theory] [InlineData(AWSServiceType.SQSService)] [InlineData(AWSServiceType.SNSService)] - public void InjectIntoDictionary_ParametersCollection_TraceDataInjected(string serviceType) + public void AddAttributes_ParametersCollection_TraceDataInjected(string serviceType) { var expectedParameters = new List>() { @@ -89,7 +89,7 @@ public void InjectIntoDictionary_ParametersCollection_TraceDataInjected(string s .Returns(request.Object); var addAttributes = TestsHelper.CreateAddAttributesAction(serviceType, context.Object); - addAttributes?.Invoke(AWSMessagingUtils.InjectIntoDictionary(CreatePropagationContext())); + addAttributes?.Invoke(context.Object, AWSMessagingUtils.InjectIntoDictionary(CreatePropagationContext())); TestsHelper.AssertStringParameters(serviceType, expectedParameters, parameters); } @@ -97,7 +97,7 @@ public void InjectIntoDictionary_ParametersCollection_TraceDataInjected(string s [Theory] [InlineData(AWSServiceType.SQSService)] [InlineData(AWSServiceType.SNSService)] - public void InjectIntoDictionary_ParametersCollectionWithCustomParameter_TraceDataInjected(string serviceType) + public void AddAttributes_ParametersCollectionWithCustomParameter_TraceDataInjected(string serviceType) { var expectedParameters = new List>() { @@ -121,7 +121,7 @@ public void InjectIntoDictionary_ParametersCollectionWithCustomParameter_TraceDa .Returns(request.Object); var addAttributes = TestsHelper.CreateAddAttributesAction(serviceType, context.Object); - addAttributes?.Invoke(AWSMessagingUtils.InjectIntoDictionary(CreatePropagationContext())); + addAttributes?.Invoke(context.Object, AWSMessagingUtils.InjectIntoDictionary(CreatePropagationContext())); TestsHelper.AssertStringParameters(serviceType, expectedParameters, parameters); } @@ -129,7 +129,7 @@ public void InjectIntoDictionary_ParametersCollectionWithCustomParameter_TraceDa [Theory] [InlineData(AWSServiceType.SQSService)] [InlineData(AWSServiceType.SNSService)] - public void InjectIntoDictionary_ParametersCollectionWithTraceParent_TraceStateNotInjected(string serviceType) + public void AddAttributes_ParametersCollectionWithTraceParent_TraceStateNotInjected(string serviceType) { // This test just checks the common implementation logic: // if at least one attribute is already present the whole injection is skipped. @@ -157,7 +157,7 @@ public void InjectIntoDictionary_ParametersCollectionWithTraceParent_TraceStateN .Returns(request.Object); var addAttributes = TestsHelper.CreateAddAttributesAction(serviceType, context.Object); - addAttributes?.Invoke(AWSMessagingUtils.InjectIntoDictionary(CreatePropagationContext())); + addAttributes?.Invoke(context.Object, AWSMessagingUtils.InjectIntoDictionary(CreatePropagationContext())); TestsHelper.AssertStringParameters(serviceType, expectedParameters, parameters); } diff --git a/test/OpenTelemetry.Contrib.Instrumentation.AWS.Tests/Implementation/TestsHelper.cs b/test/OpenTelemetry.Contrib.Instrumentation.AWS.Tests/Implementation/TestsHelper.cs index c55ec74ad9..d2d8107015 100644 --- a/test/OpenTelemetry.Contrib.Instrumentation.AWS.Tests/Implementation/TestsHelper.cs +++ b/test/OpenTelemetry.Contrib.Instrumentation.AWS.Tests/Implementation/TestsHelper.cs @@ -26,12 +26,12 @@ namespace OpenTelemetry.Contrib.Instrumentation.AWS.Tests.Implementation; internal static class TestsHelper { - internal static Action>? CreateAddAttributesAction(string serviceType, IRequestContext context) + internal static Action>? CreateAddAttributesAction(string serviceType, IRequestContext context) { return serviceType switch { - AWSServiceType.SQSService => SqsRequestContextAdapter.CreateAddAttributesAction(context), - AWSServiceType.SNSService => SnsRequestContextAdapter.CreateAddAttributesAction(context), + AWSServiceType.SQSService => SqsRequestContextHelper.AddAttributes, + AWSServiceType.SNSService => SnsRequestContextHelper.AddAttributes, _ => throw new NotSupportedException($"Tests for service type {serviceType} not supported."), }; }