Skip to content
This repository was archived by the owner on Aug 30, 2022. It is now read-only.

Commit aeefccc

Browse files
authored
Set default-constructed timestamp to current time in StartSpan (#18)
1 parent a1bea40 commit aeefccc

File tree

3 files changed

+89
-4
lines changed

3 files changed

+89
-4
lines changed

src/jaegertracing/Span.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,15 +80,19 @@ void Span::SetBaggageItem(opentracing::string_view restrictedKey,
8080
void Span::FinishWithOptions(
8181
const opentracing::FinishSpanOptions& finishSpanOptions) noexcept
8282
{
83+
const auto finishTimeSteady =
84+
(finishSpanOptions.finish_steady_timestamp ==
85+
SteadyClock::time_point())
86+
? SteadyClock::now() : finishSpanOptions.finish_steady_timestamp;
8387
std::shared_ptr<const Tracer> tracer;
8488
{
89+
8590
std::lock_guard<std::mutex> lock(_mutex);
8691
if (isFinished()) {
8792
// Already finished, so return immediately.
8893
return;
8994
}
90-
_duration =
91-
finishSpanOptions.finish_steady_timestamp - _startTimeSteady;
95+
_duration = finishTimeSteady - _startTimeSteady;
9296
tracer = _tracer;
9397
}
9498

src/jaegertracing/Tracer.cpp

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,35 @@
1919
#include "jaegertracing/Reference.h"
2020

2121
namespace jaegertracing {
22+
namespace {
23+
24+
using SystemClock = Tracer::SystemClock;
25+
using SteadyClock = Tracer::SteadyClock;
26+
using TimePoints = std::tuple<SystemClock::time_point, SteadyClock::time_point>;
27+
28+
TimePoints determineStartTimes(const opentracing::StartSpanOptions& options)
29+
{
30+
if (options.start_system_timestamp == SystemClock::time_point() &&
31+
options.start_steady_timestamp == SteadyClock::time_point()) {
32+
return std::make_tuple(SystemClock::now(), SteadyClock::now());
33+
}
34+
if (options.start_system_timestamp == SystemClock::time_point()) {
35+
return std::make_tuple(
36+
opentracing::convert_time_point<SystemClock>(
37+
options.start_steady_timestamp),
38+
options.start_steady_timestamp);
39+
}
40+
if (options.start_steady_timestamp == SteadyClock::time_point()) {
41+
return std::make_tuple(
42+
options.start_system_timestamp,
43+
opentracing::convert_time_point<SteadyClock>(
44+
options.start_system_timestamp));
45+
}
46+
return std::make_tuple(options.start_system_timestamp,
47+
options.start_steady_timestamp);
48+
}
49+
50+
} // anonymous namespace
2251

2352
using StrMap = SpanContext::StrMap;
2453

@@ -75,10 +104,14 @@ Tracer::StartSpanWithOptions(string_view operationName,
75104
ctx = ctx.withBaggage(parent->baggage());
76105
}
77106

107+
SystemClock::time_point startTimeSystem;
108+
SteadyClock::time_point startTimeSteady;
109+
std::tie(startTimeSystem, startTimeSteady) =
110+
determineStartTimes(options);
78111
return startSpanInternal(ctx,
79112
operationName,
80-
options.start_system_timestamp,
81-
options.start_steady_timestamp,
113+
startTimeSystem,
114+
startTimeSteady,
82115
samplerTags,
83116
options.tags,
84117
newTrace,

src/jaegertracing/TracerTest.cpp

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,14 @@ struct ReaderMock : public BaseReader {
8181
const StrMap& _keyValuePairs;
8282
};
8383

84+
template <typename ClockType>
85+
typename ClockType::duration absTimeDiff(
86+
const typename ClockType::time_point& lhs,
87+
const typename ClockType::time_point& rhs)
88+
{
89+
return (rhs < lhs) ? (lhs - rhs) : (rhs - lhs);
90+
}
91+
8492
} // anonymous namespace
8593

8694
TEST(Tracer, testTracer)
@@ -161,6 +169,46 @@ TEST(Tracer, testTracer)
161169
tracer->StartSpanWithOptions("test-span-with-debug-parent", options)
162170
.release()));
163171

172+
options.references.clear();
173+
options.references.emplace_back(
174+
opentracing::SpanReferenceType::FollowsFromRef, &parentCtx);
175+
options.start_steady_timestamp = Tracer::SteadyClock::now();
176+
span.reset(static_cast<Span*>(
177+
tracer->StartSpanWithOptions("test-span-with-default-system-timestamp",
178+
options)
179+
.release()));
180+
const auto calculatedSystemTime =
181+
static_cast<Tracer::SystemClock::time_point>(
182+
opentracing::convert_time_point<Tracer::SystemClock>(
183+
span->startTimeSteady()));
184+
ASSERT_GE(
185+
std::chrono::milliseconds(10),
186+
absTimeDiff<Tracer::SystemClock>(
187+
span->startTimeSystem(), calculatedSystemTime));
188+
189+
options.start_system_timestamp = Tracer::SystemClock::now();
190+
span.reset(static_cast<Span*>(
191+
tracer->StartSpanWithOptions("test-span-with-default-steady-timestamp",
192+
options)
193+
.release()));
194+
const auto calculatedSteadyTime =
195+
static_cast<Tracer::SteadyClock::time_point>(
196+
opentracing::convert_time_point<Tracer::SteadyClock>(
197+
span->startTimeSystem()));
198+
ASSERT_GE(
199+
std::chrono::milliseconds(10),
200+
absTimeDiff<Tracer::SteadyClock>(
201+
span->startTimeSteady(), calculatedSteadyTime));
202+
203+
options.start_system_timestamp = Tracer::SystemClock::now();
204+
options.start_steady_timestamp = Tracer::SteadyClock::now();
205+
span.reset(static_cast<Span*>(
206+
tracer->StartSpanWithOptions("test-span-with-both-timestamps",
207+
options)
208+
.release()));
209+
ASSERT_EQ(options.start_system_timestamp, span->startTimeSystem());
210+
ASSERT_EQ(options.start_steady_timestamp, span->startTimeSteady());
211+
164212
span.reset();
165213

166214
opentracing::Tracer::InitGlobal(opentracing::MakeNoopTracer());

0 commit comments

Comments
 (0)