Skip to content

refactor(otel-thread-ctx): backport review suggestions from Polar Signals#2016

Merged
gh-worker-dd-mergequeue-cf854d[bot] merged 1 commit into
mainfrom
yannham/otel-thread-ctx-polarsignals-review
May 20, 2026
Merged

refactor(otel-thread-ctx): backport review suggestions from Polar Signals#2016
gh-worker-dd-mergequeue-cf854d[bot] merged 1 commit into
mainfrom
yannham/otel-thread-ctx-polarsignals-review

Conversation

@yannham
Copy link
Copy Markdown
Contributor

@yannham yannham commented May 20, 2026

What does this PR do?

Backports applicable review feedback from the upstream polarsignals/custom-labels#15 PR into our libdd-otel-thread-ctx crate.

This PR switches get_tls_slot from returning a reference with an unconstrained lifetime parameter to a closure-based with_tls_slot API, following the same pattern as std::thread_local!, for improved safety. This PR also moves the extern "C" FFI declaration inside the function body so the raw symbol is structurally inaccessible outside the safe wrapper.

More changes to follow in small themed PRs.

Motivation

We upstreamed libdatadog's OTel process context and thread context to the custom-labels repo, and got good suggestions (courtesy of @umanwizard). Several suggestions apply directly to our vanilla otel-thread-ctx crate:

  • The old get_tls_slot<'a>() -> &'a AtomicPtr<...> signature has an unconstrained lifetime equivalent to 'static, but the TLS storage is not actually 'static (it dies with the thread). Plus, it's forbidden to mix atomic and non-atomic accesses (although we don't do it, it's not prevented for future code changes). The closure-based API prevents this structurally, matching how std::thread_local! solves the same problem.
  • The top-level extern "C" block relied on a doc comment ("CAUTION: do not use directly") to prevent misuse. Nesting it inside the accessor enforces this at the language level.

Additional Notes

The closure is fully monomorphized and inlined by LLVM in release mode: attach, detach, and update produce byte-identical assembly before and after this change. No performance impact.

How to test the change?

Tests pass (no change in behavior)

@datadog-official
Copy link
Copy Markdown

datadog-official Bot commented May 20, 2026

Tests

🎉 All green!

🧪 All tests passed
❄️ No new flaky tests detected

🎯 Code Coverage (details)
Patch Coverage: 100.00%
Overall Coverage: 72.65% (-0.06%)

This comment will be updated automatically if new data arrives.
🔗 Commit SHA: 2448ca0 | Docs | Datadog PR Page | Give us feedback!

@github-actions
Copy link
Copy Markdown
Contributor

Clippy Allow Annotation Report

Comparing clippy allow annotations between branches:

  • Base Branch: origin/main
  • PR Branch: origin/yannham/otel-thread-ctx-polarsignals-review

Summary by Rule

Rule Base Branch PR Branch Change

Annotation Counts by File

File Base Branch PR Branch Change

Annotation Stats by Crate

Crate Base Branch PR Branch Change
clippy-annotation-reporter 5 5 No change (0%)
datadog-ffe-ffi 1 1 No change (0%)
datadog-ipc 21 21 No change (0%)
datadog-live-debugger 6 6 No change (0%)
datadog-live-debugger-ffi 10 10 No change (0%)
datadog-profiling-replayer 4 4 No change (0%)
datadog-remote-config 3 3 No change (0%)
datadog-sidecar 57 57 No change (0%)
libdd-common 13 13 No change (0%)
libdd-common-ffi 12 12 No change (0%)
libdd-data-pipeline 5 5 No change (0%)
libdd-ddsketch 2 2 No change (0%)
libdd-dogstatsd-client 1 1 No change (0%)
libdd-profiling 13 13 No change (0%)
libdd-telemetry 20 20 No change (0%)
libdd-tinybytes 4 4 No change (0%)
libdd-trace-normalization 2 2 No change (0%)
libdd-trace-obfuscation 3 3 No change (0%)
libdd-trace-stats 1 1 No change (0%)
libdd-trace-utils 15 15 No change (0%)
Total 198 198 No change (0%)

About This Report

This report tracks Clippy allow annotations for specific rules, showing how they've changed in this PR. Decreasing the number of these annotations generally improves code quality.

@yannham yannham force-pushed the yannham/otel-thread-ctx-polarsignals-review branch from fc7cbd2 to 6ddbfa4 Compare May 20, 2026 13:15
@yannham yannham changed the title refactor(otel-thread-ctx): backport review improvements from polarsignals/custom-labels#15 refactor(otel-thread-ctx): backport review suggestions from Polar Signals May 20, 2026
@codecov-commenter
Copy link
Copy Markdown

codecov-commenter commented May 20, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 72.65%. Comparing base (cea1e44) to head (2448ca0).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2016      +/-   ##
==========================================
- Coverage   72.71%   72.65%   -0.06%     
==========================================
  Files         453      453              
  Lines       74934    74939       +5     
==========================================
- Hits        54486    54445      -41     
- Misses      20448    20494      +46     
Components Coverage Δ
libdd-crashtracker 65.29% <ø> (-0.02%) ⬇️
libdd-crashtracker-ffi 37.56% <ø> (ø)
libdd-alloc 98.77% <ø> (ø)
libdd-data-pipeline 85.91% <ø> (ø)
libdd-data-pipeline-ffi 73.93% <ø> (ø)
libdd-common 79.81% <ø> (ø)
libdd-common-ffi 74.41% <ø> (ø)
libdd-telemetry 73.37% <ø> (+0.02%) ⬆️
libdd-telemetry-ffi 31.36% <ø> (ø)
libdd-dogstatsd-client 82.64% <ø> (ø)
datadog-ipc 74.75% <ø> (-1.47%) ⬇️
libdd-profiling 81.69% <ø> (-0.03%) ⬇️
libdd-profiling-ffi 64.79% <ø> (ø)
libdd-sampling 97.46% <ø> (ø)
datadog-sidecar 28.87% <ø> (ø)
datdog-sidecar-ffi 8.56% <ø> (ø)
spawn-worker 48.86% <ø> (ø)
libdd-tinybytes 93.16% <ø> (ø)
libdd-trace-normalization 81.71% <ø> (ø)
libdd-trace-obfuscation 87.30% <ø> (ø)
libdd-trace-protobuf 68.25% <ø> (ø)
libdd-trace-utils 89.59% <ø> (ø)
libdd-tracer-flare 86.88% <ø> (ø)
libdd-log 74.83% <ø> (ø)
🚀 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.

Switch `get_tls_slot` from returning a reference with an unconstrained
lifetime to a closure-based `with_tls_slot` API, matching the pattern
used by `std::thread_local!`. This prevents leaking a reference to
thread-local storage to another thread where it could become dangling.

Also moves the `extern "C"` declaration inside `with_tls_slot`, making
the raw FFI symbol structurally inaccessible outside the safe wrapper.

Backported from upstream review feedback on
polarsignals/custom-labels#15.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@yannham yannham force-pushed the yannham/otel-thread-ctx-polarsignals-review branch from 6ddbfa4 to 2448ca0 Compare May 20, 2026 13:34
@yannham yannham marked this pull request as ready for review May 20, 2026 13:35
@yannham yannham requested a review from a team as a code owner May 20, 2026 13:35
@yannham yannham requested review from ivoanjo and scottgerring May 20, 2026 13:42
@dd-octo-sts
Copy link
Copy Markdown
Contributor

dd-octo-sts Bot commented May 20, 2026

Artifact Size Benchmark Report

aarch64-alpine-linux-musl
Artifact Baseline Commit Change
/aarch64-alpine-linux-musl/lib/libdatadog_profiling.so 7.57 MB 7.57 MB 0% (0 B) 👌
/aarch64-alpine-linux-musl/lib/libdatadog_profiling.a 81.76 MB 81.76 MB 0% (0 B) 👌
aarch64-unknown-linux-gnu
Artifact Baseline Commit Change
/aarch64-unknown-linux-gnu/lib/libdatadog_profiling.a 97.95 MB 97.95 MB 0% (0 B) 👌
/aarch64-unknown-linux-gnu/lib/libdatadog_profiling.so 10.01 MB 10.01 MB 0% (0 B) 👌
libdatadog-x64-windows
Artifact Baseline Commit Change
/libdatadog-x64-windows/debug/dynamic/datadog_profiling_ffi.dll 24.47 MB 24.47 MB 0% (0 B) 👌
/libdatadog-x64-windows/debug/dynamic/datadog_profiling_ffi.lib 79.87 KB 79.87 KB 0% (0 B) 👌
/libdatadog-x64-windows/debug/dynamic/datadog_profiling_ffi.pdb 179.99 MB 180.03 MB +.02% (+40.00 KB) 🔍
/libdatadog-x64-windows/debug/static/datadog_profiling_ffi.lib 913.27 MB 913.27 MB 0% (0 B) 👌
/libdatadog-x64-windows/release/dynamic/datadog_profiling_ffi.dll 7.73 MB 7.73 MB 0% (0 B) 👌
/libdatadog-x64-windows/release/dynamic/datadog_profiling_ffi.lib 79.87 KB 79.87 KB 0% (0 B) 👌
/libdatadog-x64-windows/release/dynamic/datadog_profiling_ffi.pdb 23.15 MB 23.15 MB 0% (0 B) 👌
/libdatadog-x64-windows/release/static/datadog_profiling_ffi.lib 45.34 MB 45.34 MB 0% (0 B) 👌
libdatadog-x86-windows
Artifact Baseline Commit Change
/libdatadog-x86-windows/debug/dynamic/datadog_profiling_ffi.dll 21.09 MB 21.09 MB 0% (0 B) 👌
/libdatadog-x86-windows/debug/dynamic/datadog_profiling_ffi.lib 81.11 KB 81.11 KB 0% (0 B) 👌
/libdatadog-x86-windows/debug/dynamic/datadog_profiling_ffi.pdb 184.17 MB 184.22 MB +.02% (+48.00 KB) 🔍
/libdatadog-x86-windows/debug/static/datadog_profiling_ffi.lib 898.97 MB 898.97 MB 0% (0 B) 👌
/libdatadog-x86-windows/release/dynamic/datadog_profiling_ffi.dll 5.99 MB 5.99 MB 0% (0 B) 👌
/libdatadog-x86-windows/release/dynamic/datadog_profiling_ffi.lib 81.11 KB 81.11 KB 0% (0 B) 👌
/libdatadog-x86-windows/release/dynamic/datadog_profiling_ffi.pdb 24.80 MB 24.80 MB 0% (0 B) 👌
/libdatadog-x86-windows/release/static/datadog_profiling_ffi.lib 42.85 MB 42.85 MB 0% (0 B) 👌
x86_64-alpine-linux-musl
Artifact Baseline Commit Change
/x86_64-alpine-linux-musl/lib/libdatadog_profiling.a 72.87 MB 72.87 MB 0% (0 B) 👌
/x86_64-alpine-linux-musl/lib/libdatadog_profiling.so 8.42 MB 8.42 MB 0% (0 B) 👌
x86_64-unknown-linux-gnu
Artifact Baseline Commit Change
/x86_64-unknown-linux-gnu/lib/libdatadog_profiling.a 90.63 MB 90.63 MB 0% (0 B) 👌
/x86_64-unknown-linux-gnu/lib/libdatadog_profiling.so 10.06 MB 10.06 MB 0% (0 B) 👌

Copy link
Copy Markdown
Member

@ivoanjo ivoanjo 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 from my side. I'll admit that as usual I'm not the most knowledgeable about the rust low-level details so probably worth having an additional rusty human do a quick pass as well :)

@yannham
Copy link
Copy Markdown
Contributor Author

yannham commented May 20, 2026

/merge

@gh-worker-devflow-routing-ef8351
Copy link
Copy Markdown

gh-worker-devflow-routing-ef8351 Bot commented May 20, 2026

View all feedbacks in Devflow UI.

2026-05-20 16:16:03 UTC ℹ️ Start processing command /merge


2026-05-20 16:16:07 UTC ℹ️ MergeQueue: pull request added to the queue

The expected merge time in main is approximately 51m (p90).


2026-05-20 16:58:08 UTC ℹ️ MergeQueue: This merge request was merged

@gh-worker-dd-mergequeue-cf854d gh-worker-dd-mergequeue-cf854d Bot merged commit d96d160 into main May 20, 2026
92 checks passed
@gh-worker-dd-mergequeue-cf854d gh-worker-dd-mergequeue-cf854d Bot deleted the yannham/otel-thread-ctx-polarsignals-review branch May 20, 2026 16:58
gh-worker-dd-mergequeue-cf854d Bot pushed a commit that referenced this pull request May 20, 2026
…extRecord (#2018)

# What does this PR do?

Follow-up to #2016.

Adds compile-time `mem::offset_of!` assertions for every field of `ThreadContextRecord`, catching accidental ABI breakage (field reordering, padding changes) at compile time rather than at runtime in a profiler.

The existing size-only assertion from inside `new()` is moved to a top-level `const _: () = { ... }` block next to the struct definition so it fires unconditionally regardless of code path.

# Motivation

`ThreadContextRecord` has a fixed ABI read by out-of-process eBPF readers. The previous single `size_of == 640` check would still pass if fields were reordered without changing the total size. Per-field offset assertions make this impossible.

# Additional Notes

N/A

# How to test the change?

N/A

Co-authored-by: yann.hamdaoui <yann.hamdaoui@datadoghq.com>
@scottgerring
Copy link
Copy Markdown
Member

Late to the party but super excited to see the stuff from the polar' PR feeding back here :)

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.

5 participants