Skip to content

Commit

Permalink
open-telemetry#2032: unit test for cold start tag
Browse files Browse the repository at this point in the history
  • Loading branch information
rypdal committed Sep 4, 2024
1 parent a7978e3 commit fbf882d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 8 deletions.
14 changes: 6 additions & 8 deletions src/OpenTelemetry.Instrumentation.AWSLambda/AWSLambdaWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -162,14 +162,9 @@ public static Task<TResult> TraceAsync<TInput, TResult>(
}
}

// No parallel invocation of the same lambda handler.
bool faasColdStart = isColdStart;
if (faasColdStart)
{
isColdStart = false;
}

var functionTags = AWSLambdaUtils.GetFunctionTags(input, context, faasColdStart);
// 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 @@ -179,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
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 fbf882d

Please sign in to comment.