Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 35 additions & 2 deletions sdk/include/opentelemetry/sdk/trace/span_data.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

std::string -> opentelemetry::nostd::string_view (for consistency with how SpanData sets string members).

{}

/**
* Get the name for this event
* @return the name for this event
*/
std::string GetName() const noexcept { return name_; }
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

std::string -> -> opentelemetry::nostd::string_view (for consistency with how SpanData returns string members, and it also should avoid copy).


/**
* 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_;
Comment on lines +110 to +111
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both name_ and timestamp_ can be const membes since they are only set in ctor.

};

/**
* SpanData is a representation of all data collected by a span.
*/
Expand Down Expand Up @@ -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<SpanDataEvent> &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
Expand All @@ -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
Expand Down Expand Up @@ -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<std::string, SpanDataAttributeValue> attributes_;
std::vector<SpanDataEvent> events_;
AttributeConverter converter_;
};
} // namespace trace
Expand Down
3 changes: 3 additions & 0 deletions sdk/test/trace/span_data_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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<int64_t>(data.GetAttributes().at("attr1")), 314159);
ASSERT_EQ(data.GetEvents().at(0).GetName(), "event1");
ASSERT_EQ(data.GetEvents().at(0).GetTimestamp(), now);
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think ASSERT_EQ can be replaced by EXPECT_EQ throughout this file.

}