Skip to content

Commit

Permalink
[Instrumentation.AWSLambda] Fix analysis warnings (#959)
Browse files Browse the repository at this point in the history
martincostello authored Feb 7, 2023
1 parent 525dd90 commit 9c96392
Showing 3 changed files with 26 additions and 20 deletions.
30 changes: 18 additions & 12 deletions src/OpenTelemetry.Instrumentation.AWSLambda/AWSLambdaWrapper.cs
Original file line number Diff line number Diff line change
@@ -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<TInput, TResult>(
ILambdaContext context,
ActivityContext parentContext = default)
{
Guard.ThrowIfNull(lambdaHandler);
return TraceInternal(tracerProvider, lambdaHandler, input, context, parentContext);
}

@@ -93,12 +95,15 @@ public static void Trace<TInput>(
ILambdaContext context,
ActivityContext parentContext = default)
{
Func<TInput, ILambdaContext, object> func = (input, context) =>
Guard.ThrowIfNull(lambdaHandler);

object Handler(TInput input, ILambdaContext context)
{
lambdaHandler(input, context);
return null;
};
TraceInternal(tracerProvider, func, input, context, parentContext);
}

TraceInternal(tracerProvider, Handler, input, context, parentContext);
}

/// <summary>
@@ -123,12 +128,15 @@ public static Task TraceAsync<TInput>(
ILambdaContext context,
ActivityContext parentContext = default)
{
Func<TInput, ILambdaContext, Task<object>> func = async (input, context) =>
Guard.ThrowIfNull(lambdaHandler);

async Task<object> Handler(TInput input, ILambdaContext context)
{
await lambdaHandler(input, context);
await lambdaHandler(input, context).ConfigureAwait(false);
return null;
};
return TraceInternalAsync(tracerProvider, func, input, context, parentContext);
}

return TraceInternalAsync(tracerProvider, Handler, input, context, parentContext);
}

/// <summary>
@@ -154,6 +162,7 @@ public static Task<TResult> TraceAsync<TInput, TResult>(
ILambdaContext context,
ActivityContext parentContext = default)
{
Guard.ThrowIfNull(lambdaHandler);
return TraceInternalAsync(tracerProvider, lambdaHandler, input, context, parentContext);
}

@@ -182,10 +191,7 @@ internal static Activity OnFunctionStart<TInput>(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();
@@ -239,7 +245,7 @@ private static async Task<TResult> TraceInternalAsync<TInput, TResult>(
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;
}
Original file line number Diff line number Diff line change
@@ -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 = '&';
}
Original file line number Diff line number Diff line change
@@ -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; }
}

0 comments on commit 9c96392

Please sign in to comment.