Skip to content

Conversation

@cjihrig
Copy link
Contributor

@cjihrig cjihrig commented Oct 13, 2025

Which problem is this PR solving?

The BasicTracerProvider tests currently clean up their modified environment variables at the end of each test. This is unreliable because if a given test fails, the clean up code is not run, and can leak environment variables into surrounding tests.

Fixes # N/A

Short description of the changes

This commit updates the BasicTracerProvider tests to ensure that modified environment variables are cleaned up between tests.

Type of change

  • Bug fix (non-breaking change which fixes an issue)

How Has This Been Tested?

  • Existing test suite.

Checklist:

  • Followed the style guidelines of this project
  • Unit tests have been added
  • Documentation has been updated

…ween tests

The BasicTracerProvider tests currently clean up their modified
environment variables at the end of each test. This is unreliable
because if a given test fails, the clean up code is not run, and
can leak environment variables into surrounding tests.

This commit updates the BasicTracerProvider tests to ensure that
modified environment variables are cleaned up between tests.
@cjihrig cjihrig requested a review from a team as a code owner October 13, 2025 20:25
@codecov
Copy link

codecov bot commented Oct 13, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 95.00%. Comparing base (1dc69a6) to head (71f3d0a).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #6011   +/-   ##
=======================================
  Coverage   95.00%   95.00%           
=======================================
  Files         313      313           
  Lines        8771     8771           
  Branches     1883     1883           
=======================================
  Hits         8333     8333           
  Misses        438      438           
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Copy link
Contributor

@david-luna david-luna left a comment

Choose a reason for hiding this comment

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

@cjihrig thanks for contributing :)

I left a comment. Let me know what you think.

describe('constructor', () => {
describe('spanLimits', () => {
describe('when attribute value length limit is defined via env', () => {
afterEach(function () {
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: when tests of this file are finished these env vars will be deleted but we are not certain if that was the env state before executing this file. following the spirit of this PR we could have a beforeEach block to stash the values in a couple of local vars and then restore these values in the afterEach

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the review @david-luna. I'm fine with making whichever changes you want. I've noticed that environment variables in tests are handled inconsistently across the project (which is kind of expected in such a large project). For example:

  • Some unconditionally delete any environment variables used by the tests in an afterEach() hook. That is the style I copied here.
  • Some delete the environment variables at the end of the test body. This is probably the least good approach since the cleanup might never run if the test fails.
  • Some make an effort to completely restore the environment. This is the most complete approach IMO, but does have its own drawbacks - see test(opentelemetry-configuration): preserve special process.env behavior #6010. This probably has the biggest performance hit as well, but probably is not an issue in practice. Also, if the approach from test(opentelemetry-configuration): preserve special process.env behavior #6010 were to be adopted across other packages, it would probably make sense to put it in some centralized place, but I'm not sure where those types of test helpers would live in the OTel project.
  • There may be some other approaches I'm missing here.

Thoughts?

Copy link
Contributor

Choose a reason for hiding this comment

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

On the examples you provided I think we should avoid the second option (deleting env vars at the end of the test). I also think the performance is not an issue but having to create a package or similar to share this logic across all packages for testing seems a bit overkill. A comment about it when there is a PR updating tests would gradually move the entire codebase to have the same approach regarding env.

I'd say let's go for your option and review it in a while :)

@david-luna david-luna added this pull request to the merge queue Oct 15, 2025
Merged via the queue into open-telemetry:main with commit 5f51a37 Oct 15, 2025
25 checks passed
@otelbot
Copy link
Contributor

otelbot bot commented Oct 15, 2025

Thank you for your contribution @cjihrig! 🎉 We would like to hear from you about your experience contributing to OpenTelemetry by taking a few minutes to fill out this survey.

@cjihrig cjihrig deleted the basic-trace-provider branch October 15, 2025 14:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants