Skip to content

Commit fbba3f8

Browse files
authored
[OpenTelemetry.Instrumentation.AWSLambda] Do not crash on empty LambdaContext (open-telemetry#2457)
1 parent a575dcc commit fbba3f8

File tree

4 files changed

+51
-13
lines changed

4 files changed

+51
-13
lines changed

src/OpenTelemetry.Instrumentation.AWSLambda/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## Unreleased
44

5+
* Trace instrumentation will not fail with an exception
6+
if empty `LambdaContext` instance is passed.
7+
([#2457](https://github.com/open-telemetry/opentelemetry-dotnet-contrib/pull/2457))
8+
59
## 1.10.0-rc.1
610

711
Released 2025-Jan-06

src/OpenTelemetry.Instrumentation.AWSLambda/Implementation/AWSLambdaUtils.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,14 +170,19 @@ internal IEnumerable<KeyValuePair<string, object>> GetFunctionTags<TInput>(TInpu
170170
return items.Length >= 5 ? items[4] : null;
171171
}
172172

173-
private static string GetFaasId(string functionArn)
173+
private static string? GetFaasId(string? functionArn)
174174
{
175+
if (string.IsNullOrEmpty(functionArn))
176+
{
177+
return null;
178+
}
179+
175180
var faasId = functionArn;
176181

177182
// According to faas.id description https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/trace/semantic_conventions/instrumentation/aws-lambda.md#all-triggers
178183
// the 8th part of arn (function version or alias, see https://docs.aws.amazon.com/lambda/latest/dg/lambda-api-permissions-ref.html)
179184
// should not be included into faas.id
180-
var items = functionArn.Split(':');
185+
var items = functionArn!.Split(':');
181186
if (items.Length >= 8)
182187
{
183188
faasId = string.Join(":", items.Take(7));

test/OpenTelemetry.Instrumentation.AWSLambda.Tests/AWSLambdaWrapperTests.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,35 @@ public void OnFunctionStart_ColdStart_ColdStartTagHasCorrectValue(int invocation
264264
Assert.Contains(activity.TagObjects, x => x.Key == ExpectedSemanticConventions.AttributeFaasColdStart && expectedColdStartValue.Equals(x.Value));
265265
}
266266

267+
[Fact]
268+
public async Task TraceAsyncDoesNotCrashForEmptyLambdaContext()
269+
{
270+
var emptyLambdaContext = new SampleLambdaContext
271+
{
272+
AwsRequestId = null!,
273+
ClientContext = null,
274+
FunctionName = null!,
275+
FunctionVersion = null!,
276+
Identity = null!,
277+
InvokedFunctionArn = null!,
278+
Logger = null!,
279+
LogGroupName = null!,
280+
LogStreamName = null!,
281+
MemoryLimitInMB = 0,
282+
RemainingTime = TimeSpan.Zero,
283+
};
284+
285+
using var tracerProvider = Sdk.CreateTracerProviderBuilder()
286+
.AddAWSLambdaConfigurations(opt =>
287+
{
288+
opt.SemanticConventionVersion = SemanticConventionVersion.Latest;
289+
})
290+
.Build();
291+
292+
// We simply verify that no exception is thrown here.
293+
await AWSLambdaWrapper.TraceAsync(tracerProvider, this.sampleHandlers.SampleHandlerAsyncInputAndNoReturn, "TestStream", emptyLambdaContext);
294+
}
295+
267296
private static ActivityContext CreateParentContext()
268297
{
269298
var traceId = ActivityTraceId.CreateFromString(TraceId.AsSpan());

test/OpenTelemetry.Instrumentation.AWSLambda.Tests/SampleLambdaContext.cs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,25 @@ namespace OpenTelemetry.Instrumentation.AWSLambda.Tests;
77

88
internal class SampleLambdaContext : ILambdaContext
99
{
10-
public string AwsRequestId { get; } = "testrequestid";
10+
public string AwsRequestId { get; set; } = "testrequestid";
1111

12-
public IClientContext? ClientContext { get; }
12+
public IClientContext? ClientContext { get; set; }
1313

14-
public string FunctionName { get; } = "testfunction";
14+
public string FunctionName { get; set; } = "testfunction";
1515

16-
public string FunctionVersion { get; } = "latest";
16+
public string FunctionVersion { get; set; } = "latest";
1717

18-
public ICognitoIdentity? Identity { get; }
18+
public ICognitoIdentity? Identity { get; set; }
1919

20-
public string InvokedFunctionArn { get; } = "arn:aws:lambda:us-east-1:111111111111:function:testfunction";
20+
public string InvokedFunctionArn { get; set; } = "arn:aws:lambda:us-east-1:111111111111:function:testfunction";
2121

22-
public ILambdaLogger? Logger { get; }
22+
public ILambdaLogger? Logger { get; set; }
2323

24-
public string? LogGroupName { get; }
24+
public string? LogGroupName { get; set; }
2525

26-
public string? LogStreamName { get; }
26+
public string? LogStreamName { get; set; }
2727

28-
public int MemoryLimitInMB { get; }
28+
public int MemoryLimitInMB { get; set; }
2929

30-
public TimeSpan RemainingTime { get; }
30+
public TimeSpan RemainingTime { get; set; }
3131
}

0 commit comments

Comments
 (0)