dd-trace-dotnet icon indicating copy to clipboard operation
dd-trace-dotnet copied to clipboard

[Config Registry] 6/7 Forbid use of System.Environment methods and adapt everywhere

Open anna-git opened this issue 3 weeks ago • 2 comments

Context

Part of Configuration Inversion (Step 6) - Stack progress:

  1. #7548 - Add GitLab step and JSON configuration file
  2. #7688 - Cleanup configuration / platform keys + source generator
  3. #7698 - Aliases handling via source generator
  4. #7689 - Analyzers for platform and ConfigurationBuilder
  5. #7931 - Replace manual ConfigurationKeys by generated ones in the whole solution
  6. #7932 - Forbid use of System.Environment methods and adapt everywhere (this PR)
  7. #7937 - Integration names to generated keys

I'll update the PR summary to mention the YAML documentation file:

Summary of changes

Banned direct System.Environment.GetEnvironmentVariable() usage and migrated all environment variable access to use EnvironmentHelpers with strongly-typed ConfigurationKeys and PlatformKeys constants.

Key changes:

  • Banned System.Environment.GetEnvironmentVariable() via BannedApiAnalyzers
  • Added EnvironmentGetEnvironmentVariableAnalyzer (DD0009) to enforce ConfigurationKeys/PlatformKeys usage only
  • Nested PlatformKeys by category (Ci, Aws, AzureAppService, ServiceFabric, DotNet)
  • Migrated ~50+ files across CI Visibility, AWS Lambda, AppSec, Telemetry, Profiler, and Agent components
  • Added missing keys to supported-configurations.json and their documentation to supported-configurations-docs.yaml

Reason for change

Direct System.Environment.GetEnvironmentVariable() calls with string literals are error-prone. Centralizing through EnvironmentHelpers with strongly-typed constants provides compile-time validation, discoverability, and refactoring safety.

Implementation details

  1. Banned API enforcement - Added BannedSymbols.txt and configured .editorconfig to treat RS0030 as error (vendored code excluded)
  2. Custom analyzer - DD0009 validates all EnvironmentHelpers calls accept only ConfigurationKeys/PlatformKeys constants, rejecting hardcoded strings
  3. PlatformKeys organization - Nested by category for better discoverability
  4. EnvironmentHelpers refactoring - Added overloads for nested class constants, maintained backward compatibility
  5. Configuration documentation - Added missing configuration keys to supported-configurations.json and comprehensive XML documentation to supported-configurations-docs.yaml, which the source generator uses to generate XML doc comments in ConfigurationKeys classes

Test coverage

  • ✅ Comprehensive analyzer tests covering valid/invalid scenarios
  • ✅ All existing unit tests pass
  • ✅ Banned API analyzer correctly flags direct System.Environment usage

Other details

+2,600/-940 lines. No breaking changes, negligible performance impact.

anna-git avatar Dec 09 '25 15:12 anna-git

Execution-Time Benchmarks Report :stopwatch:

Execution-time results for samples comparing This PR (7932) and master.

✅ No regressions detected - check the details below

Full Metrics Comparison

FakeDbCommand

Metric Master (Mean ± 95% CI) Current (Mean ± 95% CI) Change Status
.NET Framework 4.8 - Baseline
duration68.24 ± (68.23 - 68.42) ms68.26 ± (68.24 - 68.43) ms+0.0%✅⬆️
.NET Framework 4.8 - Bailout
duration71.98 ± (71.93 - 72.16) ms72.08 ± (72.00 - 72.23) ms+0.1%✅⬆️
.NET Framework 4.8 - CallTarget+Inlining+NGEN
duration1004.14 ± (1010.66 - 1020.72) ms1001.57 ± (1004.49 - 1011.87) ms-0.3%
.NET Core 3.1 - Baseline
process.internal_duration_ms22.02 ± (21.98 - 22.06) ms21.93 ± (21.89 - 21.97) ms-0.4%
process.time_to_main_ms78.78 ± (78.61 - 78.95) ms78.86 ± (78.68 - 79.03) ms+0.1%✅⬆️
runtime.dotnet.exceptions.count0 ± (0 - 0)0 ± (0 - 0)+0.0%
runtime.dotnet.mem.committed10.91 ± (10.90 - 10.91) MB10.91 ± (10.90 - 10.91) MB+0.0%✅⬆️
runtime.dotnet.threads.count12 ± (12 - 12)12 ± (12 - 12)+0.0%
.NET Core 3.1 - Bailout
process.internal_duration_ms21.93 ± (21.91 - 21.96) ms21.88 ± (21.85 - 21.92) ms-0.2%
process.time_to_main_ms79.86 ± (79.78 - 79.95) ms80.07 ± (79.95 - 80.18) ms+0.3%✅⬆️
runtime.dotnet.exceptions.count0 ± (0 - 0)0 ± (0 - 0)+0.0%
runtime.dotnet.mem.committed10.94 ± (10.94 - 10.95) MB10.95 ± (10.95 - 10.95) MB+0.1%✅⬆️
runtime.dotnet.threads.count13 ± (13 - 13)13 ± (13 - 13)+0.0%
.NET Core 3.1 - CallTarget+Inlining+NGEN
process.internal_duration_ms210.36 ± (208.88 - 211.85) ms210.41 ± (209.05 - 211.77) ms+0.0%✅⬆️
process.time_to_main_ms471.25 ± (470.64 - 471.87) ms472.06 ± (471.44 - 472.68) ms+0.2%✅⬆️
runtime.dotnet.exceptions.count0 ± (0 - 0)0 ± (0 - 0)+0.0%
runtime.dotnet.mem.committed48.09 ± (48.07 - 48.12) MB48.06 ± (48.04 - 48.08) MB-0.1%
runtime.dotnet.threads.count28 ± (28 - 28)28 ± (28 - 28)+0.0%
.NET 6 - Baseline
process.internal_duration_ms20.79 ± (20.77 - 20.82) ms20.66 ± (20.63 - 20.68) ms-0.7%
process.time_to_main_ms68.40 ± (68.26 - 68.54) ms68.02 ± (67.93 - 68.12) ms-0.5%
runtime.dotnet.exceptions.count0 ± (0 - 0)0 ± (0 - 0)+0.0%
runtime.dotnet.mem.committed10.59 ± (10.58 - 10.59) MB10.63 ± (10.63 - 10.64) MB+0.4%✅⬆️
runtime.dotnet.threads.count10 ± (10 - 10)10 ± (10 - 10)+0.0%
.NET 6 - Bailout
process.internal_duration_ms20.72 ± (20.70 - 20.75) ms20.56 ± (20.54 - 20.58) ms-0.8%
process.time_to_main_ms69.16 ± (69.10 - 69.23) ms68.88 ± (68.84 - 68.93) ms-0.4%
runtime.dotnet.exceptions.count0 ± (0 - 0)0 ± (0 - 0)+0.0%
runtime.dotnet.mem.committed10.71 ± (10.71 - 10.72) MB10.68 ± (10.67 - 10.68) MB-0.3%
runtime.dotnet.threads.count11 ± (11 - 11)11 ± (11 - 11)+0.0%
.NET 6 - CallTarget+Inlining+NGEN
process.internal_duration_ms200.41 ± (199.51 - 201.32) ms200.06 ± (198.89 - 201.23) ms-0.2%
process.time_to_main_ms440.66 ± (439.87 - 441.44) ms436.91 ± (436.40 - 437.43) ms-0.8%
runtime.dotnet.exceptions.count0 ± (0 - 0)0 ± (0 - 0)+0.0%
runtime.dotnet.mem.committed48.45 ± (48.40 - 48.50) MB48.24 ± (48.16 - 48.32) MB-0.4%
runtime.dotnet.threads.count28 ± (28 - 28)28 ± (28 - 28)+0.0%✅⬆️
.NET 8 - Baseline
process.internal_duration_ms18.82 ± (18.79 - 18.85) ms18.80 ± (18.78 - 18.83) ms-0.1%
process.time_to_main_ms67.21 ± (67.10 - 67.31) ms66.97 ± (66.88 - 67.07) ms-0.3%
runtime.dotnet.exceptions.count0 ± (0 - 0)0 ± (0 - 0)+0.0%
runtime.dotnet.mem.committed7.65 ± (7.65 - 7.66) MB7.69 ± (7.68 - 7.70) MB+0.4%✅⬆️
runtime.dotnet.threads.count10 ± (10 - 10)10 ± (10 - 10)+0.0%
.NET 8 - Bailout
process.internal_duration_ms18.70 ± (18.68 - 18.73) ms18.78 ± (18.76 - 18.81) ms+0.4%✅⬆️
process.time_to_main_ms68.06 ± (68.01 - 68.11) ms68.07 ± (68.02 - 68.12) ms+0.0%✅⬆️
runtime.dotnet.exceptions.count0 ± (0 - 0)0 ± (0 - 0)+0.0%
runtime.dotnet.mem.committed7.72 ± (7.71 - 7.73) MB7.75 ± (7.74 - 7.77) MB+0.5%✅⬆️
runtime.dotnet.threads.count11 ± (11 - 11)11 ± (11 - 11)+0.0%
.NET 8 - CallTarget+Inlining+NGEN
process.internal_duration_ms179.05 ± (178.19 - 179.90) ms179.32 ± (178.22 - 180.42) ms+0.2%✅⬆️
process.time_to_main_ms422.60 ± (422.13 - 423.07) ms423.51 ± (422.90 - 424.12) ms+0.2%✅⬆️
runtime.dotnet.exceptions.count0 ± (0 - 0)0 ± (0 - 0)+0.0%
runtime.dotnet.mem.committed36.31 ± (36.28 - 36.34) MB36.31 ± (36.28 - 36.35) MB+0.0%✅⬆️
runtime.dotnet.threads.count27 ± (27 - 27)27 ± (27 - 27)-0.1%

HttpMessageHandler

Metric Master (Mean ± 95% CI) Current (Mean ± 95% CI) Change Status
.NET Framework 4.8 - Baseline
duration193.39 ± (193.35 - 194.00) ms193.10 ± (193.33 - 194.09) ms-0.2%
.NET Framework 4.8 - Bailout
duration196.97 ± (196.95 - 197.77) ms196.81 ± (196.75 - 197.27) ms-0.1%
.NET Framework 4.8 - CallTarget+Inlining+NGEN
duration1111.82 ± (1115.44 - 1124.23) ms1112.76 ± (1119.34 - 1129.64) ms+0.1%✅⬆️
.NET Core 3.1 - Baseline
process.internal_duration_ms189.44 ± (189.00 - 189.88) ms187.79 ± (187.38 - 188.20) ms-0.9%
process.time_to_main_ms80.86 ± (80.65 - 81.08) ms81.02 ± (80.78 - 81.26) ms+0.2%✅⬆️
runtime.dotnet.exceptions.count3 ± (3 - 3)3 ± (3 - 3)+0.0%
runtime.dotnet.mem.committed16.08 ± (16.06 - 16.11) MB16.16 ± (16.14 - 16.19) MB+0.5%✅⬆️
runtime.dotnet.threads.count20 ± (20 - 20)20 ± (20 - 20)+0.5%✅⬆️
.NET Core 3.1 - Bailout
process.internal_duration_ms187.26 ± (187.02 - 187.50) ms188.11 ± (187.79 - 188.43) ms+0.5%✅⬆️
process.time_to_main_ms82.11 ± (81.95 - 82.27) ms82.31 ± (82.16 - 82.47) ms+0.3%✅⬆️
runtime.dotnet.exceptions.count3 ± (3 - 3)3 ± (3 - 3)+0.0%
runtime.dotnet.mem.committed16.17 ± (16.14 - 16.19) MB16.22 ± (16.19 - 16.25) MB+0.3%✅⬆️
runtime.dotnet.threads.count21 ± (21 - 21)21 ± (21 - 21)+0.5%✅⬆️
.NET Core 3.1 - CallTarget+Inlining+NGEN
process.internal_duration_ms398.17 ± (395.26 - 401.08) ms398.02 ± (394.57 - 401.47) ms-0.0%
process.time_to_main_ms474.49 ± (473.87 - 475.11) ms473.91 ± (473.04 - 474.78) ms-0.1%
runtime.dotnet.exceptions.count3 ± (3 - 3)3 ± (3 - 3)+0.0%
runtime.dotnet.mem.committed58.08 ± (57.93 - 58.24) MB58.71 ± (58.55 - 58.87) MB+1.1%✅⬆️
runtime.dotnet.threads.count29 ± (29 - 29)29 ± (29 - 30)+0.1%✅⬆️
.NET 6 - Baseline
process.internal_duration_ms192.09 ± (191.70 - 192.47) ms194.45 ± (193.95 - 194.95) ms+1.2%✅⬆️
process.time_to_main_ms69.83 ± (69.64 - 70.01) ms70.48 ± (70.29 - 70.68) ms+0.9%✅⬆️
runtime.dotnet.exceptions.count4 ± (4 - 4)4 ± (4 - 4)+0.0%
runtime.dotnet.mem.committed16.20 ± (16.08 - 16.32) MB16.33 ± (16.31 - 16.36) MB+0.8%✅⬆️
runtime.dotnet.threads.count19 ± (18 - 19)19 ± (19 - 19)+2.2%✅⬆️
.NET 6 - Bailout
process.internal_duration_ms191.49 ± (191.16 - 191.82) ms193.58 ± (193.15 - 194.01) ms+1.1%✅⬆️
process.time_to_main_ms70.81 ± (70.70 - 70.92) ms71.58 ± (71.41 - 71.76) ms+1.1%✅⬆️
runtime.dotnet.exceptions.count4 ± (4 - 4)4 ± (4 - 4)+0.0%
runtime.dotnet.mem.committed16.27 ± (16.16 - 16.39) MB16.31 ± (16.24 - 16.39) MB+0.2%✅⬆️
runtime.dotnet.threads.count19 ± (19 - 20)20 ± (19 - 20)+1.5%✅⬆️
.NET 6 - CallTarget+Inlining+NGEN
process.internal_duration_ms411.39 ± (408.89 - 413.89) ms412.91 ± (410.58 - 415.25) ms+0.4%✅⬆️
process.time_to_main_ms445.57 ± (444.98 - 446.16) ms450.02 ± (449.24 - 450.79) ms+1.0%✅⬆️
runtime.dotnet.exceptions.count4 ± (4 - 4)4 ± (4 - 4)+0.0%
runtime.dotnet.mem.committed59.30 ± (59.19 - 59.42) MB59.18 ± (59.05 - 59.30) MB-0.2%
runtime.dotnet.threads.count30 ± (29 - 30)29 ± (29 - 30)-0.1%
.NET 8 - Baseline
process.internal_duration_ms190.85 ± (190.48 - 191.21) ms190.53 ± (190.24 - 190.81) ms-0.2%
process.time_to_main_ms69.52 ± (69.35 - 69.70) ms69.34 ± (69.19 - 69.49) ms-0.3%
runtime.dotnet.exceptions.count4 ± (4 - 4)4 ± (4 - 4)+0.0%
runtime.dotnet.mem.committed11.73 ± (11.70 - 11.76) MB11.70 ± (11.67 - 11.73) MB-0.2%
runtime.dotnet.threads.count18 ± (18 - 18)18 ± (18 - 18)+0.2%✅⬆️
.NET 8 - Bailout
process.internal_duration_ms189.72 ± (189.46 - 189.97) ms190.07 ± (189.82 - 190.31) ms+0.2%✅⬆️
process.time_to_main_ms70.35 ± (70.26 - 70.44) ms70.65 ± (70.51 - 70.79) ms+0.4%✅⬆️
runtime.dotnet.exceptions.count4 ± (4 - 4)4 ± (4 - 4)+0.0%
runtime.dotnet.mem.committed11.86 ± (11.83 - 11.89) MB11.78 ± (11.75 - 11.81) MB-0.7%
runtime.dotnet.threads.count19 ± (19 - 19)19 ± (19 - 19)-0.2%
.NET 8 - CallTarget+Inlining+NGEN
process.internal_duration_ms365.97 ± (364.58 - 367.35) ms365.41 ± (364.06 - 366.75) ms-0.2%
process.time_to_main_ms427.54 ± (426.84 - 428.25) ms428.45 ± (427.93 - 428.96) ms+0.2%✅⬆️
runtime.dotnet.exceptions.count4 ± (4 - 4)4 ± (4 - 4)+0.0%
runtime.dotnet.mem.committed47.98 ± (47.95 - 48.01) MB47.96 ± (47.94 - 47.98) MB-0.0%
runtime.dotnet.threads.count29 ± (29 - 29)29 ± (29 - 29)-0.1%
Comparison explanation

Execution-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:

  • Welch test with statistical test for significance of 5%
  • Only results indicating a difference greater than 5% and 5 ms are considered.

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 charts
FakeDbCommand (.NET Framework 4.8)
gantt
    title Execution time (ms) FakeDbCommand (.NET Framework 4.8)
    dateFormat  x
    axisFormat %Q
    todayMarker off
    section Baseline
    This PR (7932) - mean (68ms)  : 67, 70
    master - mean (68ms)  : 67, 70

    section Bailout
    This PR (7932) - mean (72ms)  : 71, 73
    master - mean (72ms)  : 71, 73

    section CallTarget+Inlining+NGEN
    This PR (7932) - mean (1,008ms)  : 955, 1061
    master - mean (1,016ms)  : 942, 1090

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 (7932) - mean (106ms)  : 102, 110
    master - mean (106ms)  : 104, 108

    section Bailout
    This PR (7932) - mean (107ms)  : 105, 109
    master - mean (107ms)  : 106, 108

    section CallTarget+Inlining+NGEN
    This PR (7932) - mean (709ms)  : 681, 738
    master - mean (710ms)  : 683, 736

FakeDbCommand (.NET 6)
gantt
    title Execution time (ms) FakeDbCommand (.NET 6)
    dateFormat  x
    axisFormat %Q
    todayMarker off
    section Baseline
    This PR (7932) - mean (93ms)  : 92, 95
    master - mean (94ms)  : 92, 96

    section Bailout
    This PR (7932) - mean (94ms)  : 93, 95
    master - mean (95ms)  : 93, 96

    section CallTarget+Inlining+NGEN
    This PR (7932) - mean (664ms)  : 646, 682
    master - mean (668ms)  : 635, 701

FakeDbCommand (.NET 8)
gantt
    title Execution time (ms) FakeDbCommand (.NET 8)
    dateFormat  x
    axisFormat %Q
    todayMarker off
    section Baseline
    This PR (7932) - mean (92ms)  : 90, 94
    master - mean (92ms)  : 90, 94

    section Bailout
    This PR (7932) - mean (93ms)  : 91, 94
    master - mean (93ms)  : 91, 94

    section CallTarget+Inlining+NGEN
    This PR (7932) - mean (630ms)  : 616, 644
    master - mean (629ms)  : 614, 644

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 (7932) - mean (194ms)  : 190, 198
    master - mean (194ms)  : 190, 197

    section Bailout
    This PR (7932) - mean (197ms)  : 195, 199
    master - mean (197ms)  : 193, 201

    section CallTarget+Inlining+NGEN
    This PR (7932) - mean (1,124ms)  : 1046, 1203
    master - mean (1,120ms)  : 1054, 1186

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 (7932) - mean (278ms)  : 271, 285
    master - mean (279ms)  : 270, 288

    section Bailout
    This PR (7932) - mean (279ms)  : 274, 283
    master - mean (277ms)  : 274, 281

    section CallTarget+Inlining+NGEN
    This PR (7932) - mean (907ms)  : 851, 962
    master - mean (905ms)  : 854, 955

HttpMessageHandler (.NET 6)
gantt
    title Execution time (ms) HttpMessageHandler (.NET 6)
    dateFormat  x
    axisFormat %Q
    todayMarker off
    section Baseline
    This PR (7932) - mean (274ms)  : 266, 281
    master - mean (271ms)  : 266, 275

    section Bailout
    This PR (7932) - mean (273ms)  : 266, 281
    master - mean (270ms)  : 266, 274

    section CallTarget+Inlining+NGEN
    This PR (7932) - mean (894ms)  : 850, 939
    master - mean (891ms)  : 840, 942

HttpMessageHandler (.NET 8)
gantt
    title Execution time (ms) HttpMessageHandler (.NET 8)
    dateFormat  x
    axisFormat %Q
    todayMarker off
    section Baseline
    This PR (7932) - mean (269ms)  : 265, 274
    master - mean (270ms)  : 265, 274

    section Bailout
    This PR (7932) - mean (270ms)  : 267, 274
    master - mean (270ms)  : 266, 273

    section CallTarget+Inlining+NGEN
    This PR (7932) - mean (825ms)  : 796, 854
    master - mean (824ms)  : 802, 846

✅ Tests

🎉 All green!

❄️ No new flaky tests detected
🧪 All tests passed

This comment will be updated automatically if new data arrives.
🔗 Commit SHA: b7ac294 | Docs | Datadog PR Page | Was this helpful? Give us feedback!