Skip to content

[bug] EnvironmentVariableDetector does not load variables when configured within HostBuilder #6789

@maciejaszyk

Description

@maciejaszyk

Package

OpenTelemetry

Package Version

Package Name Version
OpenTelemetry.Api 1.14.0

Runtime Version

net8.0, net10.0

Description

The default ResourceBuilder adds EnvironmentVariableDetector, which should add resource attributes from environment variables (such as OTEL_RESOURCE_ATTRIBUTES). However, when OpenTelemetry is configured within a HostBuilder, these environment variables are ignored.

The issue appears to be located here:

Lazy<IConfiguration> configuration = new Lazy<IConfiguration>(() => new ConfigurationBuilder().AddEnvironmentVariables().Build());
#pragma warning disable CA1062 // Validate arguments of public methods - needed for netstandard2.1
return resourceBuilder
#pragma warning restore CA1062 // Validate arguments of public methods - needed for netstandard2.1
.AddDetectorInternal(sp => new OtelEnvResourceDetector(sp?.GetService<IConfiguration>() ?? configuration.Value))

sp?.GetService<IConfiguration>() will return a value and

         Lazy<IConfiguration> configuration = new Lazy<IConfiguration>(() => new ConfigurationBuilder().AddEnvironmentVariables().Build()); 

will not be evaluated.

Steps to Reproduce

   [Fact]
    public void OTelAttributesAreAddedToTheMeterProvider()
    {
        Environment.SetEnvironmentVariable("OTEL_RESOURCE_ATTRIBUTES", "mycustom=test,other=test");

        var builder = new HostBuilder()
            .ConfigureAppConfiguration(builder =>
            {
                builder.AddInMemoryCollection(new Dictionary<string, string?>
                {
                    ["SomeUnrelatedKey"] = "SomeUnrelatedValue",
                });
            })
            .ConfigureServices(services =>
            {
                services.AddOpenTelemetry()
                    .WithMetrics(builder =>
                    {
                        var resources = ResourceBuilder.CreateDefault();
                        resources.AddService("test", "namespace", "version", false, "my_instance");
                        var attributes = resources.Build().Attributes.ToArray();
                        Assert.Contains(new KeyValuePair<string, object>("service.name", "test"), attributes);
                        Assert.Contains(new KeyValuePair<string, object>("mycustom", "test"), attributes);
                        Assert.Contains(new KeyValuePair<string, object>("other", "test"), attributes);
                        builder.SetResourceBuilder(resources);
                    });
            });

        using var host = builder.Build();
        var meterProvider = host.Services.GetRequiredService<MeterProvider>();
        Assert.IsType<MeterProviderSdk>(meterProvider);
        var attributes = ((MeterProviderSdk)meterProvider).Resource.Attributes.ToArray();
        Assert.Contains(new KeyValuePair<string, object>("service.name", "test"), attributes);
        Assert.Contains(new KeyValuePair<string, object>("mycustom", "test"), attributes);
        Assert.Contains(new KeyValuePair<string, object>("other", "test"), attributes);
    }
Assert.Contains() Failure: Item not found in collection
Collection: [["service.name"] = "test", ["service.namespace"] = "namespace", ["service.version"] = "version", ["service.instance.id"] = "my_instance", ["telemetry.sdk.name"] = "opentelemetry", ···]
Not found:  ["mycustom"] = "test"

Expected Result

I expect the attributes from OTEL_RESOURCE_ATTRIBUTES to be added to the resource attributes.

Actual Result

Environment variables are not loaded into the MeterProvider.

Additional Context

No response

Tip

React with 👍 to help prioritize this issue. Please use comments to provide useful context, avoiding +1 or me too, to help us triage it. Learn more here.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingneeds-triageNew issues which have not been classified or triaged by a community memberpkg:OpenTelemetryIssues related to OpenTelemetry NuGet package

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions