Skip to content

Conversation

lyra95
Copy link

@lyra95 lyra95 commented Oct 2, 2025

Fixes #6558

Changes

Update the constructor to augment the provided IConfiguration with environment variables before applying it.

Merge requirement checklist

  • CONTRIBUTING guidelines followed (license requirements, nullable enabled, static analysis, etc.)
  • Unit tests added/updated
  • Appropriate CHANGELOG.md files updated for non-trivial changes
  • Changes in public API reviewed (if applicable)

Copy link

linux-foundation-easycla bot commented Oct 2, 2025

CLA Signed

The committers listed above are authorized under a signed CLA.

@github-actions github-actions bot added the pkg:OpenTelemetry.Exporter.OpenTelemetryProtocol Issues related to OpenTelemetry.Exporter.OpenTelemetryProtocol NuGet package label Oct 2, 2025
@lyra95 lyra95 marked this pull request as ready for review October 2, 2025 10:11
@lyra95 lyra95 requested a review from a team as a code owner October 2, 2025 10:11
Copy link

codecov bot commented Oct 2, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 86.62%. Comparing base (5f66728) to head (dec8d51).
✅ All tests successful. No failed tests found.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #6559      +/-   ##
==========================================
- Coverage   86.63%   86.62%   -0.01%     
==========================================
  Files         258      258              
  Lines       11888    11892       +4     
==========================================
+ Hits        10299    10302       +3     
- Misses       1589     1590       +1     
Flag Coverage Δ
unittests-Project-Experimental 86.61% <100.00%> (+0.27%) ⬆️
unittests-Project-Stable 86.38% <100.00%> (-0.18%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
...orter.OpenTelemetryProtocol/OtlpExporterOptions.cs 99.12% <100.00%> (+0.03%) ⬆️

... and 1 file with indirect coverage changes

Copy link
Member

@martincostello martincostello left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The changes look reasonable.

Can you add a test that would fail without this change to protect against regressions in the future, and also update the CHANGELOG please?

Copy link
Contributor

@rajkumar-rangaraj rajkumar-rangaraj left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please take a look at #5586.. We should avoid adding AddEnvironmentVariables to the IConfiguration provider, since it's intended to be provided by the application.

@lyra95 lyra95 force-pushed the fix-envvar-picking branch from dec8d51 to 5201850 Compare October 3, 2025 06:51
@lyra95
Copy link
Author

lyra95 commented Oct 3, 2025

The changes look reasonable.

Can you add a test that would fail without this change to protect against regressions in the future, and also update the CHANGELOG please?

I’ve added tests and updated the CHANGELOG. I just noticed that this change also fixes #5586, so I included a test case for it as well.

@lyra95
Copy link
Author

lyra95 commented Oct 3, 2025

Please take a look at #5586.. We should avoid adding AddEnvironmentVariables to the IConfiguration provider, since it's intended to be provided by the application.

Instead of adding AddEnvironmentVariables, do you prefer OtlpExporterOptions to read them directly via Environment.GetEnvironmentVariable?

@lyra95 lyra95 force-pushed the fix-envvar-picking branch from 5201850 to a6574da Compare October 3, 2025 07:05
lyra95 added 7 commits October 3, 2025 16:44
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.
…rOptions

The internal constructor was only ever called from the public constructor, and the public constructor itself is only used in tests.
This made it easy to assume the internal constructor was part of normal runtime scenarios, which could cause confusion.
(in normal runtime scenarios, only the factory method `OtlpExporterOptions.CreateOtlpExporterOptions` is used)

Removing it simplifies the code and avoids misleading readers.
@lyra95 lyra95 force-pushed the fix-envvar-picking branch from a6574da to b5f8bce Compare October 3, 2025 07:44
@martincostello
Copy link
Member

Maybe another way to achieve the same end result is to make the IConfiguration field nullable and do something like this:

configuration ??= new ConfigurationBuilder().AddEnvironmentVariables().Build();
this.ApplyConfiguration(configuration, configurationType);

@lyra95
Copy link
Author

lyra95 commented Oct 3, 2025

Maybe another way to achieve the same end result is to make the IConfiguration field nullable and do something like this:

configuration ??= new ConfigurationBuilder().AddEnvironmentVariables().Build();
this.ApplyConfiguration(configuration, configurationType);

The issue is that if Host doesn’t load environment variables into IConfiguration (e.g. with Host.CreateEmptyApplicationBuilder), or if environment variables are set after the Host has already built IConfiguration, then OtlpExporterOptions won’t pick them up. This PR addresses that scenario, so the suggested change wouldn’t resolve the problem.

@martincostello
Copy link
Member

Does it not move the decision later in a way that is equivalent to before ("no IConfiguration was provided at all, so we create one with only environment variables in it")?

@lyra95
Copy link
Author

lyra95 commented Oct 3, 2025

The problematic case is "IConfiguration is provided, but no environment variables are in it".

Also, when docs says it is configurable via environment variables, I think the implementation should do so. (not "if environment variables are set and if Host is configured appropriately")

It would be ideal if OtlpExporterOptions reads directly from environment variables rather than from IConfigrutaion, but I think AddEnvironmentVariables to existing IConfiguration is an acceptable solution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pkg:OpenTelemetry.Exporter.OpenTelemetryProtocol Issues related to OpenTelemetry.Exporter.OpenTelemetryProtocol NuGet package
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[bug] OtlpExporterOptions is not picking up env variables
3 participants