Skip to content

Commit f723800

Browse files
committed
Fix: Make OtlpExporterOptions pick up env variables
When environment variables are set after IConfiguration is built (during WebHost building), even though they are set before exporters are configured, OtlpExporterOptions does not pick them up. This patch fixes the issue.
1 parent 5f66728 commit f723800

File tree

2 files changed

+7
-1
lines changed

2 files changed

+7
-1
lines changed

src/OpenTelemetry.Exporter.OpenTelemetryProtocol/Implementation/OtlpSpecConfigDefinitions.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ namespace OpenTelemetry.Exporter;
1111
/// </remarks>
1212
internal static class OtlpSpecConfigDefinitions
1313
{
14+
public const string EnvVarPrefix = "OTEL_";
15+
1416
public const string DefaultEndpointEnvVarName = "OTEL_EXPORTER_OTLP_ENDPOINT";
1517
public const string DefaultHeadersEnvVarName = "OTEL_EXPORTER_OTLP_HEADERS";
1618
public const string DefaultTimeoutEnvVarName = "OTEL_EXPORTER_OTLP_TIMEOUT";

src/OpenTelemetry.Exporter.OpenTelemetryProtocol/OtlpExporterOptions.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,12 @@ internal OtlpExporterOptions(
6767
BatchExportActivityProcessorOptions defaultBatchOptions)
6868
{
6969
Debug.Assert(defaultBatchOptions != null, "defaultBatchOptions was null");
70+
var finalConfiguration = new ConfigurationBuilder()
71+
.AddEnvironmentVariables(OtlpSpecConfigDefinitions.EnvVarPrefix)
72+
.AddConfiguration(configuration)
73+
.Build();
7074

71-
this.ApplyConfiguration(configuration, configurationType);
75+
this.ApplyConfiguration(finalConfiguration, configurationType);
7276

7377
this.DefaultHttpClientFactory = () =>
7478
{

0 commit comments

Comments
 (0)