Skip to content

Commit 6c8bf07

Browse files
committed
implement AWS Lambda detector
Signed-off-by: AzharH <[email protected]>
1 parent ffdbf76 commit 6c8bf07

File tree

2 files changed

+85
-0
lines changed

2 files changed

+85
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Copyright The OpenTelemetry Authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
using OpenTelemetry.AWS;
5+
using OpenTelemetry.Instrumentation.AWSLambda.Implementation;
6+
using OpenTelemetry.Internal;
7+
using OpenTelemetry.Resources;
8+
9+
namespace OpenTelemetry.Instrumentation.AWSLambda;
10+
11+
/// <summary>
12+
/// Extension methods to simplify registering of AWS Lambda resource detectors.
13+
/// </summary>
14+
public static class AWSLambdaResourceBuilderExtensions
15+
{
16+
/// <summary>
17+
/// Enables AWS Lambda resource detector.
18+
/// </summary>
19+
/// <param name="builder">The <see cref="ResourceBuilder"/> being configured.</param>
20+
/// <param name="configure">Optional callback action for configuring <see cref="AWSLambdaInstrumentationOptions"/>.</param>
21+
/// <returns>The instance of <see cref="ResourceBuilder"/> being configured.</returns>
22+
public static ResourceBuilder AddAWSLambdaDetector(this ResourceBuilder builder, Action<AWSLambdaInstrumentationOptions>? configure = null)
23+
{
24+
Guard.ThrowIfNull(builder);
25+
26+
var options = new AWSLambdaInstrumentationOptions();
27+
configure?.Invoke(options);
28+
29+
var semanticConventionBuilder = new AWSSemanticConventions(options.SemanticConventionVersion);
30+
31+
return builder.AddDetector(new AWSLambdaResourceDetector(semanticConventionBuilder));
32+
}
33+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// Copyright The OpenTelemetry Authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
using OpenTelemetry.Resources;
5+
using Xunit;
6+
7+
namespace OpenTelemetry.Instrumentation.AWSLambda.Tests;
8+
9+
public class AWSLambdaResourceBuilderExtensionsTests
10+
{
11+
// Expected Semantic Conventions
12+
private const string AttributeCloudProvider = "cloud.provider";
13+
private const string AttributeCloudRegion = "cloud.region";
14+
private const string AttributeFaasName = "faas.name";
15+
private const string AttributeFaasVersion = "faas.version";
16+
private const string AttributeFaasInstance = "faas.instance";
17+
private const string AttributeFaasMaxMemory = "faas.max_memory";
18+
19+
public AWSLambdaResourceBuilderExtensionsTests()
20+
{
21+
Environment.SetEnvironmentVariable("AWS_REGION", "us-east-1");
22+
Environment.SetEnvironmentVariable("AWS_LAMBDA_FUNCTION_NAME", "testfunction");
23+
Environment.SetEnvironmentVariable("AWS_LAMBDA_FUNCTION_VERSION", "latest");
24+
Environment.SetEnvironmentVariable("AWS_LAMBDA_FUNCTION_MEMORY_SIZE", "128");
25+
Environment.SetEnvironmentVariable("AWS_LAMBDA_LOG_STREAM_NAME",
26+
"2025/07/21/[$LATEST]7b176c212e954e62adfb9b5451cb5374");
27+
}
28+
29+
[Fact]
30+
public void AssertAttributes()
31+
{
32+
var resourceBuilder = ResourceBuilder.CreateDefault();
33+
resourceBuilder.AddAWSLambdaDetector();
34+
35+
var resource = resourceBuilder.Build();
36+
37+
var resourceAttributes = resource.Attributes
38+
.ToDictionary(x => x.Key, x => x.Value);
39+
40+
Assert.Equal("aws", resourceAttributes[AttributeCloudProvider]);
41+
Assert.Equal("us-east-1", resourceAttributes[AttributeCloudRegion]);
42+
Assert.Equal("testfunction", resourceAttributes[AttributeFaasName]);
43+
Assert.Equal("latest", resourceAttributes[AttributeFaasVersion]);
44+
Assert.Equal("2025/07/21/[$LATEST]7b176c212e954e62adfb9b5451cb5374",
45+
resourceAttributes[AttributeFaasInstance]);
46+
Assert.Equal(134217728L, resourceAttributes[AttributeFaasMaxMemory]);
47+
}
48+
49+
[Fact]
50+
public void AssertArgumentNullException() =>
51+
Assert.Throws<ArgumentNullException>(() => AWSLambdaResourceBuilderExtensions.AddAWSLambdaDetector(null!));
52+
}

0 commit comments

Comments
 (0)