From f3750676a3e0e42809442b5aa8fe64bebc643846 Mon Sep 17 00:00:00 2001 From: martincostello Date: Fri, 3 Feb 2023 15:28:43 +0000 Subject: [PATCH 1/5] [Instrumentation.AWSLambda] Fix analysis warnings Fix/suppress code analysis warnings in OpenTelemetry.Contrib.Instrumentation.AWSLambda. Contributes to #950. --- .../AWSLambdaWrapper.cs | 7 +++++-- .../Implementation/AWSLambdaHttpUtils.cs | 2 +- .../SampleLambdaContext.cs | 14 +++++++------- 3 files changed, 13 insertions(+), 10 deletions(-) diff --git a/src/OpenTelemetry.Instrumentation.AWSLambda/AWSLambdaWrapper.cs b/src/OpenTelemetry.Instrumentation.AWSLambda/AWSLambdaWrapper.cs index 9359fc17b5..9adbe92e0d 100644 --- a/src/OpenTelemetry.Instrumentation.AWSLambda/AWSLambdaWrapper.cs +++ b/src/OpenTelemetry.Instrumentation.AWSLambda/AWSLambdaWrapper.cs @@ -22,6 +22,7 @@ using System.Threading.Tasks; using Amazon.Lambda.Core; using OpenTelemetry.Instrumentation.AWSLambda.Implementation; +using OpenTelemetry.Internal; using OpenTelemetry.Trace; namespace OpenTelemetry.Instrumentation.AWSLambda; @@ -69,6 +70,7 @@ public static TResult Trace( ILambdaContext context, ActivityContext parentContext = default) { + Guard.ThrowIfNull(lambdaHandler); return TraceInternal(tracerProvider, lambdaHandler, input, context, parentContext); } @@ -125,7 +127,7 @@ public static Task TraceAsync( { Func> func = async (input, context) => { - await lambdaHandler(input, context); + await lambdaHandler(input, context).ConfigureAwait(false); return null; }; return TraceInternalAsync(tracerProvider, func, input, context, parentContext); @@ -154,6 +156,7 @@ public static Task TraceAsync( ILambdaContext context, ActivityContext parentContext = default) { + Guard.ThrowIfNull(lambdaHandler); return TraceInternalAsync(tracerProvider, lambdaHandler, input, context, parentContext); } @@ -239,7 +242,7 @@ private static async Task TraceInternalAsync( var activity = OnFunctionStart(input, context, parentContext); try { - var result = await handlerAsync(input, context); + var result = await handlerAsync(input, context).ConfigureAwait(false); AWSLambdaHttpUtils.SetHttpTagsFromResult(activity, result); return result; } diff --git a/src/OpenTelemetry.Instrumentation.AWSLambda/Implementation/AWSLambdaHttpUtils.cs b/src/OpenTelemetry.Instrumentation.AWSLambda/Implementation/AWSLambdaHttpUtils.cs index 94742e8fd8..0982d20c9b 100644 --- a/src/OpenTelemetry.Instrumentation.AWSLambda/Implementation/AWSLambdaHttpUtils.cs +++ b/src/OpenTelemetry.Instrumentation.AWSLambda/Implementation/AWSLambdaHttpUtils.cs @@ -97,7 +97,7 @@ internal static string GetQueryString(APIGatewayProxyRequest request) { queryString.Append(separator) .Append(HttpUtility.UrlEncode(parameterKvp.Key)) - .Append("=") + .Append('=') .Append(HttpUtility.UrlEncode(value)); separator = '&'; } diff --git a/test/OpenTelemetry.Instrumentation.AWSLambda.Tests/SampleLambdaContext.cs b/test/OpenTelemetry.Instrumentation.AWSLambda.Tests/SampleLambdaContext.cs index bc36ca20d8..8b77918a75 100644 --- a/test/OpenTelemetry.Instrumentation.AWSLambda.Tests/SampleLambdaContext.cs +++ b/test/OpenTelemetry.Instrumentation.AWSLambda.Tests/SampleLambdaContext.cs @@ -23,23 +23,23 @@ public class SampleLambdaContext : ILambdaContext { public string AwsRequestId { get; } = "testrequestid"; - public IClientContext ClientContext { get; } = null; + public IClientContext ClientContext { get; } public string FunctionName { get; } = "testfunction"; public string FunctionVersion { get; } = "latest"; - public ICognitoIdentity Identity { get; } = null; + public ICognitoIdentity Identity { get; } public string InvokedFunctionArn { get; } = "arn:aws:lambda:us-east-1:111111111111:function:testfunction"; - public ILambdaLogger Logger { get; } = null; + public ILambdaLogger Logger { get; } - public string LogGroupName { get; } = null; + public string LogGroupName { get; } - public string LogStreamName { get; } = null; + public string LogStreamName { get; } - public int MemoryLimitInMB { get; } = 0; + public int MemoryLimitInMB { get; } - public TimeSpan RemainingTime { get; } = default; + public TimeSpan RemainingTime { get; } } From 40409f22008814ba3bcc5ddd56ecc33dd14abc4b Mon Sep 17 00:00:00 2001 From: martincostello Date: Tue, 7 Feb 2023 10:33:59 +0000 Subject: [PATCH 2/5] Add missing null guard Guard against null in additional overload. --- src/OpenTelemetry.Instrumentation.AWSLambda/AWSLambdaWrapper.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/OpenTelemetry.Instrumentation.AWSLambda/AWSLambdaWrapper.cs b/src/OpenTelemetry.Instrumentation.AWSLambda/AWSLambdaWrapper.cs index 9adbe92e0d..5ff2b3de1d 100644 --- a/src/OpenTelemetry.Instrumentation.AWSLambda/AWSLambdaWrapper.cs +++ b/src/OpenTelemetry.Instrumentation.AWSLambda/AWSLambdaWrapper.cs @@ -95,6 +95,7 @@ public static void Trace( ILambdaContext context, ActivityContext parentContext = default) { + Guard.ThrowIfNull(lambdaHandler); Func func = (input, context) => { lambdaHandler(input, context); From 1d5ee98be4ff3634b6a69e9c7ed95fd8856b0c21 Mon Sep 17 00:00:00 2001 From: martincostello Date: Tue, 7 Feb 2023 10:40:14 +0000 Subject: [PATCH 3/5] Use local function Apply Visual Studio suggestion to use a local function instead of a Lambda. --- .../AWSLambdaWrapper.cs | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/OpenTelemetry.Instrumentation.AWSLambda/AWSLambdaWrapper.cs b/src/OpenTelemetry.Instrumentation.AWSLambda/AWSLambdaWrapper.cs index 5ff2b3de1d..c36897f31f 100644 --- a/src/OpenTelemetry.Instrumentation.AWSLambda/AWSLambdaWrapper.cs +++ b/src/OpenTelemetry.Instrumentation.AWSLambda/AWSLambdaWrapper.cs @@ -96,12 +96,14 @@ public static void Trace( ActivityContext parentContext = default) { Guard.ThrowIfNull(lambdaHandler); - Func func = (input, context) => + + object Handler(TInput input, ILambdaContext context) { lambdaHandler(input, context); return null; - }; - TraceInternal(tracerProvider, func, input, context, parentContext); + } + + TraceInternal(tracerProvider, Handler, input, context, parentContext); } /// @@ -126,12 +128,13 @@ public static Task TraceAsync( ILambdaContext context, ActivityContext parentContext = default) { - Func> func = async (input, context) => + async Task Handler(TInput input, ILambdaContext context) { await lambdaHandler(input, context).ConfigureAwait(false); return null; - }; - return TraceInternalAsync(tracerProvider, func, input, context, parentContext); + } + + return TraceInternalAsync(tracerProvider, Handler, input, context, parentContext); } /// From ce8e8d2094f615058bab4900664cd30f18cbb4ba Mon Sep 17 00:00:00 2001 From: martincostello Date: Tue, 7 Feb 2023 10:40:35 +0000 Subject: [PATCH 4/5] Use null propagation Use null propagation instead of a null check. --- .../AWSLambdaWrapper.cs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/OpenTelemetry.Instrumentation.AWSLambda/AWSLambdaWrapper.cs b/src/OpenTelemetry.Instrumentation.AWSLambda/AWSLambdaWrapper.cs index c36897f31f..36fd084bcb 100644 --- a/src/OpenTelemetry.Instrumentation.AWSLambda/AWSLambdaWrapper.cs +++ b/src/OpenTelemetry.Instrumentation.AWSLambda/AWSLambdaWrapper.cs @@ -189,10 +189,7 @@ internal static Activity OnFunctionStart(TInput input, ILambdaContext co private static void OnFunctionStop(Activity activity, TracerProvider tracerProvider) { - if (activity != null) - { - activity.Stop(); - } + activity?.Stop(); // force flush before function quit in case of Lambda freeze. tracerProvider?.ForceFlush(); From 9e930ec5c1e32b32a28176dcae65f088eee99c97 Mon Sep 17 00:00:00 2001 From: martincostello Date: Tue, 7 Feb 2023 14:00:24 +0000 Subject: [PATCH 5/5] Add missing null check Add another missing null check to resolve review feedback. --- src/OpenTelemetry.Instrumentation.AWSLambda/AWSLambdaWrapper.cs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/OpenTelemetry.Instrumentation.AWSLambda/AWSLambdaWrapper.cs b/src/OpenTelemetry.Instrumentation.AWSLambda/AWSLambdaWrapper.cs index 36fd084bcb..ba62e517cc 100644 --- a/src/OpenTelemetry.Instrumentation.AWSLambda/AWSLambdaWrapper.cs +++ b/src/OpenTelemetry.Instrumentation.AWSLambda/AWSLambdaWrapper.cs @@ -128,6 +128,8 @@ public static Task TraceAsync( ILambdaContext context, ActivityContext parentContext = default) { + Guard.ThrowIfNull(lambdaHandler); + async Task Handler(TInput input, ILambdaContext context) { await lambdaHandler(input, context).ConfigureAwait(false);