open-telemetry / opentelemetry-dotnet-contrib

This repository contains set of components extending functionality of the OpenTelemetry .NET SDK. Instrumentation libraries, exporters, and other components can find their home here.
https://opentelemetry.io
Apache License 2.0
475 stars 283 forks source link

Adding AWS Lambda Configurations prevents adding resource attributes via environment variable #379

Open DanDanN00dle opened 2 years ago

DanDanN00dle commented 2 years ago

Issue with OpenTelemetry.Contrib.Instrumentation.AWSLambda

Using:

On runtime net6.0

Is this a feature request or a bug?

What is the expected behavior?

When setting the environment variable OTEL_RESOURCE_ATTRIBUTES for the lambda I expect to be able to add additional resource attributes to my lambda (per the specificaton).

What is the actual behavior?

The resource does not have the additional attributes specified in the environment variable.

        [Fact]
        public void TestResourceAdditionalAttributesFromEnvVar()
        {
            Environment.SetEnvironmentVariable("OTEL_RESOURCE_ATTRIBUTES", "application=TestApplication");

            using (var tracerProvider = Sdk.CreateTracerProviderBuilder()
                .AddAWSLambdaConfigurations()
                .Build())
            {
                var result = AWSLambdaWrapper.Trace(tracerProvider, this.sampleHandlers.SampleHandlerSyncReturn, "TestStream", this.sampleLambdaContext);
                var resource = tracerProvider.GetResource();
                this.AssertResourceAttributes(resource, new Dictionary<string, object> { ["application"] = "TestApplication" });
            }
        }

See test case.

mechie commented 1 year ago

I think this issue is caused by setting a new ResourceBuilder instead of modifying the existing one:

builder.SetResourceBuilder(ResourceBuilder
    .CreateEmpty()
    .AddTelemetrySdk()
    .AddAttributes(AWSLambdaResourceDetector.Detect()));

We can bypass by adding this line after .AddAWSLambdaConfigurations(), though I'm unsure whether there's other side effects from the original Sdk builder (it's a TracerProviderServiceCollectionBuilder instead of a plain ResourceBuilder, for starters):

.ConfigureResource((resource) => resource.AddEnvironmentVariableDetector())

Should AddAWSLambdaConfigurations ConfigureResource as well?

Oberon00 commented 1 year ago

I think this issue is why I originally created https://github.com/open-telemetry/opentelemetry-dotnet/issues/2909 PRs to switch the instrumentation to use this are welcome (an OTel upgrade might be necessary)