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] Incoming FaaS Span attributes: detect and set attribute for cold start #2037

Merged
Show file tree
Hide file tree
Changes from all 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
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
Loading