Skip to content

Commit

Permalink
[Instrumentation.AWSLambda] Incoming FaaS Span attributes: detect and…
Browse files Browse the repository at this point in the history
… set attribute for cold start (#2037)

Co-authored-by: Christian Neumüller <[email protected]>
  • Loading branch information
rypdal and Oberon00 authored Sep 4, 2024
1 parent 544eb98 commit 0691810
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ public static class AWSLambdaWrapper

private static readonly ActivitySource AWSLambdaActivitySource = new(ActivitySourceName, typeof(AWSLambdaWrapper).Assembly.GetPackageVersion());

private static bool isColdStart = true;

/// <summary>
/// Gets or sets a value indicating whether AWS X-Ray propagation should be ignored. Default value is false.
/// </summary>
Expand Down Expand Up @@ -160,7 +162,9 @@ public static Task<TResult> TraceAsync<TInput, TResult>(
}
}

var functionTags = AWSLambdaUtils.GetFunctionTags(input, context);
// No parallel invocation of the same lambda handler expected.
var functionTags = AWSLambdaUtils.GetFunctionTags(input, context, isColdStart);
isColdStart = false;
var httpTags = AWSLambdaHttpUtils.GetHttpTags(input);

// We assume that functionTags and httpTags have no intersection.
Expand All @@ -170,6 +174,9 @@ public static Task<TResult> TraceAsync<TInput, TResult>(
return activity;
}

// Use only for testing.
internal static void ResetColdStart() => isColdStart = true;

private static void OnFunctionStop(Activity? activity, TracerProvider? tracerProvider)
{
activity?.Stop();
Expand Down
3 changes: 3 additions & 0 deletions src/OpenTelemetry.Instrumentation.AWSLambda/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

## Unreleased

* Add detection of Lambda cold start and set `faas.coldstart` Activity tag.
([#2037](https://github.com/open-telemetry/opentelemetry-dotnet-contrib/pull/2037))

## 1.3.0-beta.1

Released 2024-Jan-26
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ internal static class AWSLambdaSemanticConventions
public const string AttributeFaasName = "faas.name";
public const string AttributeFaasVersion = "faas.version";
public const string AttributeFaasTrigger = "faas.trigger";
public const string AttributeFaasColdStart = "faas.coldstart";
}
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,12 @@ internal static string GetCloudProvider()
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, bool isColdStart)
{
var tags = new List<KeyValuePair<string, object>>
{
new(AWSLambdaSemanticConventions.AttributeFaasTrigger, GetFaasTrigger(input)),
new(AWSLambdaSemanticConventions.AttributeFaasColdStart, isColdStart),
};

var functionName = GetFunctionName(context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,30 @@ public void OnFunctionStart_NoSampledAndAwsXRayContextExtractionDisabled_Activit
Assert.NotNull(activity);
}

[Theory]
[InlineData(1)]
[InlineData(2)]
public void OnFunctionStart_ColdStart_ColdStartTagHasCorrectValue(int invocationsCount)
{
AWSLambdaWrapper.ResetColdStart();
Activity? activity = null;

using (var tracerProvider = Sdk.CreateTracerProviderBuilder()
.AddAWSLambdaConfigurations(c => c.DisableAwsXRayContextExtraction = true)
.Build())
{
for (int i = 1; i <= invocationsCount; i++)
{
activity = AWSLambdaWrapper.OnFunctionStart("test-input", new SampleLambdaContext());
}
}

Assert.NotNull(activity);
Assert.NotNull(activity.TagObjects);
var expectedColdStartValue = invocationsCount == 1 ? true : false;
Assert.Contains(activity.TagObjects, x => x.Key == AWSLambdaSemanticConventions.AttributeFaasColdStart && expectedColdStartValue.Equals(x.Value));
}

private static ActivityContext CreateParentContext()
{
var traceId = ActivityTraceId.CreateFromString(TraceId.AsSpan());
Expand Down

0 comments on commit 0691810

Please sign in to comment.