diff --git a/src/OpenTelemetry.Instrumentation.AWSLambda/.publicApi/netstandard2.0/PublicAPI.Shipped.txt b/src/OpenTelemetry.Instrumentation.AWSLambda/.publicApi/netstandard2.0/PublicAPI.Shipped.txt index e69de29bb2..7dc5c58110 100644 --- a/src/OpenTelemetry.Instrumentation.AWSLambda/.publicApi/netstandard2.0/PublicAPI.Shipped.txt +++ b/src/OpenTelemetry.Instrumentation.AWSLambda/.publicApi/netstandard2.0/PublicAPI.Shipped.txt @@ -0,0 +1 @@ +#nullable enable 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 d5ed4319ec..8b96f675ae 100644 --- a/src/OpenTelemetry.Instrumentation.AWSLambda/.publicApi/netstandard2.0/PublicAPI.Unshipped.txt +++ b/src/OpenTelemetry.Instrumentation.AWSLambda/.publicApi/netstandard2.0/PublicAPI.Unshipped.txt @@ -6,9 +6,9 @@ OpenTelemetry.Instrumentation.AWSLambda.AWSLambdaInstrumentationOptions.SetParen 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 -static OpenTelemetry.Instrumentation.AWSLambda.AWSLambdaWrapper.Trace(OpenTelemetry.Trace.TracerProvider tracerProvider, System.Action lambdaHandler, TInput input, Amazon.Lambda.Core.ILambdaContext context, System.Diagnostics.ActivityContext parentContext = default(System.Diagnostics.ActivityContext)) -> void -static OpenTelemetry.Instrumentation.AWSLambda.AWSLambdaWrapper.TraceAsync(OpenTelemetry.Trace.TracerProvider tracerProvider, System.Func> lambdaHandler, TInput input, Amazon.Lambda.Core.ILambdaContext context, System.Diagnostics.ActivityContext parentContext = default(System.Diagnostics.ActivityContext)) -> System.Threading.Tasks.Task -static OpenTelemetry.Instrumentation.AWSLambda.AWSLambdaWrapper.TraceAsync(OpenTelemetry.Trace.TracerProvider tracerProvider, System.Func lambdaHandler, TInput input, Amazon.Lambda.Core.ILambdaContext context, System.Diagnostics.ActivityContext parentContext = default(System.Diagnostics.ActivityContext)) -> System.Threading.Tasks.Task -static OpenTelemetry.Instrumentation.AWSLambda.TracerProviderBuilderExtensions.AddAWSLambdaConfigurations(this OpenTelemetry.Trace.TracerProviderBuilder builder) -> OpenTelemetry.Trace.TracerProviderBuilder -static OpenTelemetry.Instrumentation.AWSLambda.TracerProviderBuilderExtensions.AddAWSLambdaConfigurations(this OpenTelemetry.Trace.TracerProviderBuilder builder, System.Action configure) -> OpenTelemetry.Trace.TracerProviderBuilder +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 +static OpenTelemetry.Instrumentation.AWSLambda.AWSLambdaWrapper.Trace(OpenTelemetry.Trace.TracerProvider? tracerProvider, System.Action! lambdaHandler, TInput input, Amazon.Lambda.Core.ILambdaContext! context, System.Diagnostics.ActivityContext parentContext = default(System.Diagnostics.ActivityContext)) -> void +static OpenTelemetry.Instrumentation.AWSLambda.AWSLambdaWrapper.TraceAsync(OpenTelemetry.Trace.TracerProvider? tracerProvider, System.Func!>! lambdaHandler, TInput input, Amazon.Lambda.Core.ILambdaContext! context, System.Diagnostics.ActivityContext parentContext = default(System.Diagnostics.ActivityContext)) -> System.Threading.Tasks.Task! +static OpenTelemetry.Instrumentation.AWSLambda.AWSLambdaWrapper.TraceAsync(OpenTelemetry.Trace.TracerProvider? tracerProvider, System.Func! lambdaHandler, TInput input, Amazon.Lambda.Core.ILambdaContext! context, System.Diagnostics.ActivityContext parentContext = default(System.Diagnostics.ActivityContext)) -> System.Threading.Tasks.Task! +static OpenTelemetry.Instrumentation.AWSLambda.TracerProviderBuilderExtensions.AddAWSLambdaConfigurations(this OpenTelemetry.Trace.TracerProviderBuilder! builder) -> OpenTelemetry.Trace.TracerProviderBuilder! +static OpenTelemetry.Instrumentation.AWSLambda.TracerProviderBuilderExtensions.AddAWSLambdaConfigurations(this OpenTelemetry.Trace.TracerProviderBuilder! builder, System.Action? configure) -> OpenTelemetry.Trace.TracerProviderBuilder! diff --git a/src/OpenTelemetry.Instrumentation.AWSLambda/AWSLambdaWrapper.cs b/src/OpenTelemetry.Instrumentation.AWSLambda/AWSLambdaWrapper.cs index 937ad3f9aa..ea88b102d2 100644 --- a/src/OpenTelemetry.Instrumentation.AWSLambda/AWSLambdaWrapper.cs +++ b/src/OpenTelemetry.Instrumentation.AWSLambda/AWSLambdaWrapper.cs @@ -65,7 +65,7 @@ public static class AWSLambdaWrapper /// /// Instance of output result. public static TResult Trace( - TracerProvider tracerProvider, + TracerProvider? tracerProvider, Func lambdaHandler, TInput input, ILambdaContext context, @@ -90,7 +90,7 @@ public static TResult Trace( /// unless X-Ray propagation is disabled in the configuration for this wrapper. /// public static void Trace( - TracerProvider tracerProvider, + TracerProvider? tracerProvider, Action lambdaHandler, TInput input, ILambdaContext context, @@ -98,7 +98,7 @@ public static void Trace( { Guard.ThrowIfNull(lambdaHandler); - object Handler(TInput input, ILambdaContext context) + object? Handler(TInput input, ILambdaContext context) { lambdaHandler(input, context); return null; @@ -123,7 +123,7 @@ object Handler(TInput input, ILambdaContext context) /// /// Task. public static Task TraceAsync( - TracerProvider tracerProvider, + TracerProvider? tracerProvider, Func lambdaHandler, TInput input, ILambdaContext context, @@ -131,7 +131,7 @@ public static Task TraceAsync( { Guard.ThrowIfNull(lambdaHandler); - async Task Handler(TInput input, ILambdaContext context) + async Task Handler(TInput input, ILambdaContext context) { await lambdaHandler(input, context).ConfigureAwait(false); return null; @@ -157,7 +157,7 @@ async Task Handler(TInput input, ILambdaContext context) /// /// Task of result. public static Task TraceAsync( - TracerProvider tracerProvider, + TracerProvider? tracerProvider, Func> lambdaHandler, TInput input, ILambdaContext context, @@ -169,9 +169,9 @@ public static Task TraceAsync( #pragma warning restore RS0026 // Do not add multiple public overloads with optional parameters - internal static Activity OnFunctionStart(TInput input, ILambdaContext context, ActivityContext parentContext = default) + internal static Activity? OnFunctionStart(TInput input, ILambdaContext context, ActivityContext parentContext = default) { - IEnumerable links = null; + IEnumerable? links = null; if (parentContext == default) { (parentContext, links) = AWSLambdaUtils.ExtractParentContext(input); @@ -181,8 +181,8 @@ internal static Activity OnFunctionStart(TInput input, ILambdaContext co } } - var functionTags = AWSLambdaUtils.GetFunctionTags(input, context); - var httpTags = AWSLambdaHttpUtils.GetHttpTags(input); + var functionTags = (IEnumerable>)AWSLambdaUtils.GetFunctionTags(input, context); + var httpTags = (IEnumerable>)AWSLambdaHttpUtils.GetHttpTags(input); // We assume that functionTags and httpTags have no intersection. var activityName = AWSLambdaUtils.GetFunctionName(context) ?? "AWS Lambda Invoke"; @@ -191,7 +191,7 @@ internal static Activity OnFunctionStart(TInput input, ILambdaContext co return activity; } - private static void OnFunctionStop(Activity activity, TracerProvider tracerProvider) + private static void OnFunctionStop(Activity? activity, TracerProvider? tracerProvider) { activity?.Stop(); @@ -199,7 +199,7 @@ private static void OnFunctionStop(Activity activity, TracerProvider tracerProvi tracerProvider?.ForceFlush(); } - private static void OnException(Activity activity, Exception exception) + private static void OnException(Activity? activity, Exception exception) { if (activity != null) { @@ -212,7 +212,7 @@ private static void OnException(Activity activity, Exception exception) } private static TResult TraceInternal( - TracerProvider tracerProvider, + TracerProvider? tracerProvider, Func handler, TInput input, ILambdaContext context, @@ -238,7 +238,7 @@ private static TResult TraceInternal( } private static async Task TraceInternalAsync( - TracerProvider tracerProvider, + TracerProvider? tracerProvider, Func> handlerAsync, TInput input, ILambdaContext context, diff --git a/src/OpenTelemetry.Instrumentation.AWSLambda/Implementation/AWSLambdaHttpUtils.cs b/src/OpenTelemetry.Instrumentation.AWSLambda/Implementation/AWSLambdaHttpUtils.cs index 2e2d7747e4..1b4d1b3ef8 100644 --- a/src/OpenTelemetry.Instrumentation.AWSLambda/Implementation/AWSLambdaHttpUtils.cs +++ b/src/OpenTelemetry.Instrumentation.AWSLambda/Implementation/AWSLambdaHttpUtils.cs @@ -34,10 +34,10 @@ internal static IEnumerable> GetHttpTags(TI { var tags = new List>(); - string httpScheme = null; - string httpTarget = null; - string httpMethod = null; - string hostName = null; + string? httpScheme = null; + string? httpTarget = null; + string? httpMethod = null; + string? hostName = null; int? hostPort = null; switch (input) @@ -68,8 +68,13 @@ internal static IEnumerable> GetHttpTags(TI return tags; } - internal static void SetHttpTagsFromResult(Activity activity, object result) + internal static void SetHttpTagsFromResult(Activity? activity, object? result) { + if (activity == null || result == null) + { + return; + } + switch (result) { case APIGatewayProxyResponse response: @@ -81,7 +86,7 @@ internal static void SetHttpTagsFromResult(Activity activity, object result) } } - internal static string GetQueryString(APIGatewayProxyRequest request) + internal static string? GetQueryString(APIGatewayProxyRequest request) { if (request.MultiValueQueryStringParameters == null) { @@ -107,10 +112,10 @@ internal static string GetQueryString(APIGatewayProxyRequest request) return queryString.ToString(); } - internal static string GetQueryString(APIGatewayHttpApiV2ProxyRequest request) => + internal static string? GetQueryString(APIGatewayHttpApiV2ProxyRequest request) => string.IsNullOrEmpty(request.RawQueryString) ? string.Empty : "?" + request.RawQueryString; - internal static (string Host, int? Port) GetHostAndPort(string httpScheme, string hostHeader) + internal static (string? Host, int? Port) GetHostAndPort(string? httpScheme, string? hostHeader) { if (hostHeader == null) { @@ -131,6 +136,6 @@ internal static (string Host, int? Port) GetHostAndPort(string httpScheme, strin } } - private static int? GetDefaultPort(string httpScheme) => + private static int? GetDefaultPort(string? httpScheme) => httpScheme == "https" ? 443 : httpScheme == "http" ? 80 : null; } diff --git a/src/OpenTelemetry.Instrumentation.AWSLambda/Implementation/AWSLambdaUtils.cs b/src/OpenTelemetry.Instrumentation.AWSLambda/Implementation/AWSLambdaUtils.cs index d9bebaf5f2..f9579444ba 100644 --- a/src/OpenTelemetry.Instrumentation.AWSLambda/Implementation/AWSLambdaUtils.cs +++ b/src/OpenTelemetry.Instrumentation.AWSLambda/Implementation/AWSLambdaUtils.cs @@ -65,10 +65,10 @@ internal static ActivityContext GetXRayParentContext() return activityContext; } - internal static (ActivityContext ParentContext, IEnumerable Links) ExtractParentContext(TInput input) + internal static (ActivityContext ParentContext, IEnumerable? Links) ExtractParentContext(TInput input) { PropagationContext parentContext = default; - IEnumerable links = null; + IEnumerable? links = null; switch (input) { case APIGatewayProxyRequest apiGatewayProxyRequest: @@ -104,7 +104,7 @@ internal static string GetAWSRegion() return Environment.GetEnvironmentVariable(AWSRegion); } - internal static string GetFunctionName(ILambdaContext context = null) + internal static string GetFunctionName(ILambdaContext? context = null) { return context?.FunctionName ?? Environment.GetEnvironmentVariable(FunctionName); } @@ -152,7 +152,7 @@ internal static IEnumerable> GetFunctionTags GetHeaderValues(APIGatewayProxyRequest request, string name) + internal static IEnumerable? GetHeaderValues(APIGatewayProxyRequest request, string name) { var multiValueHeader = request.MultiValueHeaders?.GetValueByKeyIgnoringCase(name); if (multiValueHeader != null) @@ -165,7 +165,7 @@ internal static IEnumerable GetHeaderValues(APIGatewayProxyRequest reque return headerValue != null ? new[] { headerValue } : null; } - internal static IEnumerable GetHeaderValues(APIGatewayHttpApiV2ProxyRequest request, string name) + internal static IEnumerable? GetHeaderValues(APIGatewayHttpApiV2ProxyRequest request, string name) { var headerValue = GetHeaderValue(request, name); @@ -173,10 +173,10 @@ internal static IEnumerable GetHeaderValues(APIGatewayHttpApiV2ProxyRequ return headerValue?.Split(','); } - private static string GetHeaderValue(APIGatewayHttpApiV2ProxyRequest request, string name) => + private static string? GetHeaderValue(APIGatewayHttpApiV2ProxyRequest request, string name) => request.Headers?.GetValueByKeyIgnoringCase(name); - private static string GetAccountId(string functionArn) + private static string? GetAccountId(string functionArn) { // The fifth item of function arn: https://github.com/open-telemetry/opentelemetry-specification/blob/86aeab1e0a7e6c67be09c7f15ff25063ee6d2b5c/specification/trace/semantic_conventions/instrumentation/aws-lambda.md#all-triggers // Function arn format - arn:aws:lambda:::function: diff --git a/src/OpenTelemetry.Instrumentation.AWSLambda/Implementation/AWSMessagingUtils.cs b/src/OpenTelemetry.Instrumentation.AWSLambda/Implementation/AWSMessagingUtils.cs index 05f28caacf..2132600e9e 100644 --- a/src/OpenTelemetry.Instrumentation.AWSLambda/Implementation/AWSMessagingUtils.cs +++ b/src/OpenTelemetry.Instrumentation.AWSLambda/Implementation/AWSMessagingUtils.cs @@ -38,7 +38,7 @@ internal class AWSMessagingUtils /// internal static bool SetParentFromMessageBatch { get; set; } - internal static (PropagationContext ParentContext, IEnumerable Links) ExtractParentContext(SQSEvent sqsEvent) + internal static (PropagationContext ParentContext, IEnumerable? Links) ExtractParentContext(SQSEvent sqsEvent) { if (sqsEvent?.Records == null) { @@ -74,7 +74,7 @@ internal static PropagationContext ExtractParentContext(SQSEvent.SQSMessage sqsM { // SQS subscribed to SNS topic with raw delivery disabled case, i.e. SNS record serialized into SQS body. // https://docs.aws.amazon.com/sns/latest/dg/sns-large-payload-raw-message-delivery.html - SNSEvent.SNSMessage snsMessage = GetSnsMessage(sqsMessage); + SNSEvent.SNSMessage? snsMessage = GetSnsMessage(sqsMessage); if (snsMessage != null) { parentContext = ExtractParentContext(snsMessage); @@ -92,7 +92,7 @@ internal static PropagationContext ExtractParentContext(SNSEvent snsEvent) return ExtractParentContext(record); } - internal static PropagationContext ExtractParentContext(SNSEvent.SNSRecord record) + internal static PropagationContext ExtractParentContext(SNSEvent.SNSRecord? record) { return (record?.Sns?.MessageAttributes != null) ? Propagators.DefaultTextMapPropagator.Extract(default, record.Sns.MessageAttributes, SnsMessageAttributeGetter) : @@ -106,7 +106,7 @@ internal static PropagationContext ExtractParentContext(SNSEvent.SNSMessage mess default; } - private static IEnumerable SqsMessageAttributeGetter(IDictionary attributes, string attributeName) + private static IEnumerable? SqsMessageAttributeGetter(IDictionary attributes, string attributeName) { if (!attributes.TryGetValue(attributeName, out var attribute)) { @@ -118,7 +118,7 @@ private static IEnumerable SqsMessageAttributeGetter(IDictionary SnsMessageAttributeGetter(IDictionary attributes, string attributeName) + private static IEnumerable? SnsMessageAttributeGetter(IDictionary attributes, string attributeName) { if (!attributes.TryGetValue(attributeName, out var attribute)) { @@ -137,9 +137,9 @@ private static IEnumerable SnsMessageAttributeGetter(IDictionary> tags, string tagName, object tagValue) + internal static void AddTagIfNotNull(this List> tags, string tagName, object? tagValue) { if (tagValue != null) { @@ -29,7 +29,7 @@ internal static void AddTagIfNotNull(this List> tag } } - internal static T GetValueByKeyIgnoringCase(this IDictionary dict, string key) + internal static T? GetValueByKeyIgnoringCase(this IDictionary dict, string key) { // TODO: there may be opportunities for performance improvements of this method. diff --git a/src/OpenTelemetry.Instrumentation.AWSLambda/OpenTelemetry.Instrumentation.AWSLambda.csproj b/src/OpenTelemetry.Instrumentation.AWSLambda/OpenTelemetry.Instrumentation.AWSLambda.csproj index be934118fc..179471dc3f 100644 --- a/src/OpenTelemetry.Instrumentation.AWSLambda/OpenTelemetry.Instrumentation.AWSLambda.csproj +++ b/src/OpenTelemetry.Instrumentation.AWSLambda/OpenTelemetry.Instrumentation.AWSLambda.csproj @@ -5,6 +5,7 @@ AWS Lambda tracing wrapper for OpenTelemetry .NET $(PackageTags);AWS Lambda Instrumentation.AWSLambda- + enable diff --git a/src/OpenTelemetry.Instrumentation.AWSLambda/TracerProviderBuilderExtensions.cs b/src/OpenTelemetry.Instrumentation.AWSLambda/TracerProviderBuilderExtensions.cs index d02312e536..42e5d733bf 100644 --- a/src/OpenTelemetry.Instrumentation.AWSLambda/TracerProviderBuilderExtensions.cs +++ b/src/OpenTelemetry.Instrumentation.AWSLambda/TracerProviderBuilderExtensions.cs @@ -42,7 +42,7 @@ public static TracerProviderBuilder AddAWSLambdaConfigurations(this TracerProvid /// The instance of to chain the calls. public static TracerProviderBuilder AddAWSLambdaConfigurations( this TracerProviderBuilder builder, - Action configure) + Action? configure) { Guard.ThrowIfNull(builder); diff --git a/test/OpenTelemetry.Instrumentation.AWSLambda.Tests/AWSLambdaWrapperTests.cs b/test/OpenTelemetry.Instrumentation.AWSLambda.Tests/AWSLambdaWrapperTests.cs index ac42826f3c..4f63719b6d 100644 --- a/test/OpenTelemetry.Instrumentation.AWSLambda.Tests/AWSLambdaWrapperTests.cs +++ b/test/OpenTelemetry.Instrumentation.AWSLambda.Tests/AWSLambdaWrapperTests.cs @@ -62,7 +62,7 @@ public void TraceSyncWithInputAndReturn(bool setCustomParent) { var parentContext = setCustomParent ? CreateParentContext() : default; var result = AWSLambdaWrapper.Trace(tracerProvider, this.sampleHandlers.SampleHandlerSyncInputAndReturn, "TestStream", this.sampleLambdaContext, parentContext); - var resource = tracerProvider.GetResource(); + var resource = tracerProvider?.GetResource(); this.AssertResourceAttributes(resource); } @@ -88,7 +88,7 @@ public void TraceSyncWithInputAndNoReturn(bool setCustomParent) { var parentContext = setCustomParent ? CreateParentContext() : default; AWSLambdaWrapper.Trace(tracerProvider, this.sampleHandlers.SampleHandlerSyncInputAndNoReturn, "TestStream", this.sampleLambdaContext, parentContext); - var resource = tracerProvider.GetResource(); + var resource = tracerProvider?.GetResource(); this.AssertResourceAttributes(resource); } @@ -114,7 +114,7 @@ public async Task TraceAsyncWithInputAndReturn(bool setCustomParent) { var parentContext = setCustomParent ? CreateParentContext() : default; var result = await AWSLambdaWrapper.TraceAsync(tracerProvider, this.sampleHandlers.SampleHandlerAsyncInputAndReturn, "TestStream", this.sampleLambdaContext, parentContext); - var resource = tracerProvider.GetResource(); + var resource = tracerProvider?.GetResource(); this.AssertResourceAttributes(resource); } @@ -140,7 +140,7 @@ public async Task TraceAsyncWithInputAndNoReturn(bool setCustomParent) { var parentContext = setCustomParent ? CreateParentContext() : default; await AWSLambdaWrapper.TraceAsync(tracerProvider, this.sampleHandlers.SampleHandlerAsyncInputAndNoReturn, "TestStream", this.sampleLambdaContext, parentContext); - var resource = tracerProvider.GetResource(); + var resource = tracerProvider?.GetResource(); this.AssertResourceAttributes(resource); } @@ -171,7 +171,7 @@ public void TestLambdaHandlerException(bool setCustomParent) } catch { - var resource = tracerProvider.GetResource(); + var resource = tracerProvider?.GetResource(); this.AssertResourceAttributes(resource); } } @@ -198,7 +198,7 @@ public void TestLambdaHandlerNotSampled() .Build()) { var result = AWSLambdaWrapper.Trace(tracerProvider, this.sampleHandlers.SampleHandlerSyncInputAndReturn, "TestStream", this.sampleLambdaContext); - var resource = tracerProvider.GetResource(); + var resource = tracerProvider?.GetResource(); this.AssertResourceAttributes(resource); } @@ -214,7 +214,7 @@ public void OnFunctionStart_NoParent_ActivityCreated() { Environment.SetEnvironmentVariable("_X_AMZN_TRACE_ID", null); - Activity activity = null; + Activity? activity = null; using (var tracerProvider = Sdk.CreateTracerProviderBuilder() .AddAWSLambdaConfigurations() .Build()) @@ -229,7 +229,7 @@ public void OnFunctionStart_NoParent_ActivityCreated() public void OnFunctionStart_NoSampledAndAwsXRayContextExtractionDisabled_ActivityCreated() { Environment.SetEnvironmentVariable("_X_AMZN_TRACE_ID", $"Root=1-5759e988-bd862e3fe1be46a994272793;Parent={XRayParentId};Sampled=0"); - Activity activity = null; + Activity? activity = null; using (var tracerProvider = Sdk.CreateTracerProviderBuilder() .AddAWSLambdaConfigurations(c => c.DisableAwsXRayContextExtraction = true) @@ -261,8 +261,13 @@ private void AssertSpanProperties(Activity activity, string parentId) Assert.Matches(@"^\d+(\.\d+){3}$", activity.Source.Version); } - private void AssertResourceAttributes(Resource resource) + private void AssertResourceAttributes(Resource? resource) { + if (resource == null) + { + return; + } + var resourceAttributes = resource.Attributes.ToDictionary(x => x.Key, x => x.Value); Assert.Equal("aws", resourceAttributes[AWSLambdaSemanticConventions.AttributeCloudProvider]); Assert.Equal("us-east-1", resourceAttributes[AWSLambdaSemanticConventions.AttributeCloudRegion]); diff --git a/test/OpenTelemetry.Instrumentation.AWSLambda.Tests/Implementation/AWSLambdaHttpUtilsTests.cs b/test/OpenTelemetry.Instrumentation.AWSLambda.Tests/Implementation/AWSLambdaHttpUtilsTests.cs index 3861ec8414..b93e9798cf 100644 --- a/test/OpenTelemetry.Instrumentation.AWSLambda.Tests/Implementation/AWSLambdaHttpUtilsTests.cs +++ b/test/OpenTelemetry.Instrumentation.AWSLambda.Tests/Implementation/AWSLambdaHttpUtilsTests.cs @@ -195,7 +195,11 @@ public void SetHttpTagsFromResult_APIGatewayProxyResponse_SetsCorrectTags() { { "http.status_code", 200 }, }; - AssertTags(expectedTags, activity.TagObjects); + + var actualTags = activity?.TagObjects + .Select(kvp => new KeyValuePair(kvp.Key, kvp.Value ?? new object())); + + AssertTags(expectedTags, actualTags); } [Fact] @@ -221,7 +225,11 @@ public void SetHttpTagsFromResult_APIGatewayHttpApiV2ProxyResponse_SetsCorrectTa { { "http.status_code", 200 }, }; - AssertTags(expectedTags, activity.TagObjects); + + var actualTags = activity?.TagObjects + .Select(kvp => new KeyValuePair(kvp.Key, kvp.Value ?? new object())); + + AssertTags(expectedTags, actualTags); } [Theory] @@ -280,14 +288,14 @@ public void GetQueryString_APIGatewayHttpApiV2ProxyRequest_CorrectQueryString(st Assert.Equal(expectedQueryString, queryString); } - private static void AssertTags(IReadOnlyDictionary expectedTags, IEnumerable> actualTags) + private static void AssertTags(IReadOnlyDictionary expectedTags, IEnumerable>? actualTags) where TActualValue : class { Assert.NotNull(actualTags); Assert.Equal(expectedTags.Count, actualTags.Count()); foreach (var tag in expectedTags) { - Assert.Contains(new KeyValuePair(tag.Key, tag.Value as TActualValue), actualTags); + Assert.Contains(new KeyValuePair(tag.Key, (TActualValue)tag.Value), actualTags); } } } diff --git a/test/OpenTelemetry.Instrumentation.AWSLambda.Tests/Implementation/AWSMessagingUtilsTests.cs b/test/OpenTelemetry.Instrumentation.AWSLambda.Tests/Implementation/AWSMessagingUtilsTests.cs index 770a4036e6..0d791ffe7a 100644 --- a/test/OpenTelemetry.Instrumentation.AWSLambda.Tests/Implementation/AWSMessagingUtilsTests.cs +++ b/test/OpenTelemetry.Instrumentation.AWSLambda.Tests/Implementation/AWSMessagingUtilsTests.cs @@ -34,7 +34,7 @@ public class AWSMessagingUtilsTests : IDisposable private const string SpanId1 = "b9c7c989f97918e1"; private const string SpanId2 = "b9c7c989f97918e2"; - private readonly TracerProvider tracerProvider; + private readonly TracerProvider? tracerProvider; public AWSMessagingUtilsTests() { @@ -44,7 +44,7 @@ public AWSMessagingUtilsTests() public void Dispose() { - this.tracerProvider.Dispose(); + this.tracerProvider?.Dispose(); } [Fact] @@ -53,10 +53,10 @@ public void ExtractParentContext_SetParentFromMessageBatchIsDisabled_ParentIsNot AWSMessagingUtils.SetParentFromMessageBatch = false; var sqsEvent = CreateSqsEventWithMessages(new[] { SpanId1, SpanId2 }); - (PropagationContext parentContext, IEnumerable links) = AWSMessagingUtils.ExtractParentContext(sqsEvent); + (PropagationContext parentContext, IEnumerable? links) = AWSMessagingUtils.ExtractParentContext(sqsEvent); Assert.Equal(default, parentContext); - Assert.Equal(2, links.Count()); + Assert.Equal(2, links?.Count()); } [Fact] @@ -65,11 +65,11 @@ public void ExtractParentContext_SetParentFromMessageBatchIsEnabled_ParentIsSetF AWSMessagingUtils.SetParentFromMessageBatch = true; var sqsEvent = CreateSqsEventWithMessages(new[] { SpanId1, SpanId2 }); - (PropagationContext parentContext, IEnumerable links) = AWSMessagingUtils.ExtractParentContext(sqsEvent); + (PropagationContext parentContext, IEnumerable? links) = AWSMessagingUtils.ExtractParentContext(sqsEvent); Assert.NotEqual(default, parentContext); Assert.Equal(SpanId2, parentContext.ActivityContext.SpanId.ToHexString()); - Assert.Equal(2, links.Count()); + Assert.Equal(2, links?.Count()); } [Fact] @@ -106,11 +106,11 @@ public void ExtractParentContext_SetParentFromMessageBatchIsEnabled_ParentIsSetF }, }; - (PropagationContext parentContext, IEnumerable links) = AWSMessagingUtils.ExtractParentContext(sqsEvent); + (PropagationContext parentContext, IEnumerable? links) = AWSMessagingUtils.ExtractParentContext(sqsEvent); Assert.NotEqual(default, parentContext); Assert.Equal(SpanId1, parentContext.ActivityContext.SpanId.ToHexString()); - Assert.Single(links); + Assert.Single(links ?? Array.Empty()); } private static SQSEvent CreateSqsEventWithMessages(string[] spans) diff --git a/test/OpenTelemetry.Instrumentation.AWSLambda.Tests/OpenTelemetry.Instrumentation.AWSLambda.Tests.csproj b/test/OpenTelemetry.Instrumentation.AWSLambda.Tests/OpenTelemetry.Instrumentation.AWSLambda.Tests.csproj index 95fbba74b5..f2d50fcda4 100644 --- a/test/OpenTelemetry.Instrumentation.AWSLambda.Tests/OpenTelemetry.Instrumentation.AWSLambda.Tests.csproj +++ b/test/OpenTelemetry.Instrumentation.AWSLambda.Tests/OpenTelemetry.Instrumentation.AWSLambda.Tests.csproj @@ -5,6 +5,7 @@ net7.0;net6.0 true true + enable diff --git a/test/OpenTelemetry.Instrumentation.AWSLambda.Tests/SampleLambdaContext.cs b/test/OpenTelemetry.Instrumentation.AWSLambda.Tests/SampleLambdaContext.cs index 8b77918a75..2ad2ac4da4 100644 --- a/test/OpenTelemetry.Instrumentation.AWSLambda.Tests/SampleLambdaContext.cs +++ b/test/OpenTelemetry.Instrumentation.AWSLambda.Tests/SampleLambdaContext.cs @@ -23,21 +23,21 @@ public class SampleLambdaContext : ILambdaContext { public string AwsRequestId { get; } = "testrequestid"; - public IClientContext ClientContext { get; } + public IClientContext? ClientContext { get; } public string FunctionName { get; } = "testfunction"; public string FunctionVersion { get; } = "latest"; - public ICognitoIdentity Identity { get; } + public ICognitoIdentity? Identity { get; } public string InvokedFunctionArn { get; } = "arn:aws:lambda:us-east-1:111111111111:function:testfunction"; - public ILambdaLogger Logger { get; } + public ILambdaLogger? Logger { get; } - public string LogGroupName { get; } + public string? LogGroupName { get; } - public string LogStreamName { get; } + public string? LogStreamName { get; } public int MemoryLimitInMB { get; }