Skip to content

feat: Add FFI for trace exporter#1952

Open
lloeki wants to merge 14 commits into
mainfrom
lloeki/ffi-for-trace-exporter
Open

feat: Add FFI for trace exporter#1952
lloeki wants to merge 14 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

📚 Documentation Check Results

⚠️ 1143 documentation warning(s) found

📦 libdd-data-pipeline-ffi - 1143 warning(s)


Updated: 2026-05-13 14:45:49 UTC | Commit: d1cf7f9 | missing-docs job results

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented May 6, 2026

🔒 Cargo Deny Results

⚠️ 5 issue(s) found, showing only errors (advisories, bans, sources)

📦 libdd-data-pipeline-ffi - 5 error(s)

Show output
error[unsound]: Rand is unsound with a custom logger using `rand::rng()`
    ┌─ /home/runner/work/libdatadog/libdatadog/Cargo.lock:220:1
    │
220 │ rand 0.8.5 registry+https://github.com/rust-lang/crates.io-index
    │ ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ unsound advisory detected
    │
    ├ ID: RUSTSEC-2026-0097
    ├ Advisory: https://rustsec.org/advisories/RUSTSEC-2026-0097
    ├ It has been reported (by @lopopolo) that the `rand` library is [unsound](https://rust-lang.github.io/unsafe-code-guidelines/glossary.html#soundness-of-code--of-a-library) (i.e. that safe code using the public API can cause Undefined Behaviour) when all the following conditions are met:
      
      - The `log` and `thread_rng` features are enabled
      - A [custom logger](https://docs.rs/log/latest/log/#implementing-a-logger) is defined
      - The custom logger accesses `rand::rng()` (previously `rand::thread_rng()`) and calls any `TryRng` (previously `RngCore`) methods on `ThreadRng`
      - The `ThreadRng` (attempts to) reseed while called from the custom logger (this happens every 64 kB of generated data)
      - Trace-level logging is enabled or warn-level logging is enabled and the random source (the `getrandom` crate) is unable to provide a new seed
      
      `TryRng` (previously `RngCore`) methods for `ThreadRng` use `unsafe` code to cast `*mut BlockRng<ReseedingCore>` to `&mut BlockRng<ReseedingCore>`. When all the above conditions are met this results in an aliased mutable reference, violating the Stacked Borrows rules. Miri is able to detect this violation in sample code. Since construction of [aliased mutable references is Undefined Behaviour](https://doc.rust-lang.org/stable/nomicon/references.html), the behaviour of optimized builds is hard to predict.
    ├ Announcement: https://github.com/rust-random/rand/pull/1763
    ├ Solution: Upgrade to >=0.10.1 OR <0.10.0, >=0.9.3 OR <0.9.0, >=0.8.6 (try `cargo update -p rand`)
    ├ rand v0.8.5
      ├── libdd-common v4.0.0
      │   ├── libdd-capabilities-impl v1.0.0
      │   │   ├── libdd-data-pipeline v3.0.1
      │   │   │   └── libdd-data-pipeline-ffi v33.0.0
      │   │   ├── libdd-data-pipeline-ffi v33.0.0 (*)
      │   │   ├── libdd-shared-runtime v0.1.0
      │   │   │   ├── libdd-data-pipeline v3.0.1 (*)
      │   │   │   ├── libdd-data-pipeline-ffi v33.0.0 (*)
      │   │   │   ├── libdd-telemetry v5.0.0
      │   │   │   │   └── libdd-data-pipeline v3.0.1 (*)
      │   │   │   └── libdd-trace-stats v2.0.0
      │   │   │       └── libdd-data-pipeline v3.0.1 (*)
      │   │   ├── libdd-trace-stats v2.0.0 (*)
      │   │   └── libdd-trace-utils v3.0.1
      │   │       ├── libdd-data-pipeline v3.0.1 (*)
      │   │       ├── (dev) libdd-data-pipeline-ffi v33.0.0 (*)
      │   │       ├── libdd-trace-obfuscation v2.0.0
      │   │       │   └── libdd-trace-stats v2.0.0 (*)
      │   │       ├── libdd-trace-stats v2.0.0 (*)
      │   │       └── (dev) libdd-trace-utils v3.0.1 (*)
      │   ├── libdd-common-ffi v33.0.0
      │   │   └── libdd-data-pipeline-ffi v33.0.0 (*)
      │   ├── libdd-data-pipeline v3.0.1 (*)
      │   ├── libdd-dogstatsd-client v2.0.0
      │   │   └── libdd-data-pipeline v3.0.1 (*)
      │   ├── libdd-shared-runtime v0.1.0 (*)
      │   ├── libdd-telemetry v5.0.0 (*)
      │   ├── libdd-trace-obfuscation v2.0.0 (*)
      │   ├── libdd-trace-stats v2.0.0 (*)
      │   └── libdd-trace-utils v3.0.1 (*)
      ├── (dev) libdd-data-pipeline v3.0.1 (*)
      ├── (dev) libdd-trace-normalization v2.0.0
      │   └── libdd-trace-utils v3.0.1 (*)
      ├── (dev) libdd-trace-stats v2.0.0 (*)
      ├── libdd-trace-utils v3.0.1 (*)
      └── proptest v1.5.0
          └── (dev) libdd-tinybytes v1.1.0
              ├── libdd-data-pipeline v3.0.1 (*)
              ├── libdd-data-pipeline-ffi v33.0.0 (*)
              ├── (dev) libdd-tinybytes v1.1.0 (*)
              └── libdd-trace-utils v3.0.1 (*)

error[vulnerability]: Name constraints for URI names were incorrectly accepted
    ┌─ /home/runner/work/libdatadog/libdatadog/Cargo.lock:244:1
    │
244 │ rustls-webpki 0.103.10 registry+https://github.com/rust-lang/crates.io-index
    │ ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ security vulnerability detected
    │
    ├ ID: RUSTSEC-2026-0098
    ├ Advisory: https://rustsec.org/advisories/RUSTSEC-2026-0098
    ├ Name constraints for URI names were ignored and therefore accepted.
      
      Note this library does not provide an API for asserting URI names, and URI name constraints are otherwise not implemented.  URI name constraints are now rejected unconditionally.
      
      Since name constraints are restrictions on otherwise properly-issued certificates, this bug is reachable only after signature verification and requires misissuance to exploit.
      
      This vulnerability is identified as [GHSA-965h-392x-2mh5](https://github.com/rustls/webpki/security/advisories/GHSA-965h-392x-2mh5). Thank you to @1seal for the report.
    ├ Solution: Upgrade to >=0.103.12, <0.104.0-alpha.1 OR >=0.104.0-alpha.6 (try `cargo update -p rustls-webpki`)
    ├ rustls-webpki v0.103.10
      └── rustls v0.23.37
          ├── hyper-rustls v0.27.7
          │   └── libdd-common v4.0.0
          │       ├── libdd-capabilities-impl v1.0.0
          │       │   ├── libdd-data-pipeline v3.0.1
          │       │   │   └── libdd-data-pipeline-ffi v33.0.0
          │       │   ├── libdd-data-pipeline-ffi v33.0.0 (*)
          │       │   ├── libdd-shared-runtime v0.1.0
          │       │   │   ├── libdd-data-pipeline v3.0.1 (*)
          │       │   │   ├── libdd-data-pipeline-ffi v33.0.0 (*)
          │       │   │   ├── libdd-telemetry v5.0.0
          │       │   │   │   └── libdd-data-pipeline v3.0.1 (*)
          │       │   │   └── libdd-trace-stats v2.0.0
          │       │   │       └── libdd-data-pipeline v3.0.1 (*)
          │       │   ├── libdd-trace-stats v2.0.0 (*)
          │       │   └── libdd-trace-utils v3.0.1
          │       │       ├── libdd-data-pipeline v3.0.1 (*)
          │       │       ├── (dev) libdd-data-pipeline-ffi v33.0.0 (*)
          │       │       ├── libdd-trace-obfuscation v2.0.0
          │       │       │   └── libdd-trace-stats v2.0.0 (*)
          │       │       ├── libdd-trace-stats v2.0.0 (*)
          │       │       └── (dev) libdd-trace-utils v3.0.1 (*)
          │       ├── libdd-common-ffi v33.0.0
          │       │   └── libdd-data-pipeline-ffi v33.0.0 (*)
          │       ├── libdd-data-pipeline v3.0.1 (*)
          │       ├── libdd-dogstatsd-client v2.0.0
          │       │   └── libdd-data-pipeline v3.0.1 (*)
          │       ├── libdd-shared-runtime v0.1.0 (*)
          │       ├── libdd-telemetry v5.0.0 (*)
          │       ├── libdd-trace-obfuscation v2.0.0 (*)
          │       ├── libdd-trace-stats v2.0.0 (*)
          │       └── libdd-trace-utils v3.0.1 (*)
          ├── libdd-common v4.0.0 (*)
          └── tokio-rustls v0.26.0
              ├── hyper-rustls v0.27.7 (*)
              └── libdd-common v4.0.0 (*)

error[vulnerability]: Name constraints were accepted for certificates asserting a wildcard name
    ┌─ /home/runner/work/libdatadog/libdatadog/Cargo.lock:244:1
    │
244 │ rustls-webpki 0.103.10 registry+https://github.com/rust-lang/crates.io-index
    │ ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ security vulnerability detected
    │
    ├ ID: RUSTSEC-2026-0099
    ├ Advisory: https://rustsec.org/advisories/RUSTSEC-2026-0099
    ├ Permitted subtree name constraints for DNS names were accepted for certificates asserting a wildcard name.
      
      This was incorrect because, given a name constraint of `accept.example.com`, `*.example.com` could feasibly allow a name of `reject.example.com` which is outside the constraint.
      This is very similar to [CVE-2025-61727](https://go.dev/issue/76442).
      
      Since name constraints are restrictions on otherwise properly-issued certificates, this bug is reachable only after signature verification and requires misissuance to exploit.
      
      This vulnerability is identified as [GHSA-xgp8-3hg3-c2mh](https://github.com/rustls/webpki/security/advisories/GHSA-xgp8-3hg3-c2mh). Thank you to @1seal for the report.
    ├ Solution: Upgrade to >=0.103.12, <0.104.0-alpha.1 OR >=0.104.0-alpha.6 (try `cargo update -p rustls-webpki`)
    ├ rustls-webpki v0.103.10
      └── rustls v0.23.37
          ├── hyper-rustls v0.27.7
          │   └── libdd-common v4.0.0
          │       ├── libdd-capabilities-impl v1.0.0
          │       │   ├── libdd-data-pipeline v3.0.1
          │       │   │   └── libdd-data-pipeline-ffi v33.0.0
          │       │   ├── libdd-data-pipeline-ffi v33.0.0 (*)
          │       │   ├── libdd-shared-runtime v0.1.0
          │       │   │   ├── libdd-data-pipeline v3.0.1 (*)
          │       │   │   ├── libdd-data-pipeline-ffi v33.0.0 (*)
          │       │   │   ├── libdd-telemetry v5.0.0
          │       │   │   │   └── libdd-data-pipeline v3.0.1 (*)
          │       │   │   └── libdd-trace-stats v2.0.0
          │       │   │       └── libdd-data-pipeline v3.0.1 (*)
          │       │   ├── libdd-trace-stats v2.0.0 (*)
          │       │   └── libdd-trace-utils v3.0.1
          │       │       ├── libdd-data-pipeline v3.0.1 (*)
          │       │       ├── (dev) libdd-data-pipeline-ffi v33.0.0 (*)
          │       │       ├── libdd-trace-obfuscation v2.0.0
          │       │       │   └── libdd-trace-stats v2.0.0 (*)
          │       │       ├── libdd-trace-stats v2.0.0 (*)
          │       │       └── (dev) libdd-trace-utils v3.0.1 (*)
          │       ├── libdd-common-ffi v33.0.0
          │       │   └── libdd-data-pipeline-ffi v33.0.0 (*)
          │       ├── libdd-data-pipeline v3.0.1 (*)
          │       ├── libdd-dogstatsd-client v2.0.0
          │       │   └── libdd-data-pipeline v3.0.1 (*)
          │       ├── libdd-shared-runtime v0.1.0 (*)
          │       ├── libdd-telemetry v5.0.0 (*)
          │       ├── libdd-trace-obfuscation v2.0.0 (*)
          │       ├── libdd-trace-stats v2.0.0 (*)
          │       └── libdd-trace-utils v3.0.1 (*)
          ├── libdd-common v4.0.0 (*)
          └── tokio-rustls v0.26.0
              ├── hyper-rustls v0.27.7 (*)
              └── libdd-common v4.0.0 (*)

error[vulnerability]: Reachable panic in certificate revocation list parsing
    ┌─ /home/runner/work/libdatadog/libdatadog/Cargo.lock:244:1
    │
244 │ rustls-webpki 0.103.10 registry+https://github.com/rust-lang/crates.io-index
    │ ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ security vulnerability detected
    │
    ├ ID: RUSTSEC-2026-0104
    ├ Advisory: https://rustsec.org/advisories/RUSTSEC-2026-0104
    ├ A panic was reachable when parsing certificate revocation lists via [`BorrowedCertRevocationList::from_der`]
      or [`OwnedCertRevocationList::from_der`].  This was the result of mishandling a syntactically valid empty
      `BIT STRING` appearing in the `onlySomeReasons` element of a `IssuingDistributionPoint` CRL extension.
      
      This panic is reachable prior to a CRL's signature being verified.
      
      Applications that do not use CRLs are not affected.
      
      Thank you to @tynus3 for the report.
    ├ Solution: Upgrade to >=0.103.13, <0.104.0-alpha.1 OR >=0.104.0-alpha.7 (try `cargo update -p rustls-webpki`)
    ├ rustls-webpki v0.103.10
      └── rustls v0.23.37
          ├── hyper-rustls v0.27.7
          │   └── libdd-common v4.0.0
          │       ├── libdd-capabilities-impl v1.0.0
          │       │   ├── libdd-data-pipeline v3.0.1
          │       │   │   └── libdd-data-pipeline-ffi v33.0.0
          │       │   ├── libdd-data-pipeline-ffi v33.0.0 (*)
          │       │   ├── libdd-shared-runtime v0.1.0
          │       │   │   ├── libdd-data-pipeline v3.0.1 (*)
          │       │   │   ├── libdd-data-pipeline-ffi v33.0.0 (*)
          │       │   │   ├── libdd-telemetry v5.0.0
          │       │   │   │   └── libdd-data-pipeline v3.0.1 (*)
          │       │   │   └── libdd-trace-stats v2.0.0
          │       │   │       └── libdd-data-pipeline v3.0.1 (*)
          │       │   ├── libdd-trace-stats v2.0.0 (*)
          │       │   └── libdd-trace-utils v3.0.1
          │       │       ├── libdd-data-pipeline v3.0.1 (*)
          │       │       ├── (dev) libdd-data-pipeline-ffi v33.0.0 (*)
          │       │       ├── libdd-trace-obfuscation v2.0.0
          │       │       │   └── libdd-trace-stats v2.0.0 (*)
          │       │       ├── libdd-trace-stats v2.0.0 (*)
          │       │       └── (dev) libdd-trace-utils v3.0.1 (*)
          │       ├── libdd-common-ffi v33.0.0
          │       │   └── libdd-data-pipeline-ffi v33.0.0 (*)
          │       ├── libdd-data-pipeline v3.0.1 (*)
          │       ├── libdd-dogstatsd-client v2.0.0
          │       │   └── libdd-data-pipeline v3.0.1 (*)
          │       ├── libdd-shared-runtime v0.1.0 (*)
          │       ├── libdd-telemetry v5.0.0 (*)
          │       ├── libdd-trace-obfuscation v2.0.0 (*)
          │       ├── libdd-trace-stats v2.0.0 (*)
          │       └── libdd-trace-utils v3.0.1 (*)
          ├── libdd-common v4.0.0 (*)
          └── tokio-rustls v0.26.0
              ├── hyper-rustls v0.27.7 (*)
              └── libdd-common v4.0.0 (*)

error[vulnerability]: Denial of Service via Stack Exhaustion
    ┌─ /home/runner/work/libdatadog/libdatadog/Cargo.lock:285:1
    │
285 │ time 0.3.41 registry+https://github.com/rust-lang/crates.io-index
    │ ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ security vulnerability detected
    │
    ├ ID: RUSTSEC-2026-0009
    ├ Advisory: https://rustsec.org/advisories/RUSTSEC-2026-0009
    ├ ## Impact
      
      When user-provided input is provided to any type that parses with the RFC 2822 format, a denial of
      service attack via stack exhaustion is possible. The attack relies on formally deprecated and
      rarely-used features that are part of the RFC 2822 format used in a malicious manner. Ordinary,
      non-malicious input will never encounter this scenario.
      
      ## Patches
      
      A limit to the depth of recursion was added in v0.3.47. From this version, an error will be returned
      rather than exhausting the stack.
      
      ## Workarounds
      
      Limiting the length of user input is the simplest way to avoid stack exhaustion, as the amount of
      the stack consumed would be at most a factor of the length of the input.
    ├ Announcement: https://github.com/time-rs/time/blob/main/CHANGELOG.md#0347-2026-02-05
    ├ Solution: Upgrade to >=0.3.47 (try `cargo update -p time`)
    ├ time v0.3.41
      └── tracing-appender v0.2.3
          └── libdd-log v1.0.0
              └── (dev) libdd-data-pipeline v3.0.1
                  └── libdd-data-pipeline-ffi v33.0.0

advisories FAILED, bans ok, sources ok

Updated: 2026-05-13 14:47:27 UTC | Commit: d1cf7f9 | dependency-check job results

@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 10 ✅ -3 (-23.1%)
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 8 8 No change (0%)
libdd-trace-stats 1 1 No change (0%)
libdd-trace-utils 15 15 No change (0%)
Total 203 200 ✅ -3 (-1.5%)

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!

❄️ No new flaky tests detected
🧪 All tests passed

🎯 Code Coverage (details)
Patch Coverage: 79.80%
Overall Coverage: 72.76% (+0.96%)

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

@codecov-commenter
Copy link
Copy Markdown

codecov-commenter commented May 6, 2026

Codecov Report

❌ Patch coverage is 79.80456% with 62 lines in your changes missing coverage. Please review.
✅ Project coverage is 72.75%. Comparing base (4ae8ebe) to head (9d7998b).
⚠️ Report is 20 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1952      +/-   ##
==========================================
+ Coverage   71.79%   72.75%   +0.96%     
==========================================
  Files         434      452      +18     
  Lines       70598    74562    +3964     
==========================================
+ Hits        50687    54249    +3562     
- Misses      19911    20313     +402     
Components Coverage Δ
libdd-crashtracker 65.29% <ø> (-0.68%) ⬇️
libdd-crashtracker-ffi 37.68% <ø> (+3.58%) ⬆️
libdd-alloc 98.77% <ø> (ø)
libdd-data-pipeline 86.18% <79.80%> (-0.07%) ⬇️
libdd-data-pipeline-ffi 75.70% <79.80%> (+1.77%) ⬆️
libdd-common 79.81% <ø> (+0.23%) ⬆️
libdd-common-ffi 74.41% <ø> (+0.54%) ⬆️
libdd-telemetry 73.37% <ø> (+4.10%) ⬆️
libdd-telemetry-ffi 31.36% <ø> (+11.99%) ⬆️
libdd-dogstatsd-client 82.64% <ø> (ø)
datadog-ipc 74.75% <ø> (ø)
libdd-profiling 81.56% <ø> (-0.05%) ⬇️
libdd-profiling-ffi 64.51% <ø> (+0.15%) ⬆️
datadog-sidecar 29.09% <ø> (+0.47%) ⬆️
datdog-sidecar-ffi 9.67% <ø> (+2.36%) ⬆️
spawn-worker 54.69% <ø> (ø)
libdd-tinybytes 93.80% <ø> (+0.64%) ⬆️
libdd-trace-normalization 81.71% <ø> (ø)
libdd-trace-obfuscation 87.39% <ø> (+0.13%) ⬆️
libdd-trace-protobuf 68.25% <ø> (ø)
libdd-trace-utils 88.85% <ø> (-0.42%) ⬇️
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 from 303d914 to 2b39efc Compare May 6, 2026 17:10
lloeki added 3 commits May 6, 2026 19:18
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.
@lloeki lloeki force-pushed the lloeki/ffi-for-trace-exporter branch 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 81.47 MB 82.25 MB +.94% (+792.18 KB) 🔍
/aarch64-alpine-linux-musl/lib/libdatadog_profiling.so 7.51 MB 7.57 MB +.83% (+64.00 KB) 🔍
aarch64-unknown-linux-gnu
Artifact Baseline Commit Change
/aarch64-unknown-linux-gnu/lib/libdatadog_profiling.so 9.93 MB 10.01 MB +.81% (+83.21 KB) 🔍
/aarch64-unknown-linux-gnu/lib/libdatadog_profiling.a 97.62 MB 98.51 MB +.91% (+909.79 KB) 🔍
libdatadog-x64-windows
Artifact Baseline Commit Change
/libdatadog-x64-windows/debug/dynamic/datadog_profiling_ffi.dll 24.38 MB 24.59 MB +.88% (+220.50 KB) 🔍
/libdatadog-x64-windows/debug/dynamic/datadog_profiling_ffi.lib 79.57 KB 82.05 KB +3.11% (+2.48 KB) ⚠️
/libdatadog-x64-windows/debug/dynamic/datadog_profiling_ffi.pdb 179.48 MB 180.58 MB +.61% (+1.10 MB) 🔍
/libdatadog-x64-windows/debug/static/datadog_profiling_ffi.lib 910.25 MB 914.01 MB +.41% (+3.76 MB) 🔍
/libdatadog-x64-windows/release/dynamic/datadog_profiling_ffi.dll 7.71 MB 7.81 MB +1.39% (+110.50 KB) ⚠️
/libdatadog-x64-windows/release/dynamic/datadog_profiling_ffi.lib 79.57 KB 82.05 KB +3.11% (+2.48 KB) ⚠️
/libdatadog-x64-windows/release/dynamic/datadog_profiling_ffi.pdb 23.07 MB 23.30 MB +1.01% (+240.00 KB) ⚠️
/libdatadog-x64-windows/release/static/datadog_profiling_ffi.lib 45.17 MB 45.63 MB +1.00% (+465.49 KB) ⚠️
libdatadog-x86-windows
Artifact Baseline Commit Change
/libdatadog-x86-windows/debug/dynamic/datadog_profiling_ffi.dll 20.99 MB 21.18 MB +.91% (+196.00 KB) 🔍
/libdatadog-x86-windows/debug/dynamic/datadog_profiling_ffi.lib 80.81 KB 83.34 KB +3.12% (+2.52 KB) ⚠️
/libdatadog-x86-windows/debug/dynamic/datadog_profiling_ffi.pdb 183.62 MB 184.83 MB +.65% (+1.20 MB) 🔍
/libdatadog-x86-windows/debug/static/datadog_profiling_ffi.lib 896.28 MB 900.03 MB +.41% (+3.74 MB) 🔍
/libdatadog-x86-windows/release/dynamic/datadog_profiling_ffi.dll 5.97 MB 6.04 MB +1.11% (+68.50 KB) ⚠️
/libdatadog-x86-windows/release/dynamic/datadog_profiling_ffi.lib 80.81 KB 83.34 KB +3.12% (+2.52 KB) ⚠️
/libdatadog-x86-windows/release/dynamic/datadog_profiling_ffi.pdb 24.69 MB 24.95 MB +1.04% (+264.00 KB) ⚠️
/libdatadog-x86-windows/release/static/datadog_profiling_ffi.lib 42.68 MB 43.09 MB +.94% (+412.96 KB) 🔍
x86_64-alpine-linux-musl
Artifact Baseline Commit Change
/x86_64-alpine-linux-musl/lib/libdatadog_profiling.a 72.59 MB 73.29 MB +.95% (+712.91 KB) 🔍
/x86_64-alpine-linux-musl/lib/libdatadog_profiling.so 8.37 MB 8.44 MB +.83% (+72.00 KB) 🔍
x86_64-unknown-linux-gnu
Artifact Baseline Commit Change
/x86_64-unknown-linux-gnu/lib/libdatadog_profiling.a 90.34 MB 91.12 MB +.86% (+801.87 KB) 🔍
/x86_64-unknown-linux-gnu/lib/libdatadog_profiling.so 9.99 MB 10.07 MB +.80% (+82.46 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
lloeki added 7 commits May 7, 2026 14:02
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.
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 added 4 commits May 13, 2026 13:11
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 requested a review from yannham May 13, 2026 15:27
@lloeki lloeki requested a review from hoolioh 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.

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