diff --git a/api/include/opentelemetry/core/timestamp.h b/api/include/opentelemetry/core/timestamp.h index 7ab7da6f5d..c76edd76f4 100644 --- a/api/include/opentelemetry/core/timestamp.h +++ b/api/include/opentelemetry/core/timestamp.h @@ -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_; }; @@ -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_; }; diff --git a/api/include/opentelemetry/plugin/tracer.h b/api/include/opentelemetry/plugin/tracer.h index 4f00be0c32..3d308e5826 100644 --- a/api/include/opentelemetry/plugin/tracer.h +++ b/api/include/opentelemetry/plugin/tracer.h @@ -39,7 +39,7 @@ class Span final : public trace::Span void UpdateName(nostd::string_view name) noexcept override { span_->UpdateName(name); } - void End(const trace::EndSpanOptions &options = {}) noexcept override { span_->End(); } + void End(const trace::EndSpanOptions &options = {}) noexcept override { span_->End(options); } bool IsRecording() const noexcept override { return span_->IsRecording(); } diff --git a/api/include/opentelemetry/trace/noop.h b/api/include/opentelemetry/trace/noop.h index 82b10b9a71..50dcc8b558 100644 --- a/api/include/opentelemetry/trace/noop.h +++ b/api/include/opentelemetry/trace/noop.h @@ -38,7 +38,7 @@ class NoopSpan final : public Span void UpdateName(nostd::string_view /*name*/) noexcept override {} - void End(const EndSpanOptions &options = {}) noexcept override {} + void End(const EndSpanOptions & /*options*/) noexcept override {} bool IsRecording() const noexcept override { return false; } diff --git a/api/include/opentelemetry/trace/span.h b/api/include/opentelemetry/trace/span.h index f7b0aebd94..21ed1aaaff 100644 --- a/api/include/opentelemetry/trace/span.h +++ b/api/include/opentelemetry/trace/span.h @@ -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; }; @@ -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 diff --git a/api/test/core/timestamp_test.cc b/api/test/core/timestamp_test.cc index 140092942a..7ea4484321 100644 --- a/api/test/core/timestamp_test.cc +++ b/api/test/core/timestamp_test.cc @@ -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(); @@ -36,3 +49,16 @@ TEST(SteadyTimestampTest, Construction) EXPECT_EQ(std::chrono::duration_cast(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); +} diff --git a/examples/plugin/plugin/tracer.cc b/examples/plugin/plugin/tracer.cc index 18ab36a03b..c6e646d5d0 100644 --- a/examples/plugin/plugin/tracer.cc +++ b/examples/plugin/plugin/tracer.cc @@ -13,7 +13,7 @@ class Span final : public trace::Span public: Span(std::shared_ptr &&tracer, nostd::string_view name, - const trace::StartSpanOptions &options) noexcept + const trace::StartSpanOptions & /*options*/) noexcept : tracer_{std::move(tracer)}, name_{name} { std::cout << "StartSpan: " << name << "\n"; @@ -38,7 +38,7 @@ class Span final : public trace::Span void UpdateName(nostd::string_view /*name*/) noexcept override {} - void End(const trace::EndSpanOptions &options) noexcept override {} + void End(const trace::EndSpanOptions & /*options*/) noexcept override {} bool IsRecording() const noexcept override { return true; } diff --git a/sdk/src/trace/span.cc b/sdk/src/trace/span.cc index fbe2923dcf..a69e6673b0 100644 --- a/sdk/src/trace/span.cc +++ b/sdk/src/trace/span.cc @@ -13,7 +13,7 @@ using opentelemetry::core::SystemTimestamp; namespace { -SystemTimestamp NowOrGiven(const SystemTimestamp &system) +SystemTimestamp NowOr(const SystemTimestamp &system) { if (system == SystemTimestamp()) { @@ -25,7 +25,7 @@ SystemTimestamp NowOrGiven(const SystemTimestamp &system) } } -SteadyTimestamp NowOrGiven(const SteadyTimestamp &steady) +SteadyTimestamp NowOr(const SteadyTimestamp &steady) { if (steady == SteadyTimestamp()) { @@ -55,8 +55,8 @@ Span::Span(std::shared_ptr &&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() @@ -112,9 +112,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(); } diff --git a/sdk/test/trace/tracer_test.cc b/sdk/test/trace/tracer_test.cc index 92167fe3b1..e0bc2afde6 100644 --- a/sdk/test/trace/tracer_test.cc +++ b/sdk/test/trace/tracer_test.cc @@ -91,7 +91,7 @@ TEST(Tracer, StartSpan) ASSERT_LT(std::chrono::nanoseconds(0), span_data->GetDuration()); } -TEST(Tracer, StartSpanWithTime) +TEST(Tracer, StartSpanWithOptions) { std::shared_ptr>> spans_received( new std::vector>);