feat(c-binding): accept explicit span timestamps#309
Conversation
BenchmarksBenchmark execution time: 2026-04-21 15:21:04 Comparing candidate commit 616f1e5 in PR branch Found 0 performance improvements and 0 performance regressions! Performance is the same for 1 metrics, 0 unstable metrics.
|
2341445 to
79fa0bf
Compare
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 79fa0bfd18
ℹ️ 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".
db1bf1b to
2049e78
Compare
|
@codex review |
There was a problem hiding this comment.
Pull request overview
This PR extends the C binding to accept explicit wall-clock timestamps for span start/end, enabling FFI callers to avoid timing skew from crossing the boundary and to represent work that happened before span creation.
Changes:
- Add
start_time_nstodd_span_options_tand a newdd_span_finish_with_time(span, end_time_ns)API (with0preserving existing “use now” behavior). - Implement wall-nanoseconds to internal
dd::TimePointconversion and clamp end tick to start tick to prevent negative durations from serializing as huge unsigned values. - Extend the Catch2 C-binding test to cover explicit start/end timestamps and the “end before start” clamp behavior.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
| binding/c/include/datadog/c/tracer.h | Public C API updated with start_time_ns and dd_span_finish_with_time declaration. |
| binding/c/src/tracer.cpp | Implements wall-ns conversion, new finish function, and clamps negative durations. |
| binding/c/test/test_c_binding.cpp | Adds test coverage for explicit timestamps and end-before-start clamping. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| dd::TimePoint wall_ns_to_timepoint(int64_t wall_ns) { | ||
| const auto wall = std::chrono::system_clock::time_point( | ||
| std::chrono::duration_cast<std::chrono::system_clock::duration>( |
There was a problem hiding this comment.
wall_ns_to_timepoint converts the caller-provided nanoseconds to system_clock::duration via duration_cast, which will truncate on platforms where system_clock has coarser-than-nanosecond resolution. If the C API intends to promise nanosecond precision, consider documenting the effective precision (or rounding behavior) explicitly so callers don’t assume exact round-tripping.
| dd::TimePoint wall_ns_to_timepoint(int64_t wall_ns) { | |
| const auto wall = std::chrono::system_clock::time_point( | |
| std::chrono::duration_cast<std::chrono::system_clock::duration>( | |
| // The C API accepts nanoseconds, but system_clock may have coarser | |
| // resolution on some platforms. Round to the nearest representable | |
| // system_clock tick so the loss of precision is explicit and unbiased. | |
| dd::TimePoint wall_ns_to_timepoint(int64_t wall_ns) { | |
| const auto wall = std::chrono::system_clock::time_point( | |
| std::chrono::round<std::chrono::system_clock::duration>( |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 2049e78cf1
ℹ️ 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".
2049e78 to
ba2ee22
Compare
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: ba2ee22e3b
ℹ️ 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".
|
@codex review |
ba2ee22 to
f2b0803
Compare
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: f2b08038d9
ℹ️ 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".
f2b0803 to
0d25f79
Compare
0d25f79 to
3fe3518
Compare
b7ebbe6 to
f6c17d0
Compare
f6c17d0 to
c12d8ee
Compare
xlamorlette-datadog
left a comment
There was a problem hiding this comment.
Please review my comments, and I think we're good to go.
Per review feedback from @dmehala: 1. Rename dd_span_finish_with_time → dd_span_set_end_time to match the C++ Span::set_end_time and the existing dd_span_set_* naming family. 2. Expose DD_TRACE_CURRENT_TIME (-1) as the "use current time" sentinel for start_time_ns, so 0 (1970-01-01) stays a valid timestamp. 3. Drop the sentinel branch in dd_span_set_end_time; callers wanting the current end time use dd_span_finish, and this also removes the implicit-path clamp that was only defending against a future-start caller bug. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
c12d8ee to
616f1e5
Compare
Summary
Adds
start_time_nstodd_span_options_tanddd_span_set_end_time(span, end_time_ns)so FFI callers (Kong) can pass wall-clock nanoseconds captured before the FFI crossing instead of letting the binding record "now."start_time_ns = DD_TRACE_CURRENT_TIME(-1) preserves the current behaviour;dd_span_finishis unchanged.Notes
start.tickis derived from a current system/steady offset sample (the canonical C++17 pattern; C++20'sclock_castisn't available). Indd_span_set_end_time,end.tickis anchored tostart.tick + (end_wall − start_wall)so mid-span clock adjustments (NTP, VM suspend) don't distort the caller's reported duration.uint64_t, soend < startwould ship as a bogus huge value.dd_span_options_tis source-compatible (designated initializers zero-init the new field, which is then treated as the 1970-epoch); ABI grows by 8 bytes, so recompilation is required. Callers who want "use current time" must set.start_time_ns = DD_TRACE_CURRENT_TIMEexplicitly.