Skip to content

Commit

Permalink
open-telemetry#894: enabled nullable for AWSLambda instrumentation
Browse files Browse the repository at this point in the history
  • Loading branch information
rypdal committed Aug 7, 2023
1 parent f56ae3b commit aa525f5
Show file tree
Hide file tree
Showing 14 changed files with 93 additions and 72 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#nullable enable
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.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
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!
28 changes: 14 additions & 14 deletions src/OpenTelemetry.Instrumentation.AWSLambda/AWSLambdaWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public static class AWSLambdaWrapper
/// </param>
/// <returns>Instance of output result.</returns>
public static TResult Trace<TInput, TResult>(
TracerProvider tracerProvider,
TracerProvider? tracerProvider,
Func<TInput, ILambdaContext, TResult> lambdaHandler,
TInput input,
ILambdaContext context,
Expand All @@ -90,15 +90,15 @@ 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,
TracerProvider? tracerProvider,
Action<TInput, ILambdaContext> lambdaHandler,
TInput input,
ILambdaContext context,
ActivityContext parentContext = default)
{
Guard.ThrowIfNull(lambdaHandler);

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

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 @@ -157,7 +157,7 @@ async Task<object> Handler(TInput input, ILambdaContext context)
/// </param>
/// <returns>Task of result.</returns>
public static Task<TResult> TraceAsync<TInput, TResult>(
TracerProvider tracerProvider,
TracerProvider? tracerProvider,
Func<TInput, ILambdaContext, Task<TResult>> lambdaHandler,
TInput input,
ILambdaContext context,
Expand All @@ -169,9 +169,9 @@ public static Task<TResult> TraceAsync<TInput, TResult>(

#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;
IEnumerable<ActivityLink>? links = null;
if (parentContext == default)
{
(parentContext, links) = AWSLambdaUtils.ExtractParentContext(input);
Expand All @@ -181,8 +181,8 @@ internal static Activity OnFunctionStart<TInput>(TInput input, ILambdaContext co
}
}

var functionTags = AWSLambdaUtils.GetFunctionTags(input, context);
var httpTags = AWSLambdaHttpUtils.GetHttpTags(input);
var functionTags = (IEnumerable<KeyValuePair<string, object?>>)AWSLambdaUtils.GetFunctionTags(input, context);
var httpTags = (IEnumerable<KeyValuePair<string, object?>>)AWSLambdaHttpUtils.GetHttpTags(input);

// We assume that functionTags and httpTags have no intersection.
var activityName = AWSLambdaUtils.GetFunctionName(context) ?? "AWS Lambda Invoke";
Expand All @@ -191,15 +191,15 @@ internal static Activity OnFunctionStart<TInput>(TInput input, ILambdaContext co
return activity;
}

private static void OnFunctionStop(Activity activity, TracerProvider tracerProvider)
private static void OnFunctionStop(Activity? activity, TracerProvider? tracerProvider)
{
activity?.Stop();

// force flush before function quit in case of Lambda freeze.
tracerProvider?.ForceFlush();
}

private static void OnException(Activity activity, Exception exception)
private static void OnException(Activity? activity, Exception exception)
{
if (activity != null)
{
Expand All @@ -212,7 +212,7 @@ private static void OnException(Activity activity, Exception exception)
}

private static TResult TraceInternal<TInput, TResult>(
TracerProvider tracerProvider,
TracerProvider? tracerProvider,
Func<TInput, ILambdaContext, TResult> handler,
TInput input,
ILambdaContext context,
Expand All @@ -238,7 +238,7 @@ private static TResult TraceInternal<TInput, TResult>(
}

private static async Task<TResult> TraceInternalAsync<TInput, TResult>(
TracerProvider tracerProvider,
TracerProvider? tracerProvider,
Func<TInput, ILambdaContext, Task<TResult>> handlerAsync,
TInput input,
ILambdaContext context,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ internal static IEnumerable<KeyValuePair<string, object>> GetHttpTags<TInput>(TI
{
var tags = new List<KeyValuePair<string, object>>();

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)
Expand Down Expand Up @@ -68,8 +68,13 @@ internal static IEnumerable<KeyValuePair<string, object>> GetHttpTags<TInput>(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:
Expand All @@ -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)
{
Expand All @@ -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)
{
Expand All @@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,10 @@ internal static ActivityContext GetXRayParentContext()
return activityContext;
}

internal static (ActivityContext ParentContext, IEnumerable<ActivityLink> Links) ExtractParentContext<TInput>(TInput input)
internal static (ActivityContext ParentContext, IEnumerable<ActivityLink>? Links) ExtractParentContext<TInput>(TInput input)
{
PropagationContext parentContext = default;
IEnumerable<ActivityLink> links = null;
IEnumerable<ActivityLink>? links = null;
switch (input)
{
case APIGatewayProxyRequest apiGatewayProxyRequest:
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -152,7 +152,7 @@ internal static IEnumerable<KeyValuePair<string, object>> GetFunctionTags<TInput
return tags;
}

internal static IEnumerable<string> GetHeaderValues(APIGatewayProxyRequest request, string name)
internal static IEnumerable<string>? GetHeaderValues(APIGatewayProxyRequest request, string name)
{
var multiValueHeader = request.MultiValueHeaders?.GetValueByKeyIgnoringCase(name);
if (multiValueHeader != null)
Expand All @@ -165,18 +165,18 @@ internal static IEnumerable<string> GetHeaderValues(APIGatewayProxyRequest reque
return headerValue != null ? new[] { headerValue } : null;
}

internal static IEnumerable<string> GetHeaderValues(APIGatewayHttpApiV2ProxyRequest request, string name)
internal static IEnumerable<string>? GetHeaderValues(APIGatewayHttpApiV2ProxyRequest request, string name)
{
var headerValue = GetHeaderValue(request, name);

// Multiple values for the same header will be separated by a comma.
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:<region>:<account-id>:function:<function-name>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ internal class AWSMessagingUtils
/// </summary>
internal static bool SetParentFromMessageBatch { get; set; }

internal static (PropagationContext ParentContext, IEnumerable<ActivityLink> Links) ExtractParentContext(SQSEvent sqsEvent)
internal static (PropagationContext ParentContext, IEnumerable<ActivityLink>? Links) ExtractParentContext(SQSEvent sqsEvent)
{
if (sqsEvent?.Records == null)
{
Expand Down Expand Up @@ -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);
Expand All @@ -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) :
Expand All @@ -106,7 +106,7 @@ internal static PropagationContext ExtractParentContext(SNSEvent.SNSMessage mess
default;
}

private static IEnumerable<string> SqsMessageAttributeGetter(IDictionary<string, SQSEvent.MessageAttribute> attributes, string attributeName)
private static IEnumerable<string>? SqsMessageAttributeGetter(IDictionary<string, SQSEvent.MessageAttribute> attributes, string attributeName)
{
if (!attributes.TryGetValue(attributeName, out var attribute))
{
Expand All @@ -118,7 +118,7 @@ private static IEnumerable<string> SqsMessageAttributeGetter(IDictionary<string,
attribute?.StringListValues;
}

private static IEnumerable<string> SnsMessageAttributeGetter(IDictionary<string, SNSEvent.MessageAttribute> attributes, string attributeName)
private static IEnumerable<string>? SnsMessageAttributeGetter(IDictionary<string, SNSEvent.MessageAttribute> attributes, string attributeName)
{
if (!attributes.TryGetValue(attributeName, out var attribute))
{
Expand All @@ -137,9 +137,9 @@ private static IEnumerable<string> SnsMessageAttributeGetter(IDictionary<string,
}
}

private static SNSEvent.SNSMessage GetSnsMessage(SQSEvent.SQSMessage sqsMessage)
private static SNSEvent.SNSMessage? GetSnsMessage(SQSEvent.SQSMessage sqsMessage)
{
SNSEvent.SNSMessage snsMessage = null;
SNSEvent.SNSMessage? snsMessage = null;

var body = sqsMessage.Body;
if (body != null &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ namespace OpenTelemetry.Instrumentation.AWSLambda.Implementation;

internal static class CommonExtensions
{
internal static void AddTagIfNotNull(this List<KeyValuePair<string, object>> tags, string tagName, object tagValue)
internal static void AddTagIfNotNull(this List<KeyValuePair<string, object>> tags, string tagName, object? tagValue)
{
if (tagValue != null)
{
tags.Add(new(tagName, tagValue));
}
}

internal static T GetValueByKeyIgnoringCase<T>(this IDictionary<string, T> dict, string key)
internal static T? GetValueByKeyIgnoringCase<T>(this IDictionary<string, T> dict, string key)
{
// TODO: there may be opportunities for performance improvements of this method.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<Description>AWS Lambda tracing wrapper for OpenTelemetry .NET</Description>
<PackageTags>$(PackageTags);AWS Lambda</PackageTags>
<MinVerTagPrefix>Instrumentation.AWSLambda-</MinVerTagPrefix>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public static TracerProviderBuilder AddAWSLambdaConfigurations(this TracerProvid
/// <returns>The instance of <see cref="TracerProviderBuilder"/> to chain the calls.</returns>
public static TracerProviderBuilder AddAWSLambdaConfigurations(
this TracerProviderBuilder builder,
Action<AWSLambdaInstrumentationOptions> configure)
Action<AWSLambdaInstrumentationOptions>? configure)
{
Guard.ThrowIfNull(builder);

Expand Down
Loading

0 comments on commit aa525f5

Please sign in to comment.