diff --git a/sdk/include/opentelemetry/sdk/trace/span_data.h b/sdk/include/opentelemetry/sdk/trace/span_data.h index 2750ff8740..ffe1db1878 100644 --- a/sdk/include/opentelemetry/sdk/trace/span_data.h +++ b/sdk/include/opentelemetry/sdk/trace/span_data.h @@ -84,6 +84,33 @@ struct AttributeConverter } }; +/** + * Class for storing events in SpanData. + */ +class SpanDataEvent +{ +public: + SpanDataEvent(std::string name, core::SystemTimestamp timestamp) + : name_(name), timestamp_(timestamp) + {} + + /** + * Get the name for this event + * @return the name for this event + */ + std::string GetName() const noexcept { return name_; } + + /** + * Get the timestamp for this event + * @return the timestamp for this event + */ + core::SystemTimestamp GetTimestamp() const noexcept { return timestamp_; } + +private: + std::string name_; + core::SystemTimestamp timestamp_; +}; + /** * SpanData is a representation of all data collected by a span. */ @@ -147,6 +174,12 @@ class SpanData final : public Recordable return attributes_; } + /** + * Get the events associated with this span + * @return the events associated with this span + */ + const std::vector &GetEvents() const noexcept { return events_; } + void SetIds(opentelemetry::trace::TraceId trace_id, opentelemetry::trace::SpanId span_id, opentelemetry::trace::SpanId parent_span_id) noexcept override @@ -163,8 +196,7 @@ class SpanData final : public Recordable void AddEvent(nostd::string_view name, core::SystemTimestamp timestamp) noexcept override { - (void)name; - (void)timestamp; + events_.push_back(SpanDataEvent(std::string(name), timestamp)); } void SetStatus(trace_api::CanonicalCode code, nostd::string_view description) noexcept override @@ -192,6 +224,7 @@ class SpanData final : public Recordable opentelemetry::trace::CanonicalCode status_code_{opentelemetry::trace::CanonicalCode::OK}; std::string status_desc_; std::unordered_map attributes_; + std::vector events_; AttributeConverter converter_; }; } // namespace trace diff --git a/sdk/test/trace/span_data_test.cc b/sdk/test/trace/span_data_test.cc index 1bc231d294..41d7b2c670 100644 --- a/sdk/test/trace/span_data_test.cc +++ b/sdk/test/trace/span_data_test.cc @@ -22,6 +22,7 @@ TEST(SpanData, DefaultValues) ASSERT_EQ(data.GetStartTime().time_since_epoch(), std::chrono::nanoseconds(0)); ASSERT_EQ(data.GetDuration(), std::chrono::nanoseconds(0)); ASSERT_EQ(data.GetAttributes().size(), 0); + ASSERT_EQ(data.GetEvents().size(), 0); } TEST(SpanData, Set) @@ -49,4 +50,6 @@ TEST(SpanData, Set) ASSERT_EQ(data.GetStartTime().time_since_epoch(), now.time_since_epoch()); ASSERT_EQ(data.GetDuration(), std::chrono::nanoseconds(1000000)); ASSERT_EQ(opentelemetry::nostd::get(data.GetAttributes().at("attr1")), 314159); + ASSERT_EQ(data.GetEvents().at(0).GetName(), "event1"); + ASSERT_EQ(data.GetEvents().at(0).GetTimestamp(), now); }