Skip to content

Commit 877de40

Browse files
committed
enhance: capture timestamp earlier for more accurate span timing
Capture the timestamp as close as posssible to the moment a span is created. This improves the accuracy of both the start time and duration measurements.
1 parent 4d64c46 commit 877de40

File tree

4 files changed

+17
-14
lines changed

4 files changed

+17
-14
lines changed

src/datadog/span.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ Span::Span(SpanData* data, const std::shared_ptr<TraceSegment>& trace_segment,
2828
}
2929

3030
Span::~Span() {
31+
const auto now = clock_();
3132
if (!trace_segment_) {
3233
// We were moved from.
3334
return;
@@ -36,16 +37,17 @@ Span::~Span() {
3637
if (end_time_) {
3738
data_->duration = *end_time_ - data_->start.tick;
3839
} else {
39-
const auto now = clock_();
4040
data_->duration = now - data_->start;
4141
}
4242

4343
trace_segment_->span_finished();
4444
}
4545

4646
Span Span::create_child(const SpanConfig& config) const {
47+
auto now = clock_();
4748
auto span_data = std::make_unique<SpanData>();
48-
span_data->apply_config(trace_segment_->defaults(), config, clock_);
49+
span_data->start = now;
50+
span_data->apply_config(trace_segment_->defaults(), config);
4951
span_data->trace_id = data_->trace_id;
5052
span_data->parent_id = data_->span_id;
5153
span_data->span_id = generate_span_id_();

src/datadog/span_data.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,11 @@ Optional<StringView> SpanData::version() const {
3636
}
3737

3838
void SpanData::apply_config(const SpanDefaults& defaults,
39-
const SpanConfig& config, const Clock& clock) {
39+
const SpanConfig& config) {
40+
if (config.start) {
41+
start = *config.start;
42+
}
43+
4044
std::string version;
4145
if (config.service) {
4246
service = *config.service;
@@ -66,11 +70,6 @@ void SpanData::apply_config(const SpanDefaults& defaults,
6670

6771
resource = config.resource.value_or(name);
6872
service_type = config.service_type.value_or(defaults.service_type);
69-
if (config.start) {
70-
start = *config.start;
71-
} else {
72-
start = clock();
73-
}
7473
}
7574

7675
Expected<void> msgpack_encode(std::string& destination, const SpanData& span) {

src/datadog/span_data.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,8 @@ struct SpanData {
3939

4040
// Modify the properties of this object to honor the specified `config` and
4141
// `defaults`. The properties of `config`, if set, override the properties of
42-
// `defaults`. Use the specified `clock` to provide a start none of none is
43-
// specified in `config`.
44-
void apply_config(const SpanDefaults& defaults, const SpanConfig& config,
45-
const Clock& clock);
42+
// `defaults`.
43+
void apply_config(const SpanDefaults& defaults, const SpanConfig& config);
4644
};
4745

4846
// Append to the specified `destination` the MessagePack representation of the

src/datadog/tracer.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,9 +168,11 @@ void Tracer::store_config() {
168168
Span Tracer::create_span() { return create_span(SpanConfig{}); }
169169

170170
Span Tracer::create_span(const SpanConfig& config) {
171+
auto now = clock_();
171172
auto defaults = config_manager_->span_defaults();
172173
auto span_data = std::make_unique<SpanData>();
173-
span_data->apply_config(*defaults, config, clock_);
174+
span_data->start = now;
175+
span_data->apply_config(*defaults, config);
174176
span_data->trace_id = generator_->trace_id(span_data->start);
175177
span_data->span_id = span_data->trace_id.low;
176178
span_data->parent_id = 0;
@@ -203,6 +205,7 @@ Expected<Span> Tracer::extract_span(const DictReader& reader) {
203205

204206
Expected<Span> Tracer::extract_span(const DictReader& reader,
205207
const SpanConfig& config) {
208+
auto now = clock_();
206209
assert(!extraction_styles_.empty());
207210

208211
AuditedReader audited_reader{reader};
@@ -335,7 +338,8 @@ Expected<Span> Tracer::extract_span(const DictReader& reader,
335338

336339
// We're done extracting fields. Now create the span.
337340
// This is similar to what we do in `create_span`.
338-
span_data->apply_config(*config_manager_->span_defaults(), config, clock_);
341+
span_data->start = now;
342+
span_data->apply_config(*config_manager_->span_defaults(), config);
339343
span_data->span_id = generator_->span_id();
340344
span_data->trace_id = *merged_context.trace_id;
341345
span_data->parent_id = *merged_context.parent_id;

0 commit comments

Comments
 (0)