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.
Package
OpenTelemetry
Package Version
Runtime Version
net8.0, net10.0
Description
The default
ResourceBuilderaddsEnvironmentVariableDetector, which should add resource attributes from environment variables (such asOTEL_RESOURCE_ATTRIBUTES). However, whenOpenTelemetryis configured within aHostBuilder, these environment variables are ignored.The issue appears to be located here:
opentelemetry-dotnet/src/OpenTelemetry/Resources/ResourceBuilderExtensions.cs
Lines 118 to 123 in 58519b7
sp?.GetService<IConfiguration>()will return a value andwill not be evaluated.
Steps to Reproduce
Expected Result
I expect the attributes from
OTEL_RESOURCE_ATTRIBUTESto 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
+1orme too, to help us triage it. Learn more here.