Skip to content

feat: Add FFI for trace exporter#1952

Merged
gh-worker-dd-mergequeue-cf854d[bot] merged 15 commits into
mainfrom
lloeki/ffi-for-trace-exporter
May 26, 2026
Merged

feat: Add FFI for trace exporter#1952
gh-worker-dd-mergequeue-cf854d[bot] merged 15 commits into
mainfrom
lloeki/ffi-for-trace-exporter

Conversation

@lloeki
Copy link
Copy Markdown
Member

@lloeki lloeki commented May 6, 2026

What does this PR do?

Add span-building and trace-sending FFI functions to libdd-data-pipeline-ffi, enabling language tracers to construct spans field-by-field through the C API instead of passing pre-serialized msgpack.

New types:

  • TracerSpan: opaque handle wrapping Span<BytesData>
  • TracerTraceChunks: opaque handle wrapping Vec<Vec<SpanBytes>>

New functions:

  • ddog_tracer_span_new, _free, _set_meta, _set_metric
  • ddog_tracer_trace_chunks_new, _free, _begin_chunk, _push_span
  • ddog_trace_exporter_send_trace_chunks

Motivation

The Ruby tracer needs a native trace export path that bypasses Ruby-side msgpack serialization. This provides the FFI surface for it; the corresponding C extension in dd-trace-rb calls these functions.

Continuation of the prototype in #1661, reworked to stay close to main: the generic TraceExporter<H> and SharedRuntime are untouched; this is purely additive.

Additional Notes

Spans are consumed on push, chunks are consumed on send — single-ownership enforced at the API level.

libdd-trace-utils is promoted from dev-dependency to regular dependency since the FFI crate now needs SpanBytes at build time.

Sibling PR for Ruby is at: DataDog/dd-trace-rb#5690

How to test the change?

cargo test -p libdd-data-pipeline-ffi --lib — 39 tests, all passing. The existing trace_exporter tests are unaffected.

@lloeki lloeki requested a review from a team as a code owner May 6, 2026 16:47
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented May 6, 2026

Clippy Allow Annotation Report

Comparing clippy allow annotations between branches:

  • Base Branch: origin/main
  • PR Branch: origin/lloeki/ffi-for-trace-exporter

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.

@lloeki lloeki force-pushed the lloeki/ffi-for-trace-exporter branch from 96342f8 to 303d914 Compare May 6, 2026 16:56
@lloeki lloeki changed the title Add FFI for trace exporter feat: Add FFI for trace exporter May 6, 2026
@datadog-prod-us1-6
Copy link
Copy Markdown

datadog-prod-us1-6 Bot commented May 6, 2026

Tests

🎉 All green!

🧪 All tests passed
❄️ No new flaky tests detected

🎯 Code Coverage (details)
Patch Coverage: 78.50%
Overall Coverage: 72.80% (+0.03%)

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

@codecov-commenter
Copy link
Copy Markdown

codecov-commenter commented May 6, 2026

Codecov Report

❌ Patch coverage is 78.50163% with 66 lines in your changes missing coverage. Please review.
✅ Project coverage is 72.79%. Comparing base (3bf778c) to head (693a1ca).
⚠️ Report is 5 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1952      +/-   ##
==========================================
+ Coverage   72.76%   72.79%   +0.03%     
==========================================
  Files         458      459       +1     
  Lines       75789    76106     +317     
==========================================
+ Hits        55147    55405     +258     
- Misses      20642    20701      +59     
Components Coverage Δ
libdd-crashtracker 65.21% <ø> (-0.02%) ⬇️
libdd-crashtracker-ffi 36.82% <ø> (ø)
libdd-alloc 98.77% <ø> (ø)
libdd-data-pipeline 85.63% <78.50%> (+0.33%) ⬆️
libdd-data-pipeline-ffi 75.70% <78.50%> (+5.19%) ⬆️
libdd-common 79.85% <ø> (+0.04%) ⬆️
libdd-common-ffi 74.41% <ø> (ø)
libdd-telemetry 73.34% <ø> (-0.03%) ⬇️
libdd-telemetry-ffi 31.36% <ø> (ø)
libdd-dogstatsd-client 82.64% <ø> (ø)
datadog-ipc 74.75% <ø> (-1.47%) ⬇️
libdd-profiling 81.69% <ø> (ø)
libdd-profiling-ffi 64.79% <ø> (ø)
libdd-sampling 97.46% <ø> (ø)
datadog-sidecar 29.19% <100.00%> (+0.02%) ⬆️
datdog-sidecar-ffi 10.17% <100.00%> (+0.21%) ⬆️
spawn-worker 48.86% <ø> (ø)
libdd-tinybytes 93.80% <ø> (+0.64%) ⬆️
libdd-trace-normalization 81.71% <ø> (ø)
libdd-trace-obfuscation 87.30% <ø> (ø)
libdd-trace-protobuf 68.25% <ø> (ø)
libdd-trace-utils 88.96% <ø> (ø)
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.

@lloeki lloeki force-pushed the lloeki/ffi-for-trace-exporter branch 2 times, most recently from 2b39efc to 2701231 Compare May 6, 2026 17:19
@dd-octo-sts
Copy link
Copy Markdown
Contributor

dd-octo-sts Bot commented May 6, 2026

Artifact Size Benchmark Report

aarch64-alpine-linux-musl
Artifact Baseline Commit Change
/aarch64-alpine-linux-musl/lib/libdatadog_profiling.a 82.01 MB 82.91 MB +1.10% (+926.02 KB) ⚠️
/aarch64-alpine-linux-musl/lib/libdatadog_profiling.so 7.57 MB 7.63 MB +.82% (+64.00 KB) 🔍
aarch64-unknown-linux-gnu
Artifact Baseline Commit Change
/aarch64-unknown-linux-gnu/lib/libdatadog_profiling.so 10.17 MB 10.25 MB +.79% (+82.71 KB) 🔍
/aarch64-unknown-linux-gnu/lib/libdatadog_profiling.a 93.07 MB 94.00 MB +.99% (+949.92 KB) 🔍
libdatadog-x64-windows
Artifact Baseline Commit Change
/libdatadog-x64-windows/debug/dynamic/datadog_profiling_ffi.dll 24.31 MB 24.53 MB +.90% (+225.00 KB) 🔍
/libdatadog-x64-windows/debug/dynamic/datadog_profiling_ffi.lib 81.48 KB 83.96 KB +3.04% (+2.48 KB) ⚠️
/libdatadog-x64-windows/debug/dynamic/datadog_profiling_ffi.pdb 176.90 MB 178.10 MB +.68% (+1.20 MB) 🔍
/libdatadog-x64-windows/debug/static/datadog_profiling_ffi.lib 910.53 MB 914.61 MB +.44% (+4.07 MB) 🔍
/libdatadog-x64-windows/release/dynamic/datadog_profiling_ffi.dll 7.91 MB 8.02 MB +1.43% (+116.00 KB) ⚠️
/libdatadog-x64-windows/release/dynamic/datadog_profiling_ffi.lib 81.48 KB 83.96 KB +3.04% (+2.48 KB) ⚠️
/libdatadog-x64-windows/release/dynamic/datadog_profiling_ffi.pdb 23.49 MB 23.76 MB +1.16% (+280.00 KB) ⚠️
/libdatadog-x64-windows/release/static/datadog_profiling_ffi.lib 46.91 MB 47.41 MB +1.06% (+513.05 KB) ⚠️
libdatadog-x86-windows
Artifact Baseline Commit Change
/libdatadog-x86-windows/debug/dynamic/datadog_profiling_ffi.dll 21.03 MB 21.26 MB +1.09% (+236.50 KB) ⚠️
/libdatadog-x86-windows/debug/dynamic/datadog_profiling_ffi.lib 82.76 KB 85.29 KB +3.05% (+2.52 KB) ⚠️
/libdatadog-x86-windows/debug/dynamic/datadog_profiling_ffi.pdb 180.72 MB 182.05 MB +.73% (+1.32 MB) 🔍
/libdatadog-x86-windows/debug/static/datadog_profiling_ffi.lib 903.74 MB 907.77 MB +.44% (+4.02 MB) 🔍
/libdatadog-x86-windows/release/dynamic/datadog_profiling_ffi.dll 6.12 MB 6.20 MB +1.27% (+80.00 KB) ⚠️
/libdatadog-x86-windows/release/dynamic/datadog_profiling_ffi.lib 82.76 KB 85.29 KB +3.05% (+2.52 KB) ⚠️
/libdatadog-x86-windows/release/dynamic/datadog_profiling_ffi.pdb 25.19 MB 25.48 MB +1.14% (+296.00 KB) ⚠️
/libdatadog-x86-windows/release/static/datadog_profiling_ffi.lib 44.60 MB 45.06 MB +1.03% (+471.61 KB) ⚠️
x86_64-alpine-linux-musl
Artifact Baseline Commit Change
/x86_64-alpine-linux-musl/lib/libdatadog_profiling.a 73.11 MB 73.92 MB +1.11% (+834.48 KB) ⚠️
/x86_64-alpine-linux-musl/lib/libdatadog_profiling.so 8.45 MB 8.52 MB +.92% (+80.00 KB) 🔍
x86_64-unknown-linux-gnu
Artifact Baseline Commit Change
/x86_64-unknown-linux-gnu/lib/libdatadog_profiling.a 88.51 MB 89.33 MB +.92% (+838.52 KB) 🔍
/x86_64-unknown-linux-gnu/lib/libdatadog_profiling.so 10.27 MB 10.36 MB +.90% (+95.00 KB) 🔍

Comment thread libdd-data-pipeline-ffi/src/tracer.rs Outdated
Comment thread libdd-data-pipeline-ffi/src/tracer.rs Outdated
Comment thread libdd-data-pipeline-ffi/src/tracer.rs
Comment thread libdd-data-pipeline-ffi/src/tracer.rs Outdated
Comment thread libdd-data-pipeline-ffi/src/tracer.rs Outdated
Comment thread libdd-data-pipeline-ffi/src/tracer.rs
Comment thread libdd-data-pipeline-ffi/src/tracer.rs Outdated
Comment thread libdd-data-pipeline-ffi/src/tracer.rs Outdated
Comment thread libdd-data-pipeline-ffi/src/tracer.rs Outdated
Comment thread libdd-data-pipeline-ffi/src/tracer.rs
Comment thread libdd-data-pipeline-ffi/src/tracer.rs Outdated
Comment thread libdd-data-pipeline-ffi/src/tracer.rs
Comment thread libdd-data-pipeline-ffi/src/tracer.rs
Comment thread libdd-data-pipeline-ffi/src/tracer.rs
@lloeki lloeki requested review from hoolioh and yannham May 13, 2026 15:27
@lloeki
Copy link
Copy Markdown
Member Author

lloeki commented May 13, 2026

Review comments addressed. If acceptable, I suggest we merge this one as a first iteration of the FFI API.

A new stacked PR based on this one is brewing, which has more contentious API changes coming from the dd-trace-rb side review.

Copy link
Copy Markdown
Contributor

@hoolioh hoolioh left a comment

Choose a reason for hiding this comment

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

I just left a comment requesting a change, other than that LGTM overall for a first version.

Comment thread libdd-data-pipeline-ffi/src/tracer.rs
lloeki added 10 commits May 26, 2026 13:04
Introduce an opaque `TracerSpan` handle wrapping `Span<BytesData>` and
expose it to C callers via four FFI functions:

- `ddog_tracer_span_new`: create a span with all scalar fields
- `ddog_tracer_span_free`: release the span
- `ddog_tracer_span_set_meta`: add a string tag
- `ddog_tracer_span_set_metric`: add a numeric tag

This enables language tracers (e.g. Ruby) to build spans field-by-field
through the C API, bypassing msgpack serialization on the caller side.
Introduce an opaque `TracerTraceChunks` handle wrapping
`Vec<Vec<SpanBytes>>` and expose it to C callers via four FFI
functions:

- `ddog_tracer_trace_chunks_new`: create a container with optional
  capacity hint
- `ddog_tracer_trace_chunks_free`: release the container
- `ddog_tracer_trace_chunks_begin_chunk`: start a new trace chunk
- `ddog_tracer_trace_chunks_push_span`: move a `TracerSpan` into the
  current chunk, consuming it

Spans are consumed on push to enforce single-ownership and prevent
double-use from C callers.
Wire `TracerTraceChunks` to the existing `TraceExporter::send_trace_chunks`
method through a new C-callable function. The chunks are consumed on
call and the agent response is optionally written to an out-parameter.

This completes the span-building FFI surface: callers can now construct
spans via `ddog_tracer_span_*`, group them via
`ddog_tracer_trace_chunks_*`, and send them via
`ddog_trace_exporter_send_trace_chunks` — all without serializing to
msgpack on the caller side.

The `TraceExporter` type alias in `trace_exporter.rs` is widened to
`pub(crate)` to allow cross-module access.
Replace the `match` with identity `Ok` arm with `map_err`, which is the
idiomatic Rust combinator for transforming only the error side of a
`Result`.
The inner fields are only accessed within the `tracer` module itself, so
`pub(crate)` is unnecessarily broad. Default (private) visibility is
sufficient since the module owns all access.
`Vec::with_capacity(0)` and `Vec::new()` are identical — both create an
empty vec with no heap allocation — so the branch is unnecessary.
…race_chunks`

Replace the `match` with `let Some(...) = ... else { return ... }`,
which is the idiomatic Rust pattern for early-return on a refutable
binding (stabilized in Rust 1.65).
`make_minimal_span` and `make_chunks` have no safety preconditions for
their callers — all unsafety is self-contained. Move the `unsafe` from
the function signature into internal blocks so the functions present a
safe interface.
Extract the span-building logic into an inner closure that returns
`Result`, allowing the `?` operator to propagate conversion errors.
The outer body calls `inner().err()` to map `Ok(()) -> None` and
`Err(e) -> Some(e)`, matching the FFI return type.
Replace `NonNull<Box<T>>` with `&mut MaybeUninit<Box<T>>` for
out-parameters in `ddog_tracer_span_new` and
`ddog_tracer_trace_chunks_new`. This makes the uninitialized nature of
the slot explicit in the type system and allows using `MaybeUninit::write`
instead of raw `ptr::write`.

Note that `&mut Box<T>` cannot be used here: assignment through
`&mut Box<T>` drops the old value first, which is undefined behavior
when the slot is uninitialized. `MaybeUninit::write` overwrites without
dropping, which is the correct semantics for out-parameters.

cbindgen produces identical C signatures (`T **out_handle`) for all
three representations.
lloeki added 4 commits May 26, 2026 13:04
Restore the established `NonNull<Box<T>>` convention used throughout
`trace_exporter.rs` for FFI out-parameters. While `&mut MaybeUninit`
is technically more precise, it is less idiomatic for FFI and
inconsistent with the rest of the module.

cbindgen produces identical C headers for both representations.
Replace 11 positional parameters with a `#[repr(C)]` struct passed by
reference. This avoids holding all fields on the stack per call and
allows adding or changing fields without breaking the function
signature.

Follows the same pattern as `TelemetryClientConfig` elsewhere in the
codebase.
Accept a capacity hint for pre-allocating the inner span vector,
avoiding reallocations when the number of spans is known beforehand.
…egin_chunk`

Change the return type from void to `Option<Box<ExporterError>>` and
wrap the body in `catch_panic!` with a null handle check, making the
function consistent with every other mutating function in the tracer
FFI API.
@lloeki lloeki force-pushed the lloeki/ffi-for-trace-exporter branch from 5e2089f to b9ea727 Compare May 26, 2026 11:05
@lloeki
Copy link
Copy Markdown
Member Author

lloeki commented May 26, 2026

9d7998b had all CI OK and now post-rebase there's no CI running?

EDIT: GitHub Actions incident


20260526_14h29m03s_grim
20260526_14h31m14s_grim

@lloeki lloeki requested a review from a team as a code owner May 26, 2026 14:40
@lloeki lloeki force-pushed the lloeki/ffi-for-trace-exporter branch from b1c81ea to 693a1ca Compare May 26, 2026 14:41
@lloeki
Copy link
Copy Markdown
Member Author

lloeki commented May 26, 2026

/merge

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

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

View all feedbacks in Devflow UI.

2026-05-26 15:13:58 UTC ℹ️ Start processing command /merge


2026-05-26 15:14:06 UTC ℹ️ MergeQueue: waiting for PR to be ready

This pull request is not mergeable according to GitHub. Common reasons include pending required checks, missing approvals, or merge conflicts — but it could also be blocked by other repository rules or settings.
It will be added to the queue as soon as checks pass and/or get approvals. View in MergeQueue UI.
Note: if you pushed new commits since the last approval, you may need additional approval.
You can remove it from the waiting list with /remove command.


2026-05-26 15:21:23 UTC ℹ️ MergeQueue: merge request added to the queue

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


2026-05-26 16:03:06 UTCMergeQueue: The checks failed on this merge request

Tests failed on this commit b9ba4bb:

What to do next?

  • Investigate the failures and when ready, re-add your pull request to the queue!
  • If your PR checks are green, try to rebase/merge. It might be because the CI run is a bit old.
  • Any question, go check the FAQ.

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