[Tests] Use EnvironmentRestorer to prevent env var leaks#8388
[Tests] Use EnvironmentRestorer to prevent env var leaks#8388lucaspimentel wants to merge 4 commits intomasterfrom
EnvironmentRestorer to prevent env var leaks#8388Conversation
Replace manual save/restore patterns with the [EnvironmentRestorer] attribute to prevent env var leaks between tests. Also allow the attribute on methods (not just classes) for more targeted usage. 🤖 Co-Authored-By: Claude Code <noreply@anthropic.com>
EnvironmentRestorer to prevent env var leaks
BenchmarksBenchmark execution time: 2026-03-30 21:49:45 Comparing candidate commit c29d481 in PR branch Found 10 performance improvements and 9 performance regressions! Performance is the same for 253 metrics, 16 unstable metrics.
|
tracer/test/Datadog.Trace.Tests/RuntimeMetrics/AzurePerformanceCountersListenerTests.cs
Outdated
Show resolved
Hide resolved
Execution-Time Benchmarks Report ⏱️Execution-time results for samples comparing This PR (8388) and master. ✅ No regressions detected - check the details below Full Metrics ComparisonFakeDbCommand
HttpMessageHandler
Comparison explanationExecution-time benchmarks measure the whole time it takes to execute a program, and are intended to measure the one-off costs. Cases where the execution time results for the PR are worse than latest master results are highlighted in **red**. The following thresholds were used for comparing the execution times:
Note that these results are based on a single point-in-time result for each branch. For full results, see the dashboard. Graphs show the p99 interval based on the mean and StdDev of the test run, as well as the mean value of the run (shown as a diamond below the graph). Duration chartsFakeDbCommand (.NET Framework 4.8)gantt
title Execution time (ms) FakeDbCommand (.NET Framework 4.8)
dateFormat x
axisFormat %Q
todayMarker off
section Baseline
This PR (8388) - mean (72ms) : 69, 74
master - mean (72ms) : 70, 74
section Bailout
This PR (8388) - mean (76ms) : 74, 78
master - mean (76ms) : 74, 78
section CallTarget+Inlining+NGEN
This PR (8388) - mean (1,073ms) : 1032, 1114
master - mean (1,078ms) : 1030, 1127
FakeDbCommand (.NET Core 3.1)gantt
title Execution time (ms) FakeDbCommand (.NET Core 3.1)
dateFormat x
axisFormat %Q
todayMarker off
section Baseline
This PR (8388) - mean (112ms) : 108, 115
master - mean (112ms) : 108, 116
section Bailout
This PR (8388) - mean (114ms) : 111, 116
master - mean (113ms) : 110, 116
section CallTarget+Inlining+NGEN
This PR (8388) - mean (789ms) : 767, 812
master - mean (795ms) : 778, 812
FakeDbCommand (.NET 6)gantt
title Execution time (ms) FakeDbCommand (.NET 6)
dateFormat x
axisFormat %Q
todayMarker off
section Baseline
This PR (8388) - mean (99ms) : 96, 103
master - mean (100ms) : 95, 105
section Bailout
This PR (8388) - mean (100ms) : 98, 102
master - mean (100ms) : 98, 103
section CallTarget+Inlining+NGEN
This PR (8388) - mean (944ms) : 908, 980
master - mean (948ms) : 907, 989
FakeDbCommand (.NET 8)gantt
title Execution time (ms) FakeDbCommand (.NET 8)
dateFormat x
axisFormat %Q
todayMarker off
section Baseline
This PR (8388) - mean (98ms) : 96, 101
master - mean (98ms) : 95, 102
section Bailout
This PR (8388) - mean (99ms) : 97, 102
master - mean (100ms) : 97, 102
section CallTarget+Inlining+NGEN
This PR (8388) - mean (830ms) : 793, 867
master - mean (829ms) : 795, 862
HttpMessageHandler (.NET Framework 4.8)gantt
title Execution time (ms) HttpMessageHandler (.NET Framework 4.8)
dateFormat x
axisFormat %Q
todayMarker off
section Baseline
This PR (8388) - mean (206ms) : 201, 212
master - mean (214ms) : 209, 219
section Bailout
This PR (8388) - mean (212ms) : 207, 216
master - mean (217ms) : 211, 223
section CallTarget+Inlining+NGEN
This PR (8388) - mean (1,216ms) : 1167, 1266
master - mean (1,258ms) : 1211, 1305
HttpMessageHandler (.NET Core 3.1)gantt
title Execution time (ms) HttpMessageHandler (.NET Core 3.1)
dateFormat x
axisFormat %Q
todayMarker off
section Baseline
This PR (8388) - mean (297ms) : 289, 305
master - mean (310ms) : 303, 316
section Bailout
This PR (8388) - mean (298ms) : 290, 305
master - mean (312ms) : 305, 320
section CallTarget+Inlining+NGEN
This PR (8388) - mean (1,000ms) : 966, 1033
master - mean (1,035ms) : 1001, 1070
HttpMessageHandler (.NET 6)gantt
title Execution time (ms) HttpMessageHandler (.NET 6)
dateFormat x
axisFormat %Q
todayMarker off
section Baseline
This PR (8388) - mean (291ms) : 284, 298
master - mean (305ms) : 296, 315
section Bailout
This PR (8388) - mean (292ms) : 285, 300
master - mean (304ms) : 298, 310
section CallTarget+Inlining+NGEN
This PR (8388) - mean (1,188ms) : 1149, 1226
master - mean (1,205ms) : 1169, 1242
HttpMessageHandler (.NET 8)gantt
title Execution time (ms) HttpMessageHandler (.NET 8)
dateFormat x
axisFormat %Q
todayMarker off
section Baseline
This PR (8388) - mean (290ms) : 284, 296
master - mean (301ms) : 291, 311
section Bailout
This PR (8388) - mean (291ms) : 285, 298
master - mean (302ms) : 294, 310
section CallTarget+Inlining+NGEN
This PR (8388) - mean (1,075ms) : 999, 1151
master - mean (1,127ms) : 1013, 1241
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
…eCountersListenerTests.cs Co-authored-by: Andrew Lock <andrew.lock@datadoghq.com>
🤖 Co-Authored-By: Claude Code <noreply@anthropic.com>
🤖 Co-Authored-By: Claude Code <noreply@anthropic.com>
Summary of changes
Add
[EnvironmentRestorer]attribute to test classes/methods that callEnvironment.SetEnvironmentVariablewithout proper cleanup, preventing environment variable leaks between tests.Reason for change
Several test files were setting environment variables without restoring them, which could cause flaky or order-dependent test failures due to leaked state.
Implementation details
EnvironmentRestorerAttribute: Allow usage on methods (AttributeTargets.Method) in addition to classesTelemetrySettingsAgentlessSettingsTests: Replace manualIDisposablesave/restore with class-level[EnvironmentRestorer]AzurePerformanceCountersListenerTests: Add class-level[EnvironmentRestorer("WEBSITE_COUNTERS_CLR")](previously had no restore at all)LegacyCommandLineArgumentsTests.SetCi: Add method-level[EnvironmentRestorer("TF_BUILD")], remove manual try/finallyConfigureCiCommandTests.ConfigureCi: Add method-level[EnvironmentRestorer("GITHUB_ENV")], remove manual save/restoreAutodetectCikeeps its manual try/finally because it clears all environment variables, which the attribute can't handleTest coverage
Existing tests cover the affected functionality. No new tests needed — this is a cleanup of test infrastructure.
Other details