-
Notifications
You must be signed in to change notification settings - Fork 358
Implement AWS Lambda detector #3411
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
base: main
Are you sure you want to change the base?
Changes from 7 commits
6c8bf07
a00874c
840ee3b
32b8659
20d0385
b6b3443
f1290a6
c80f940
d2206e2
db9dc0f
1e0764f
1c6d70d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| OpenTelemetry.Instrumentation.AWSLambda.AWSLambdaResourceBuilderExtensions | ||
| static OpenTelemetry.Instrumentation.AWSLambda.AWSLambdaResourceBuilderExtensions.AddAWSLambdaDetector(this OpenTelemetry.Resources.ResourceBuilder! builder, System.Action<OpenTelemetry.Instrumentation.AWSLambda.AWSLambdaInstrumentationOptions!>? configure = null) -> OpenTelemetry.Resources.ResourceBuilder! |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| // Copyright The OpenTelemetry Authors | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| using OpenTelemetry.AWS; | ||
| using OpenTelemetry.Instrumentation.AWSLambda.Implementation; | ||
| using OpenTelemetry.Internal; | ||
| using OpenTelemetry.Resources; | ||
| using OpenTelemetry.Trace; | ||
|
|
||
| namespace OpenTelemetry.Instrumentation.AWSLambda; | ||
|
|
||
| /// <summary> | ||
| /// Extension methods to simplify registering of AWS Lambda resource detectors. | ||
| /// </summary> | ||
| public static class AWSLambdaResourceBuilderExtensions | ||
| { | ||
| /// <summary> | ||
| /// Enables AWS Lambda resource detector. Do not call this method while also calling <see cref="TracerProviderBuilderExtensions.AddAWSLambdaConfigurations(TracerProviderBuilder)" /> or <see cref="TracerProviderBuilderExtensions.AddAWSLambdaConfigurations(TracerProviderBuilder, System.Action{AWSLambdaInstrumentationOptions})" />. | ||
| /// </summary> | ||
| /// <param name="builder">The <see cref="ResourceBuilder"/> being configured.</param> | ||
| /// <param name="configure">Optional callback action for configuring <see cref="AWSLambdaInstrumentationOptions"/>.</param> | ||
| /// <returns>The instance of <see cref="ResourceBuilder"/> being configured.</returns> | ||
| public static ResourceBuilder AddAWSLambdaDetector(this ResourceBuilder builder, Action<AWSLambdaInstrumentationOptions>? configure = null) | ||
|
||
| { | ||
| Guard.ThrowIfNull(builder); | ||
|
|
||
| var options = new AWSLambdaInstrumentationOptions(); | ||
| configure?.Invoke(options); | ||
|
|
||
| var semanticConventionBuilder = new AWSSemanticConventions(options.SemanticConventionVersion); | ||
|
|
||
| return builder.AddDetector(new AWSLambdaResourceDetector(semanticConventionBuilder)); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| // Copyright The OpenTelemetry Authors | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| using OpenTelemetry.Resources; | ||
| using Xunit; | ||
|
|
||
| namespace OpenTelemetry.Instrumentation.AWSLambda.Tests; | ||
|
|
||
| public class AWSLambdaResourceBuilderExtensionsTests : IDisposable | ||
| { | ||
| // Expected Semantic Conventions | ||
| private const string AttributeCloudProvider = "cloud.provider"; | ||
| private const string AttributeCloudRegion = "cloud.region"; | ||
| private const string AttributeFaasName = "faas.name"; | ||
| private const string AttributeFaasVersion = "faas.version"; | ||
| private const string AttributeFaasInstance = "faas.instance"; | ||
| private const string AttributeFaasMaxMemory = "faas.max_memory"; | ||
|
|
||
| private readonly IDisposable environmentScope = EnvironmentVariableScope.Create( | ||
| ("AWS_REGION", "us-east-1"), | ||
| ("AWS_LAMBDA_FUNCTION_NAME", "testfunction"), | ||
| ("AWS_LAMBDA_FUNCTION_VERSION", "latest"), | ||
| ("AWS_LAMBDA_FUNCTION_MEMORY_SIZE", "128"), | ||
| ("AWS_LAMBDA_LOG_STREAM_NAME", "2025/07/21/[$LATEST]7b176c212e954e62adfb9b5451cb5374")); | ||
|
|
||
| public void Dispose() => this.environmentScope.Dispose(); | ||
|
|
||
| [Fact] | ||
| public void AssertAttributes() | ||
| { | ||
| var resourceBuilder = ResourceBuilder.CreateDefault(); | ||
| resourceBuilder.AddAWSLambdaDetector(); | ||
|
|
||
| var resource = resourceBuilder.Build(); | ||
|
|
||
| var resourceAttributes = resource.Attributes | ||
| .ToDictionary(x => x.Key, x => x.Value); | ||
|
|
||
| Assert.Equal("aws", resourceAttributes[AttributeCloudProvider]); | ||
| Assert.Equal("us-east-1", resourceAttributes[AttributeCloudRegion]); | ||
| Assert.Equal("testfunction", resourceAttributes[AttributeFaasName]); | ||
| Assert.Equal("latest", resourceAttributes[AttributeFaasVersion]); | ||
| Assert.Equal("2025/07/21/[$LATEST]7b176c212e954e62adfb9b5451cb5374", | ||
| resourceAttributes[AttributeFaasInstance]); | ||
| Assert.Equal(134217728L, resourceAttributes[AttributeFaasMaxMemory]); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void AssertArgumentNullException() => | ||
| Assert.Throws<ArgumentNullException>(() => AWSLambdaResourceBuilderExtensions.AddAWSLambdaDetector(null!)); | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What happens if this is called when also adding the resource detector implicitly via the instrumentation (
opentelemetry-dotnet-contrib/src/OpenTelemetry.Instrumentation.AWSLambda/TracerProviderBuilderExtensions.cs
Line 46 in 55aa86e
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Its a valid point.
However just enhancing
TracerProviderBuilderExtensions.AddAWSLambdaConfigurationsto add only resource detection will not work in that case because the extension is exposed onTracerProviderBuilder. Resource detectors should be allowed to be added for logs and metrics too (similar to the other resource detectors) and be exposed on ResourceBuilder instead.I enhanced the XML docs to make it easier to understand how the 2 extensions are expected to be used, should make it clearer to avoid double resource detection.
Let me know what you think.