Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Instrumentation.AWSLambda] Fix analysis warnings #959

Merged
merged 6 commits into from
Feb 7, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 16 additions & 12 deletions src/OpenTelemetry.Instrumentation.AWSLambda/AWSLambdaWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -69,6 +70,7 @@ public static TResult Trace<TInput, TResult>(
ILambdaContext context,
ActivityContext parentContext = default)
{
Guard.ThrowIfNull(lambdaHandler);
Kielek marked this conversation as resolved.
Show resolved Hide resolved
return TraceInternal(tracerProvider, lambdaHandler, input, context, parentContext);
}

Expand All @@ -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)
Kielek marked this conversation as resolved.
Show resolved Hide resolved
{
lambdaHandler(input, context);
return null;
};
TraceInternal(tracerProvider, func, input, context, parentContext);
}

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

/// <summary>
Expand All @@ -123,12 +128,13 @@ public static Task TraceAsync<TInput>(
ILambdaContext context,
ActivityContext parentContext = default)
{
Func<TInput, ILambdaContext, Task<object>> func = async (input, context) =>
async Task<object> Handler(TInput input, ILambdaContext context)
Kielek marked this conversation as resolved.
Show resolved Hide resolved
{
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>
Expand All @@ -154,6 +160,7 @@ public static Task<TResult> TraceAsync<TInput, TResult>(
ILambdaContext context,
ActivityContext parentContext = default)
{
Guard.ThrowIfNull(lambdaHandler);
return TraceInternalAsync(tracerProvider, lambdaHandler, input, context, parentContext);
}

Expand Down Expand Up @@ -182,10 +189,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();
Expand Down Expand Up @@ -239,7 +243,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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '&';
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
}