Skip to content

Commit 58736f0

Browse files
author
Johannes Tax
authored
Set span start and end timestamps (#98)
1 parent 2973baa commit 58736f0

8 files changed

Lines changed: 61 additions & 15 deletions

File tree

api/include/opentelemetry/core/timestamp.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ class SystemTimestamp
4747
return nanos_since_epoch_ == other.nanos_since_epoch_;
4848
}
4949

50+
bool operator!=(const SystemTimestamp &other) const noexcept
51+
{
52+
return nanos_since_epoch_ != other.nanos_since_epoch_;
53+
}
54+
5055
private:
5156
int64_t nanos_since_epoch_;
5257
};
@@ -89,6 +94,11 @@ class SteadyTimestamp
8994
return nanos_since_epoch_ == other.nanos_since_epoch_;
9095
}
9196

97+
bool operator!=(const SteadyTimestamp &other) const noexcept
98+
{
99+
return nanos_since_epoch_ != other.nanos_since_epoch_;
100+
}
101+
92102
private:
93103
int64_t nanos_since_epoch_;
94104
};

api/include/opentelemetry/plugin/tracer.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class Span final : public trace::Span
3939

4040
void UpdateName(nostd::string_view name) noexcept override { span_->UpdateName(name); }
4141

42-
void End(const trace::EndSpanOptions &options = {}) noexcept override { span_->End(); }
42+
void End(const trace::EndSpanOptions &options = {}) noexcept override { span_->End(options); }
4343

4444
bool IsRecording() const noexcept override { return span_->IsRecording(); }
4545

api/include/opentelemetry/trace/noop.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class NoopSpan final : public Span
3838

3939
void UpdateName(nostd::string_view /*name*/) noexcept override {}
4040

41-
void End(const EndSpanOptions &options = {}) noexcept override {}
41+
void End(const EndSpanOptions & /*options*/) noexcept override {}
4242

4343
bool IsRecording() const noexcept override { return false; }
4444

api/include/opentelemetry/trace/span.h

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,13 @@ struct StartSpanOptions
4343
// Attributes
4444
SpanKind kind = SpanKind::kInternal;
4545
};
46-
46+
/**
47+
* StartEndOptions provides options to set properties of a Span when it is
48+
* ended.
49+
*/
4750
struct EndSpanOptions
4851
{
52+
// Optionally sets the end time of a Span.
4953
core::SteadyTimestamp end_steady_time;
5054
};
5155

@@ -135,8 +139,13 @@ class Span
135139
// during creation.
136140
virtual void UpdateName(nostd::string_view name) noexcept = 0;
137141

138-
// Mark the end of the Span. Only the timing of the first End call for a given Span will
139-
// be recorded, and implementations are free to ignore all further calls.
142+
/**
143+
* Mark the end of the Span.
144+
* Only the timing of the first End call for a given Span will be recorded,
145+
* and implementations are free to ignore all further calls.
146+
* @param options can be used to manually define span properties like the end
147+
* timestamp
148+
*/
140149
virtual void End(const EndSpanOptions &options = {}) noexcept = 0;
141150

142151
// TODO

api/test/core/timestamp_test.cc

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,19 @@ TEST(SystemTimestampTest, Construction)
2424
t2.time_since_epoch());
2525
}
2626

27+
TEST(SystemTimestampTest, Comparison)
28+
{
29+
SystemTimestamp t1;
30+
SystemTimestamp t2;
31+
SystemTimestamp t3{std::chrono::nanoseconds{2}};
32+
33+
EXPECT_EQ(t1, t1);
34+
EXPECT_EQ(t1, t2);
35+
EXPECT_EQ(t2, t1);
36+
EXPECT_NE(t1, t3);
37+
EXPECT_NE(t3, t1);
38+
}
39+
2740
TEST(SteadyTimestampTest, Construction)
2841
{
2942
auto now_steady = std::chrono::steady_clock::now();
@@ -36,3 +49,16 @@ TEST(SteadyTimestampTest, Construction)
3649
EXPECT_EQ(std::chrono::duration_cast<std::chrono::nanoseconds>(now_steady.time_since_epoch()),
3750
t2.time_since_epoch());
3851
}
52+
53+
TEST(SteadyTimestampTest, Comparison)
54+
{
55+
SteadyTimestamp t1;
56+
SteadyTimestamp t2;
57+
SteadyTimestamp t3{std::chrono::nanoseconds{2}};
58+
59+
EXPECT_EQ(t1, t1);
60+
EXPECT_EQ(t1, t2);
61+
EXPECT_EQ(t2, t1);
62+
EXPECT_NE(t1, t3);
63+
EXPECT_NE(t3, t1);
64+
}

examples/plugin/plugin/tracer.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class Span final : public trace::Span
1313
public:
1414
Span(std::shared_ptr<Tracer> &&tracer,
1515
nostd::string_view name,
16-
const trace::StartSpanOptions &options) noexcept
16+
const trace::StartSpanOptions & /*options*/) noexcept
1717
: tracer_{std::move(tracer)}, name_{name}
1818
{
1919
std::cout << "StartSpan: " << name << "\n";
@@ -38,7 +38,7 @@ class Span final : public trace::Span
3838

3939
void UpdateName(nostd::string_view /*name*/) noexcept override {}
4040

41-
void End(const trace::EndSpanOptions &options) noexcept override {}
41+
void End(const trace::EndSpanOptions & /*options*/) noexcept override {}
4242

4343
bool IsRecording() const noexcept override { return true; }
4444

sdk/src/trace/span.cc

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ using opentelemetry::core::SystemTimestamp;
1313

1414
namespace
1515
{
16-
SystemTimestamp NowOrGiven(const SystemTimestamp &system)
16+
SystemTimestamp NowOr(const SystemTimestamp &system)
1717
{
1818
if (system == SystemTimestamp())
1919
{
@@ -25,7 +25,7 @@ SystemTimestamp NowOrGiven(const SystemTimestamp &system)
2525
}
2626
}
2727

28-
SteadyTimestamp NowOrGiven(const SteadyTimestamp &steady)
28+
SteadyTimestamp NowOr(const SteadyTimestamp &steady)
2929
{
3030
if (steady == SteadyTimestamp())
3131
{
@@ -55,8 +55,8 @@ Span::Span(std::shared_ptr<Tracer> &&tracer,
5555
processor_->OnStart(*recordable_);
5656
recordable_->SetName(name);
5757

58-
recordable_->SetStartTime(NowOrGiven(options.start_system_time));
59-
start_steady_time = NowOrGiven(options.start_steady_time);
58+
recordable_->SetStartTime(NowOr(options.start_system_time));
59+
start_steady_time = NowOr(options.start_steady_time);
6060
}
6161

6262
Span::~Span()
@@ -112,9 +112,10 @@ void Span::End(const trace_api::EndSpanOptions &options) noexcept
112112
return;
113113
}
114114

115-
recordable_->SetDuration(
116-
std::chrono::steady_clock::time_point(NowOrGiven(options.end_steady_time)) -
117-
std::chrono::steady_clock::time_point(start_steady_time));
115+
auto end_steady_time = NowOr(options.end_steady_time);
116+
recordable_->SetDuration(std::chrono::steady_clock::time_point(end_steady_time) -
117+
std::chrono::steady_clock::time_point(start_steady_time));
118+
118119
processor_->OnEnd(std::move(recordable_));
119120
recordable_.reset();
120121
}

sdk/test/trace/tracer_test.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ TEST(Tracer, StartSpan)
9191
ASSERT_LT(std::chrono::nanoseconds(0), span_data->GetDuration());
9292
}
9393

94-
TEST(Tracer, StartSpanWithTime)
94+
TEST(Tracer, StartSpanWithOptions)
9595
{
9696
std::shared_ptr<std::vector<std::unique_ptr<SpanData>>> spans_received(
9797
new std::vector<std::unique_ptr<SpanData>>);

0 commit comments

Comments
 (0)