Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Johannes Tax committed Jun 6, 2020
1 parent 9db466a commit 29a77e4
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 10 deletions.
10 changes: 10 additions & 0 deletions api/include/opentelemetry/core/timestamp.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ class SystemTimestamp
return nanos_since_epoch_ == other.nanos_since_epoch_;
}

bool operator!=(const SystemTimestamp &other) const noexcept
{
return nanos_since_epoch_ != other.nanos_since_epoch_;
}

private:
int64_t nanos_since_epoch_;
};
Expand Down Expand Up @@ -89,6 +94,11 @@ class SteadyTimestamp
return nanos_since_epoch_ == other.nanos_since_epoch_;
}

bool operator!=(const SteadyTimestamp &other) const noexcept
{
return nanos_since_epoch_ != other.nanos_since_epoch_;
}

private:
int64_t nanos_since_epoch_;
};
Expand Down
15 changes: 12 additions & 3 deletions api/include/opentelemetry/trace/span.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,13 @@ struct StartSpanOptions
// Attributes
SpanKind kind = SpanKind::kInternal;
};

/**
* StartEndOptions provides options to set properties of a Span when it is
* ended.
*/
struct EndSpanOptions
{
// Optionally sets the end time of a Span.
core::SteadyTimestamp end_steady_time;
};

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

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

// TODO
Expand Down
26 changes: 26 additions & 0 deletions api/test/core/timestamp_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,19 @@ TEST(SystemTimestampTest, Construction)
t2.time_since_epoch());
}

TEST(SystemTimestampTest, Comparison)
{
SystemTimestamp t1;
SystemTimestamp t2;
SystemTimestamp t3{std::chrono::nanoseconds{2}};

EXPECT_EQ(t1, t1);
EXPECT_EQ(t1, t2);
EXPECT_EQ(t2, t1);
EXPECT_NE(t1, t3);
EXPECT_NE(t3, t1);
}

TEST(SteadyTimestampTest, Construction)
{
auto now_steady = std::chrono::steady_clock::now();
Expand All @@ -36,3 +49,16 @@ TEST(SteadyTimestampTest, Construction)
EXPECT_EQ(std::chrono::duration_cast<std::chrono::nanoseconds>(now_steady.time_since_epoch()),
t2.time_since_epoch());
}

TEST(SteadyTimestampTest, Comparison)
{
SteadyTimestamp t1;
SteadyTimestamp t2;
SteadyTimestamp t3{std::chrono::nanoseconds{2}};

EXPECT_EQ(t1, t1);
EXPECT_EQ(t1, t2);
EXPECT_EQ(t2, t1);
EXPECT_NE(t1, t3);
EXPECT_NE(t3, t1);
}
15 changes: 8 additions & 7 deletions sdk/src/trace/span.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ using opentelemetry::core::SystemTimestamp;

namespace
{
SystemTimestamp NowOrGiven(const SystemTimestamp &system)
SystemTimestamp NowOr(const SystemTimestamp &system)
{
if (system == SystemTimestamp())
{
Expand All @@ -25,7 +25,7 @@ SystemTimestamp NowOrGiven(const SystemTimestamp &system)
}
}

SteadyTimestamp NowOrGiven(const SteadyTimestamp &steady)
SteadyTimestamp NowOr(const SteadyTimestamp &steady)
{
if (steady == SteadyTimestamp())
{
Expand All @@ -52,8 +52,8 @@ Span::Span(std::shared_ptr<Tracer> &&tracer,
processor_->OnStart(*recordable_);
recordable_->SetName(name);

recordable_->SetStartTime(NowOrGiven(options.start_system_time));
start_steady_time = NowOrGiven(options.start_steady_time);
recordable_->SetStartTime(NowOr(options.start_system_time));
start_steady_time = NowOr(options.start_steady_time);
}

Span::~Span()
Expand Down Expand Up @@ -109,9 +109,10 @@ void Span::End(const trace_api::EndSpanOptions &options) noexcept
return;
}

recordable_->SetDuration(
std::chrono::steady_clock::time_point(NowOrGiven(options.end_steady_time)) -
std::chrono::steady_clock::time_point(start_steady_time));
auto end_steady_time = NowOr(options.end_steady_time);
recordable_->SetDuration(std::chrono::steady_clock::time_point(end_steady_time) -
std::chrono::steady_clock::time_point(start_steady_time));

processor_->OnEnd(std::move(recordable_));
recordable_.reset();
}
Expand Down

0 comments on commit 29a77e4

Please sign in to comment.