Skip to content

Commit 2341445

Browse files
adeshkumar1claude
andcommitted
refactor(c-binding): trim explicit-timestamp change
Drop transitive includes, inline locals in the helper and in the test, and compress comments to match the surrounding file style. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent 3b12728 commit 2341445

3 files changed

Lines changed: 14 additions & 28 deletions

File tree

binding/c/include/datadog/c/tracer.h

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,7 @@ typedef struct {
5252
const char* service_type;
5353
const char* environment;
5454
const char* version;
55-
// Wall-clock span start time in nanoseconds since the UNIX epoch.
56-
// 0 means "use the current time".
57-
int64_t start_time_ns;
55+
int64_t start_time_ns; // UNIX-epoch nanoseconds; 0 = use current time
5856
} dd_span_options_t;
5957

6058
// Error codes returned by the C binding.
@@ -183,12 +181,10 @@ DD_TRACE_C_API dd_span_t* dd_span_create_child(dd_span_t* span_handle,
183181
// @param span_handle Span handle
184182
DD_TRACE_C_API void dd_span_finish(dd_span_t* span_handle);
185183

186-
// Finish a span using an explicit end time given as wall-clock nanoseconds
187-
// since the UNIX epoch. If end_time_ns is 0, behaves like dd_span_finish
188-
// (uses the current time). No-op if span_handle is NULL.
184+
// Finish a span using an explicit end time. No-op if span_handle is NULL.
189185
//
190186
// @param span_handle Span handle
191-
// @param end_time_ns Wall-clock end time in nanoseconds since the UNIX epoch
187+
// @param end_time_ns Wall-clock end time (UNIX-epoch ns); 0 = current time
192188
DD_TRACE_C_API void dd_span_finish_with_time(dd_span_t* span_handle,
193189
int64_t end_time_ns);
194190

binding/c/src/tracer.cpp

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
#include "datadog/c/tracer.h"
22

3-
#include <datadog/clock.h>
43
#include <datadog/hex.h>
54
#include <datadog/trace_segment.h>
65
#include <datadog/tracer.h>
76

8-
#include <chrono>
97
#include <cstddef>
108
#include <cstring>
119
#include <string>
@@ -46,19 +44,15 @@ class ContextWriter : public dd::DictWriter {
4644
}
4745
};
4846

49-
// Convert a wall-clock timestamp (nanoseconds since UNIX epoch) into a
50-
// TimePoint. The steady-clock tick is derived from the current offset
51-
// between system_clock::now() and steady_clock::now() so that duration
52-
// calculations (which are done in steady-clock space) remain meaningful.
47+
// tick is derived from the current system/steady offset so that duration
48+
// (end.tick - start.tick) stays correct across FFI-supplied timestamps.
5349
dd::TimePoint wall_ns_to_timepoint(int64_t wall_ns) {
5450
const auto wall = std::chrono::system_clock::time_point(
5551
std::chrono::duration_cast<std::chrono::system_clock::duration>(
5652
std::chrono::nanoseconds(wall_ns)));
57-
const auto now_wall = std::chrono::system_clock::now();
58-
const auto now_tick = std::chrono::steady_clock::now();
59-
const auto offset = now_wall - wall;
60-
return dd::TimePoint{
61-
wall, now_tick - std::chrono::duration_cast<dd::Duration>(offset)};
53+
const auto offset = std::chrono::system_clock::now() - wall;
54+
return {wall, std::chrono::steady_clock::now() -
55+
std::chrono::duration_cast<dd::Duration>(offset)};
6256
}
6357

6458
dd::SpanConfig make_span_config(dd_span_options_t options) {

binding/c/test/test_c_binding.cpp

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
#include <datadog/c/tracer.h>
22
#include <datadog/tracer.h>
33

4-
#include <chrono>
5-
64
#include "mocks/collectors.h"
75
#include "null_logger.h"
86
#include "test.h"
@@ -116,14 +114,12 @@ TEST_CASE("span create, tag, finish, free", "[c_binding]") {
116114
CHECK(sd.error == true);
117115
CHECK(sd.tags.at("error.message") == "something broke");
118116

119-
const auto start_wall_ns =
120-
std::chrono::duration_cast<std::chrono::nanoseconds>(
121-
sd.start.wall.time_since_epoch())
122-
.count();
123-
CHECK(start_wall_ns == start_ns);
124-
const auto diff =
125-
sd.duration - std::chrono::nanoseconds(end_ns - start_ns);
126-
CHECK(std::chrono::abs(diff) < std::chrono::milliseconds(1));
117+
CHECK(std::chrono::duration_cast<std::chrono::nanoseconds>(
118+
sd.start.wall.time_since_epoch())
119+
.count() == start_ns);
120+
CHECK(std::chrono::abs(sd.duration -
121+
std::chrono::nanoseconds(end_ns - start_ns)) <
122+
std::chrono::milliseconds(1));
127123
}
128124

129125
TEST_CASE("create span with resource", "[c_binding]") {

0 commit comments

Comments
 (0)