Skip to content

Commit

Permalink
open-telemetry#894: made ILambdaContext non-nullable (for AWS lambda …
Browse files Browse the repository at this point in the history
…functions it must be always available)
  • Loading branch information
rypdal committed Aug 8, 2023
1 parent fce5a66 commit edd3e9d
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<TInput, TResult>(OpenTelemetry.Trace.TracerProvider? tracerProvider, System.Func<TInput, Amazon.Lambda.Core.ILambdaContext?, TResult>! lambdaHandler, TInput input, Amazon.Lambda.Core.ILambdaContext? context, System.Diagnostics.ActivityContext parentContext = default(System.Diagnostics.ActivityContext)) -> TResult
static OpenTelemetry.Instrumentation.AWSLambda.AWSLambdaWrapper.Trace<TInput>(OpenTelemetry.Trace.TracerProvider? tracerProvider, System.Action<TInput, Amazon.Lambda.Core.ILambdaContext?>! lambdaHandler, TInput input, Amazon.Lambda.Core.ILambdaContext? context, System.Diagnostics.ActivityContext parentContext = default(System.Diagnostics.ActivityContext)) -> void
static OpenTelemetry.Instrumentation.AWSLambda.AWSLambdaWrapper.TraceAsync<TInput, TResult>(OpenTelemetry.Trace.TracerProvider? tracerProvider, System.Func<TInput, Amazon.Lambda.Core.ILambdaContext?, System.Threading.Tasks.Task<TResult>!>! lambdaHandler, TInput input, Amazon.Lambda.Core.ILambdaContext? context, System.Diagnostics.ActivityContext parentContext = default(System.Diagnostics.ActivityContext)) -> System.Threading.Tasks.Task<TResult>!
static OpenTelemetry.Instrumentation.AWSLambda.AWSLambdaWrapper.TraceAsync<TInput>(OpenTelemetry.Trace.TracerProvider? tracerProvider, System.Func<TInput, Amazon.Lambda.Core.ILambdaContext?, System.Threading.Tasks.Task!>! 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.Trace<TInput, TResult>(OpenTelemetry.Trace.TracerProvider! tracerProvider, System.Func<TInput, Amazon.Lambda.Core.ILambdaContext!, TResult>! lambdaHandler, TInput input, Amazon.Lambda.Core.ILambdaContext! context, System.Diagnostics.ActivityContext parentContext = default(System.Diagnostics.ActivityContext)) -> TResult
static OpenTelemetry.Instrumentation.AWSLambda.AWSLambdaWrapper.Trace<TInput>(OpenTelemetry.Trace.TracerProvider! tracerProvider, System.Action<TInput, Amazon.Lambda.Core.ILambdaContext!>! lambdaHandler, TInput input, Amazon.Lambda.Core.ILambdaContext! context, System.Diagnostics.ActivityContext parentContext = default(System.Diagnostics.ActivityContext)) -> void
static OpenTelemetry.Instrumentation.AWSLambda.AWSLambdaWrapper.TraceAsync<TInput, TResult>(OpenTelemetry.Trace.TracerProvider? tracerProvider, System.Func<TInput, Amazon.Lambda.Core.ILambdaContext!, System.Threading.Tasks.Task<TResult>!>! lambdaHandler, TInput input, Amazon.Lambda.Core.ILambdaContext! context, System.Diagnostics.ActivityContext parentContext = default(System.Diagnostics.ActivityContext)) -> System.Threading.Tasks.Task<TResult>!
static OpenTelemetry.Instrumentation.AWSLambda.AWSLambdaWrapper.TraceAsync<TInput>(OpenTelemetry.Trace.TracerProvider? tracerProvider, System.Func<TInput, Amazon.Lambda.Core.ILambdaContext!, System.Threading.Tasks.Task!>! 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<OpenTelemetry.Instrumentation.AWSLambda.AWSLambdaInstrumentationOptions!>? configure) -> OpenTelemetry.Trace.TracerProviderBuilder!
39 changes: 22 additions & 17 deletions src/OpenTelemetry.Instrumentation.AWSLambda/AWSLambdaWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,15 @@ public static class AWSLambdaWrapper
/// </param>
/// <returns>Instance of output result.</returns>
public static TResult Trace<TInput, TResult>(
TracerProvider? tracerProvider,
Func<TInput, ILambdaContext?, TResult> lambdaHandler,
TracerProvider tracerProvider,
Func<TInput, ILambdaContext, TResult> lambdaHandler,
TInput input,
ILambdaContext? context,
ILambdaContext context,
ActivityContext parentContext = default)
{
Guard.ThrowIfNull(lambdaHandler);
Guard.ThrowIfNull(context);

return TraceInternal(tracerProvider, lambdaHandler, input, context, parentContext);
}

Expand All @@ -90,15 +92,16 @@ public static TResult Trace<TInput, TResult>(
/// unless X-Ray propagation is disabled in the configuration for this wrapper.
/// </param>
public static void Trace<TInput>(
TracerProvider? tracerProvider,
Action<TInput, ILambdaContext?> lambdaHandler,
TracerProvider tracerProvider,
Action<TInput, ILambdaContext> lambdaHandler,
TInput input,
ILambdaContext? context,
ILambdaContext context,
ActivityContext parentContext = default)
{
Guard.ThrowIfNull(lambdaHandler);
Guard.ThrowIfNull(context);

object? Handler(TInput input, ILambdaContext? context)
object? Handler(TInput input, ILambdaContext context)
{
lambdaHandler(input, context);
return null;
Expand All @@ -124,14 +127,15 @@ public static void Trace<TInput>(
/// <returns>Task.</returns>
public static Task TraceAsync<TInput>(
TracerProvider? tracerProvider,
Func<TInput, ILambdaContext?, Task> lambdaHandler,
Func<TInput, ILambdaContext, Task> lambdaHandler,
TInput input,
ILambdaContext? context,
ILambdaContext context,
ActivityContext parentContext = default)
{
Guard.ThrowIfNull(lambdaHandler);
Guard.ThrowIfNull(context);

async Task<object?> Handler(TInput input, ILambdaContext? context)
async Task<object?> Handler(TInput input, ILambdaContext context)
{
await lambdaHandler(input, context).ConfigureAwait(false);
return null;
Expand All @@ -158,18 +162,19 @@ public static Task TraceAsync<TInput>(
/// <returns>Task of result.</returns>
public static Task<TResult> TraceAsync<TInput, TResult>(
TracerProvider? tracerProvider,
Func<TInput, ILambdaContext?, Task<TResult>> lambdaHandler,
Func<TInput, ILambdaContext, Task<TResult>> lambdaHandler,
TInput input,
ILambdaContext? context,
ILambdaContext context,
ActivityContext parentContext = default)
{
Guard.ThrowIfNull(lambdaHandler);

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

#pragma warning restore RS0026 // Do not add multiple public overloads with optional parameters

internal static Activity? OnFunctionStart<TInput>(TInput input, ILambdaContext? context, ActivityContext parentContext = default)
internal static Activity? OnFunctionStart<TInput>(TInput input, ILambdaContext context, ActivityContext parentContext = default)
{
IEnumerable<ActivityLink>? links = null;
if (parentContext == default)
Expand Down Expand Up @@ -213,9 +218,9 @@ private static void OnException(Activity? activity, Exception exception)

private static TResult TraceInternal<TInput, TResult>(
TracerProvider? tracerProvider,
Func<TInput, ILambdaContext?, TResult> handler,
Func<TInput, ILambdaContext, TResult> handler,
TInput input,
ILambdaContext? context,
ILambdaContext context,
ActivityContext parentContext = default)
{
var activity = OnFunctionStart(input, context, parentContext);
Expand All @@ -239,9 +244,9 @@ private static TResult TraceInternal<TInput, TResult>(

private static async Task<TResult> TraceInternalAsync<TInput, TResult>(
TracerProvider? tracerProvider,
Func<TInput, ILambdaContext?, Task<TResult>> handlerAsync,
Func<TInput, ILambdaContext, Task<TResult>> handlerAsync,
TInput input,
ILambdaContext? context,
ILambdaContext context,
ActivityContext parentContext = default)
{
var activity = OnFunctionStart(input, context, parentContext);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ internal static string GetFunctionVersion()
return Environment.GetEnvironmentVariable(FunctionVersion);
}

internal static IEnumerable<KeyValuePair<string, object>> GetFunctionTags<TInput>(TInput input, ILambdaContext? context)
internal static IEnumerable<KeyValuePair<string, object>> GetFunctionTags<TInput>(TInput input, ILambdaContext context)
{
var tags = new List<KeyValuePair<string, object>>
{
Expand All @@ -127,11 +127,6 @@ internal static IEnumerable<KeyValuePair<string, object>> GetFunctionTags<TInput
tags.Add(new(AWSLambdaSemanticConventions.AttributeFaasName, functionName));
}

if (context == null)
{
return tags;
}

if (context.AwsRequestId != null)
{
tags.Add(new(AWSLambdaSemanticConventions.AttributeFaasExecution, context.AwsRequestId));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,30 +23,30 @@ namespace OpenTelemetry.Instrumentation.AWSLambda.Tests;
internal class SampleHandlers
{
// Action<TInput, ILambdaContext>
public void SampleHandlerSyncInputAndNoReturn(string str, ILambdaContext? context)
public void SampleHandlerSyncInputAndNoReturn(string str, ILambdaContext context)
{
}

// Func<TInput, ILambdaContext, TResult>
public string SampleHandlerSyncInputAndReturn(string str, ILambdaContext? context)
public string SampleHandlerSyncInputAndReturn(string str, ILambdaContext context)
{
return str;
}

// Func<TInput, ILambdaContext, Task>
public async Task SampleHandlerAsyncInputAndNoReturn(string str, ILambdaContext? context)
public async Task SampleHandlerAsyncInputAndNoReturn(string str, ILambdaContext context)
{
await Task.Delay(10);
}

// Func<TInput, ILambdaContext, Task<TResult>>
public async Task<string> SampleHandlerAsyncInputAndReturn(string str, ILambdaContext? context)
public async Task<string> SampleHandlerAsyncInputAndReturn(string str, ILambdaContext context)
{
await Task.Delay(10);
return str;
}

public void SampleHandlerSyncNoReturnException(string str, ILambdaContext? context)
public void SampleHandlerSyncNoReturnException(string str, ILambdaContext context)
{
throw new Exception(str);
}
Expand Down

0 comments on commit edd3e9d

Please sign in to comment.