Skip to content

Conversation

@andrewlock
Copy link
Member

@andrewlock andrewlock commented Oct 28, 2025

Summary of changes

  • This is the big one
  • Update services to dynamically update when mutable settings or exporter settings change
  • Stop rebuilding everything when there's manual/remote configuration

Reason for change

This is the "endpoint" that we've been heading for - services only being disposed/rebuilt at the end of the app, and otherwise only rebuilding the necessary parts. For example - we don't need to tear down all the API factories when a customer changes a global tag via remote config; they only need to change if the ExporterSettings change.

The hope is that overall this reduces the overhead of using configuration in code and/or remote configuration, while also reducing the number of issues due to managing disposal of services.

Implementation details

Overall, this PR is kind of a pain. Moving from the "rebuild everything" to "reconfigure each service" couldn't be done piecemeal, so this is the one-shot PR. What's more, different services need different patterns (though we can probably consolidate some of them, this has taken a lot of work and I likely changed patterns unnecessarily in some places).

In general, there's a couple of patterns:

  • CI Vis doesn't let you change settings at runtime, so it never needs to respond to changes. It always just uses the "initial" settings
  • Debugger today doesn't respond to changes at runtime (except its own dynamic config), so for now we ignore Debugger too as it's not really a regression. I hope we can fix this soon though.
  • I've introduced the concept of Managed* versions of some services
    • These services generally "wrap" the existing type, delegating access to the underlying service, and handling settings changes
  • Many services only care about a sub-set of mutable settings, so they only update if they need to
  • Somewhat annoyingly, setting updates occur on a background thread, so we need to be careful about thread safety. Where necessary (most places) I've made sure access to a now-mutable service is done using Volatile.Read() (to ensure changes are visible) and are generally cached to a local variable (as the underlying field may be updated in the background).

Test coverage

In the vast majority of places, this should be covered by existing tests

I plan to add some additional integration tests around reconfiguring and a bunch of manual testing to make sure I'm confident.

Other details

I strongly recommend reviewing commit-by-commit. They're generally self-contained, and hopefully simple enough to understand one commit at a time.

https://datadoghq.atlassian.net/browse/LANGPLAT-819

Part of a config stack

This isn't the final PR in the stack, as there will be a bunch of cleaning up to do, but it's the final "implementation" PR

Copy link

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@andrewlock andrewlock force-pushed the andrew/settings/5-remove-mutablesettings branch 3 times, most recently from 2bc63f6 to 34f0d90 Compare October 28, 2025 15:02
@andrewlock andrewlock force-pushed the andrew/settings/5-move-mutable-settings-off-tracer-settings branch from e347879 to 8c472a5 Compare October 28, 2025 15:02
@andrewlock andrewlock force-pushed the andrew/settings/5-remove-mutablesettings branch from 34f0d90 to f1e1c7e Compare October 28, 2025 15:20
@andrewlock andrewlock force-pushed the andrew/settings/5-move-mutable-settings-off-tracer-settings branch from 8c472a5 to 8e19e3a Compare October 28, 2025 15:20
@datadog-official
Copy link

datadog-official bot commented Oct 28, 2025

⚠️ Tests

⚠️ Warnings

🧪 1 Test failed

RunTest from Datadog.Trace.Security.IntegrationTests.Rcm.AspNetCore5AsmDataSecurityEnabledBlockingRequestIp (Datadog)
Unable to determine port application is listening on

ℹ️ Info

❄️ No new flaky tests detected

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

@andrewlock andrewlock force-pushed the andrew/settings/5-remove-mutablesettings branch from f1e1c7e to c2b6a1c Compare October 28, 2025 18:13
@andrewlock andrewlock requested review from a team as code owners October 28, 2025 18:13
@andrewlock andrewlock requested review from link04 and removed request for a team October 28, 2025 18:13
@andrewlock andrewlock force-pushed the andrew/settings/5-move-mutable-settings-off-tracer-settings branch from 8e19e3a to 7940c31 Compare October 28, 2025 18:13
@andrewlock andrewlock force-pushed the andrew/settings/5-remove-mutablesettings branch from c2b6a1c to 48c7644 Compare October 29, 2025 08:57
@dd-trace-dotnet-ci-bot
Copy link

dd-trace-dotnet-ci-bot bot commented Oct 29, 2025

Execution-Time Benchmarks Report ⏱️

Execution-time results for samples comparing This PR (7724) 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
duration75.09 ± (74.98 - 75.51) ms75.45 ± (75.22 - 75.81) ms+0.5%✅⬆️
.NET Framework 4.8 - Bailout
duration79.41 ± (79.18 - 79.75) ms79.38 ± (79.39 - 80.17) ms-0.0%
.NET Framework 4.8 - CallTarget+Inlining+NGEN
duration1120.52 ± (1123.00 - 1132.29) ms1138.47 ± (1135.19 - 1144.00) ms+1.6%✅⬆️
.NET Core 3.1 - Baseline
process.internal_duration_ms23.16 ± (23.08 - 23.25) ms23.07 ± (22.97 - 23.17) ms-0.4%
process.time_to_main_ms88.39 ± (87.99 - 88.79) ms87.82 ± (87.43 - 88.20) ms-0.6%
runtime.dotnet.exceptions.count0 ± (0 - 0)0 ± (0 - 0)+0.0%
runtime.dotnet.mem.committed10.95 ± (10.95 - 10.96) MB10.93 ± (10.93 - 10.93) MB-0.2%
runtime.dotnet.threads.count12 ± (12 - 12)12 ± (12 - 12)+0.0%
.NET Core 3.1 - Bailout
process.internal_duration_ms22.98 ± (22.90 - 23.05) ms23.14 ± (23.07 - 23.21) ms+0.7%✅⬆️
process.time_to_main_ms89.09 ± (88.77 - 89.41) ms89.54 ± (89.21 - 89.86) ms+0.5%✅⬆️
runtime.dotnet.exceptions.count0 ± (0 - 0)0 ± (0 - 0)+0.0%
runtime.dotnet.mem.committed10.99 ± (10.98 - 10.99) MB10.97 ± (10.97 - 10.97) MB-0.1%
runtime.dotnet.threads.count13 ± (13 - 13)13 ± (13 - 13)+0.0%
.NET Core 3.1 - CallTarget+Inlining+NGEN
process.internal_duration_ms220.56 ± (219.04 - 222.09) ms221.24 ± (220.00 - 222.49) ms+0.3%✅⬆️
process.time_to_main_ms553.95 ± (552.58 - 555.32) ms552.36 ± (551.07 - 553.65) ms-0.3%
runtime.dotnet.exceptions.count0 ± (0 - 0)0 ± (0 - 0)+0.0%
runtime.dotnet.mem.committed52.54 ± (52.52 - 52.56) MB52.57 ± (52.55 - 52.59) MB+0.1%✅⬆️
runtime.dotnet.threads.count28 ± (28 - 28)28 ± (28 - 28)+0.1%✅⬆️
.NET 6 - Baseline
process.internal_duration_ms21.71 ± (21.64 - 21.79) ms22.00 ± (21.94 - 22.07) ms+1.3%✅⬆️
process.time_to_main_ms76.14 ± (75.76 - 76.52) ms77.39 ± (77.04 - 77.73) ms+1.6%✅⬆️
runtime.dotnet.exceptions.count0 ± (0 - 0)0 ± (0 - 0)+0.0%
runtime.dotnet.mem.committed10.62 ± (10.61 - 10.62) MB10.65 ± (10.65 - 10.65) MB+0.3%✅⬆️
runtime.dotnet.threads.count10 ± (10 - 10)10 ± (10 - 10)+0.0%
.NET 6 - Bailout
process.internal_duration_ms21.77 ± (21.70 - 21.84) ms21.83 ± (21.76 - 21.89) ms+0.3%✅⬆️
process.time_to_main_ms77.45 ± (77.10 - 77.80) ms77.56 ± (77.30 - 77.81) ms+0.1%✅⬆️
runtime.dotnet.exceptions.count0 ± (0 - 0)0 ± (0 - 0)+0.0%
runtime.dotnet.mem.committed10.66 ± (10.65 - 10.66) MB10.69 ± (10.69 - 10.70) MB+0.3%✅⬆️
runtime.dotnet.threads.count11 ± (11 - 11)11 ± (11 - 11)+0.0%
.NET 6 - CallTarget+Inlining+NGEN
process.internal_duration_ms207.28 ± (205.81 - 208.75) ms208.57 ± (207.58 - 209.56) ms+0.6%✅⬆️
process.time_to_main_ms515.34 ± (514.37 - 516.31) ms514.07 ± (512.95 - 515.19) ms-0.2%
runtime.dotnet.exceptions.count0 ± (0 - 0)0 ± (0 - 0)+0.0%
runtime.dotnet.mem.committed51.66 ± (51.63 - 51.68) MB51.64 ± (51.61 - 51.68) MB-0.0%
runtime.dotnet.threads.count28 ± (28 - 28)28 ± (28 - 28)-0.0%
.NET 8 - Baseline
process.internal_duration_ms19.99 ± (19.93 - 20.06) ms19.97 ± (19.90 - 20.04) ms-0.1%
process.time_to_main_ms76.09 ± (75.75 - 76.43) ms75.54 ± (75.18 - 75.90) ms-0.7%
runtime.dotnet.exceptions.count0 ± (0 - 0)0 ± (0 - 0)+0.0%
runtime.dotnet.mem.committed7.65 ± (7.65 - 7.65) MB7.66 ± (7.66 - 7.67) MB+0.1%✅⬆️
runtime.dotnet.threads.count10 ± (10 - 10)10 ± (10 - 10)+0.0%
.NET 8 - Bailout
process.internal_duration_ms19.79 ± (19.74 - 19.84) ms19.90 ± (19.84 - 19.96) ms+0.6%✅⬆️
process.time_to_main_ms75.60 ± (75.33 - 75.86) ms76.85 ± (76.52 - 77.18) ms+1.7%✅⬆️
runtime.dotnet.exceptions.count0 ± (0 - 0)0 ± (0 - 0)+0.0%
runtime.dotnet.mem.committed7.71 ± (7.70 - 7.71) MB7.71 ± (7.70 - 7.71) MB-0.0%
runtime.dotnet.threads.count11 ± (11 - 11)11 ± (11 - 11)+0.0%
.NET 8 - CallTarget+Inlining+NGEN
process.internal_duration_ms192.57 ± (191.80 - 193.33) ms195.43 ± (194.53 - 196.33) ms+1.5%✅⬆️
process.time_to_main_ms494.43 ± (493.32 - 495.54) ms493.14 ± (492.17 - 494.11) ms-0.3%
runtime.dotnet.exceptions.count0 ± (0 - 0)0 ± (0 - 0)+0.0%
runtime.dotnet.mem.committed38.88 ± (38.84 - 38.93) MB38.88 ± (38.84 - 38.93) 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
duration191.92 ± (191.83 - 192.73) ms191.94 ± (191.96 - 192.69) ms+0.0%✅⬆️
.NET Framework 4.8 - Bailout
duration196.20 ± (196.13 - 196.81) ms196.86 ± (196.78 - 197.76) ms+0.3%✅⬆️
.NET Framework 4.8 - CallTarget+Inlining+NGEN
duration1160.15 ± (1161.13 - 1168.34) ms1168.55 ± (1168.61 - 1175.61) ms+0.7%✅⬆️
.NET Core 3.1 - Baseline
process.internal_duration_ms187.95 ± (187.54 - 188.36) ms189.52 ± (189.10 - 189.94) ms+0.8%✅⬆️
process.time_to_main_ms80.61 ± (80.37 - 80.86) ms80.98 ± (80.71 - 81.25) ms+0.5%✅⬆️
runtime.dotnet.exceptions.count3 ± (3 - 3)3 ± (3 - 3)+0.0%
runtime.dotnet.mem.committed16.11 ± (16.08 - 16.14) MB16.07 ± (16.05 - 16.09) MB-0.2%
runtime.dotnet.threads.count20 ± (19 - 20)20 ± (20 - 20)+0.2%✅⬆️
.NET Core 3.1 - Bailout
process.internal_duration_ms187.18 ± (186.93 - 187.42) ms188.58 ± (188.13 - 189.03) ms+0.7%✅⬆️
process.time_to_main_ms81.54 ± (81.41 - 81.66) ms82.15 ± (81.93 - 82.38) ms+0.8%✅⬆️
runtime.dotnet.exceptions.count3 ± (3 - 3)3 ± (3 - 3)+0.0%
runtime.dotnet.mem.committed16.20 ± (16.17 - 16.24) MB16.10 ± (16.08 - 16.13) MB-0.6%
runtime.dotnet.threads.count21 ± (21 - 21)21 ± (21 - 21)+0.0%✅⬆️
.NET Core 3.1 - CallTarget+Inlining+NGEN
process.internal_duration_ms397.20 ± (394.49 - 399.90) ms393.05 ± (391.08 - 395.03) ms-1.0%
process.time_to_main_ms518.16 ± (517.50 - 518.83) ms520.01 ± (519.40 - 520.63) ms+0.4%✅⬆️
runtime.dotnet.exceptions.count3 ± (3 - 3)3 ± (3 - 3)+0.0%
runtime.dotnet.mem.committed62.93 ± (62.78 - 63.09) MB63.01 ± (62.85 - 63.16) MB+0.1%✅⬆️
runtime.dotnet.threads.count29 ± (29 - 29)29 ± (29 - 30)+0.1%✅⬆️
.NET 6 - Baseline
process.internal_duration_ms192.00 ± (191.65 - 192.36) ms192.41 ± (192.05 - 192.77) ms+0.2%✅⬆️
process.time_to_main_ms69.79 ± (69.61 - 69.97) ms70.14 ± (69.96 - 70.32) ms+0.5%✅⬆️
runtime.dotnet.exceptions.count4 ± (4 - 4)4 ± (4 - 4)+0.0%
runtime.dotnet.mem.committed16.11 ± (15.97 - 16.25) MB16.17 ± (16.05 - 16.29) MB+0.4%✅⬆️
runtime.dotnet.threads.count18 ± (18 - 18)19 ± (18 - 19)+2.4%✅⬆️
.NET 6 - Bailout
process.internal_duration_ms190.60 ± (190.35 - 190.86) ms191.41 ± (191.09 - 191.73) ms+0.4%✅⬆️
process.time_to_main_ms70.74 ± (70.66 - 70.83) ms71.05 ± (70.95 - 71.14) ms+0.4%✅⬆️
runtime.dotnet.exceptions.count4 ± (4 - 4)4 ± (4 - 4)+0.0%
runtime.dotnet.mem.committed15.85 ± (15.68 - 16.02) MB15.90 ± (15.74 - 16.07) MB+0.3%✅⬆️
runtime.dotnet.threads.count19 ± (19 - 19)19 ± (19 - 19)+0.1%✅⬆️
.NET 6 - CallTarget+Inlining+NGEN
process.internal_duration_ms411.39 ± (408.52 - 414.26) ms411.24 ± (408.98 - 413.50) ms-0.0%
process.time_to_main_ms487.14 ± (486.63 - 487.65) ms487.84 ± (486.49 - 489.20) ms+0.1%✅⬆️
runtime.dotnet.exceptions.count4 ± (4 - 4)4 ± (4 - 4)+0.0%
runtime.dotnet.mem.committed62.11 ± (61.97 - 62.26) MB62.66 ± (62.56 - 62.77) MB+0.9%✅⬆️
runtime.dotnet.threads.count29 ± (29 - 30)30 ± (29 - 30)+0.2%✅⬆️
.NET 8 - Baseline
process.internal_duration_ms189.90 ± (189.65 - 190.15) ms195.58 ± (195.13 - 196.03) ms+3.0%✅⬆️
process.time_to_main_ms69.17 ± (69.01 - 69.32) ms71.03 ± (70.79 - 71.27) ms+2.7%✅⬆️
runtime.dotnet.exceptions.count4 ± (4 - 4)4 ± (4 - 4)+0.0%
runtime.dotnet.mem.committed11.81 ± (11.78 - 11.84) MB11.72 ± (11.70 - 11.74) MB-0.8%
runtime.dotnet.threads.count18 ± (18 - 18)18 ± (18 - 18)+1.0%✅⬆️
.NET 8 - Bailout
process.internal_duration_ms189.31 ± (189.06 - 189.55) ms191.83 ± (191.41 - 192.25) ms+1.3%✅⬆️
process.time_to_main_ms69.90 ± (69.82 - 69.99) ms71.15 ± (70.99 - 71.30) ms+1.8%✅⬆️
runtime.dotnet.exceptions.count4 ± (4 - 4)4 ± (4 - 4)+0.0%
runtime.dotnet.mem.committed11.85 ± (11.82 - 11.88) MB11.80 ± (11.77 - 11.84) MB-0.4%
runtime.dotnet.threads.count19 ± (19 - 19)19 ± (19 - 19)+0.1%✅⬆️
.NET 8 - CallTarget+Inlining+NGEN
process.internal_duration_ms360.67 ± (359.20 - 362.15) ms369.42 ± (367.55 - 371.29) ms+2.4%✅⬆️
process.time_to_main_ms463.43 ± (462.75 - 464.10) ms470.18 ± (468.96 - 471.40) ms+1.5%✅⬆️
runtime.dotnet.exceptions.count4 ± (4 - 4)4 ± (4 - 4)+0.0%
runtime.dotnet.mem.committed50.29 ± (50.25 - 50.32) MB50.27 ± (50.23 - 50.30) MB-0.0%
runtime.dotnet.threads.count29 ± (29 - 29)29 ± (29 - 29)-0.2%
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 (7724) - mean (76ms)  : 71, 80
    master - mean (75ms)  : 71, 79

    section Bailout
    This PR (7724) - mean (80ms)  : 74, 86
    master - mean (79ms)  : 75, 84

    section CallTarget+Inlining+NGEN
    This PR (7724) - mean (1,140ms)  : 1075, 1205
    master - mean (1,128ms)  : 1061, 1194

Loading
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 (7724) - mean (118ms)  : 110, 127
    master - mean (119ms)  : 110, 128

    section Bailout
    This PR (7724) - mean (120ms)  : 115, 126
    master - mean (120ms)  : 114, 126

    section CallTarget+Inlining+NGEN
    This PR (7724) - mean (815ms)  : 777, 853
    master - mean (819ms)  : 764, 874

Loading
FakeDbCommand (.NET 6)
gantt
    title Execution time (ms) FakeDbCommand (.NET 6)
    dateFormat  x
    axisFormat %Q
    todayMarker off
    section Baseline
    This PR (7724) - mean (106ms)  : 99, 113
    master - mean (105ms)  : 97, 113

    section Bailout
    This PR (7724) - mean (107ms)  : 101, 112
    master - mean (106ms)  : 99, 114

    section CallTarget+Inlining+NGEN
    This PR (7724) - mean (763ms)  : 734, 793
    master - mean (756ms)  : 720, 793

Loading
FakeDbCommand (.NET 8)
gantt
    title Execution time (ms) FakeDbCommand (.NET 8)
    dateFormat  x
    axisFormat %Q
    todayMarker off
    section Baseline
    This PR (7724) - mean (104ms)  : 97, 111
    master - mean (105ms)  : 98, 111

    section Bailout
    This PR (7724) - mean (105ms)  : 99, 112
    master - mean (104ms)  : 98, 109

    section CallTarget+Inlining+NGEN
    This PR (7724) - mean (729ms)  : 694, 763
    master - mean (729ms)  : 695, 764

Loading
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 (7724) - mean (192ms)  : 189, 196
    master - mean (192ms)  : 188, 197

    section Bailout
    This PR (7724) - mean (197ms)  : 192, 202
    master - mean (196ms)  : 193, 200

    section CallTarget+Inlining+NGEN
    This PR (7724) - mean (1,172ms)  : 1122, 1222
    master - mean (1,165ms)  : 1113, 1216

Loading
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 (7724) - mean (279ms)  : 273, 285
    master - mean (277ms)  : 271, 282

    section Bailout
    This PR (7724) - mean (280ms)  : 272, 289
    master - mean (277ms)  : 273, 281

    section CallTarget+Inlining+NGEN
    This PR (7724) - mean (955ms)  : 915, 994
    master - mean (956ms)  : 909, 1003

Loading
HttpMessageHandler (.NET 6)
gantt
    title Execution time (ms) HttpMessageHandler (.NET 6)
    dateFormat  x
    axisFormat %Q
    todayMarker off
    section Baseline
    This PR (7724) - mean (271ms)  : 267, 274
    master - mean (271ms)  : 265, 276

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

    section CallTarget+Inlining+NGEN
    This PR (7724) - mean (939ms)  : 878, 999
    master - mean (931ms)  : 884, 978

Loading
HttpMessageHandler (.NET 8)
gantt
    title Execution time (ms) HttpMessageHandler (.NET 8)
    dateFormat  x
    axisFormat %Q
    todayMarker off
    section Baseline
    This PR (7724) - mean (277ms)  : 269, 285
    master - mean (269ms)  : 266, 272

    section Bailout
    This PR (7724) - mean (273ms)  : 267, 278
    master - mean (268ms)  : 265, 271

    section CallTarget+Inlining+NGEN
    This PR (7724) - mean (873ms)  : 824, 921
    master - mean (855ms)  : 829, 881

Loading

@andrewlock andrewlock force-pushed the andrew/settings/5-remove-mutablesettings branch from 11c23d3 to 7038956 Compare November 11, 2025 10:04
Copy link
Collaborator

@bouwkast bouwkast left a comment

Choose a reason for hiding this comment

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

Looks good to me but it is quite large 😅

Hardest part is the whole bit manipulation with the count / closing sign all packed into one int but I think it looks good but I think it may be nice to have some additional tests is possible around StatsdManager

Comment on lines 220 to 221
// Don't blame me, blame the fact we can't do Volatile.Read with a ulong in .NET FX...
var nodeHashBase = new NodeHashBase(unchecked((ulong)Volatile.Read(ref Unsafe.As<ulong, long>(ref _nodeHashBase))));
Copy link
Collaborator

Choose a reason for hiding this comment

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

😅

So uh what exactly is going on here?
It is a struct so can't use InterLocked.CompareExchange but we also need to convert it from ulong to long?

Copy link
Member Author

Choose a reason for hiding this comment

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

so, yeah, this is a mess 😅

  • We want to cache the value of NodeHashBase
  • NodeHashBase depends on some mutable settings, e.g. service name, so we need to be able to replace NodeHashBase.
  • We need to use Interlocked.Exchange() to make sure we do the swap atomically.
    • You can't do Interlocked.Exchange() with a struct (for technical reasons) 🙁
  • Luckily, NodeHashBase is just a thin wrapper around a ulong, which is 64 bit and can be used with Interlocked.Exchange 🎉
    • Except, < .NET 5, you can't do Interlocked.Exchange(ulong) because the overloads don't exist 😭
  • The "solution" for updating is (you can see this in the UpdateNodeHash method):
    • Calculate the new NodeHashBase
    • Grab the ulong from the NodeHashBase
    • Do an unchecked cast of the ulong as a long
    • Do an Interlocked.Exchange(ref long), treating the stored ulong _nodeHashBase as a long for the purposes of the exchange
  • The solution for reading is:
    • Do a Volatile.Read(ref long), treating the stored ulong _nodeHashBase as a long for the purposes of the read
      • Do an unchecked case of the long as a ulong,
      • Create the new NodeHashBase

Yes, it's a PITA.

All that said, I just realised that if we store the _nodeHashBase as an int, we can avoid all the Unsafe.As reinterpretation, and just stick with the unchecked casts, which simplifies this a bit! 🙂

"key": "service.name",
"value": {
"string_value": "unknown_service:dotnet"
"string_value": "Samples.OpenTelemetrySdk"
Copy link
Collaborator

Choose a reason for hiding this comment

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

Hmm I do remember running into this previously with unknown_service I think I did something to override this like so a long time ago, unsure how / why this changed though now 🤷

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, I'm going to defer to @link04 on this one 😅 My guess is that there were/are some race conditions here. I think this is now "correct" to be fair 😄

@andrewlock andrewlock force-pushed the andrew/settings/5-remove-mutablesettings branch from 8f633c0 to 49ed373 Compare November 12, 2025 12:22
Also:
- slight refactor of LogFormatter to reduce some allocation
- ignore "previous" when creating DirectLogSubmissionManager (seeing as that won't be a thing soon)
…n't respond to changes

I left it like this because the debugger already doesn't respond to changes like other services do
- Move statsd instance creation to separate factory
- Create a StatsdManager to handle automatic updating in response to setting changes
- Always create a statsd instance, as it's hard to know if we're _ever_ going to need one, and reduces some of the compexity
This isn't necessary with the current design, and it causes issues today
Make sure we can't dispose a stats consumer that's in use (as it will throw)
Rework to use a "lease" mechanism to track usages
Make passing in a statsmanager required
The statsd client does sync-over-async in the flush and dispose paths, which can lead to deadlocks and thread exhaustion.
To work around that, we push the dispose to happen on a thread-pool thread instead, in the background
@andrewlock andrewlock force-pushed the andrew/settings/5-remove-mutablesettings branch from 49ed373 to 70af77a Compare November 12, 2025 13:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants