Skip to content

Commit 25a0ea5

Browse files
authored
AddEvent support for Span (#343)
1 parent 9475516 commit 25a0ea5

File tree

2 files changed

+43
-6
lines changed

2 files changed

+43
-6
lines changed

sdk/src/trace/span.cc

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -118,22 +118,34 @@ void Span::SetAttribute(nostd::string_view key,
118118

119119
void Span::AddEvent(nostd::string_view name) noexcept
120120
{
121-
(void)name;
121+
std::lock_guard<std::mutex> lock_guard{mu_};
122+
if (recordable_ == nullptr)
123+
{
124+
return;
125+
}
126+
recordable_->AddEvent(name);
122127
}
123128

124129
void Span::AddEvent(nostd::string_view name, core::SystemTimestamp timestamp) noexcept
125130
{
126-
(void)name;
127-
(void)timestamp;
131+
std::lock_guard<std::mutex> lock_guard{mu_};
132+
if (recordable_ == nullptr)
133+
{
134+
return;
135+
}
136+
recordable_->AddEvent(name, timestamp);
128137
}
129138

130139
void Span::AddEvent(nostd::string_view name,
131140
core::SystemTimestamp timestamp,
132141
const trace_api::KeyValueIterable &attributes) noexcept
133142
{
134-
(void)name;
135-
(void)timestamp;
136-
(void)attributes;
143+
std::lock_guard<std::mutex> lock_guard{mu_};
144+
if (recordable_ == nullptr)
145+
{
146+
return;
147+
}
148+
recordable_->AddEvent(name, timestamp, attributes);
137149
}
138150

139151
void Span::SetStatus(trace_api::CanonicalCode code, nostd::string_view description) noexcept

sdk/test/trace/tracer_test.cc

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,31 @@ TEST(Tracer, SpanSetAttribute)
306306
ASSERT_EQ(3.1, nostd::get<double>(cur_span_data->GetAttributes().at("abc")));
307307
}
308308

309+
TEST(Tracer, SpanSetEvents)
310+
{
311+
std::unique_ptr<InMemorySpanExporter> exporter(new InMemorySpanExporter());
312+
std::shared_ptr<InMemorySpanData> span_data = exporter->GetData();
313+
auto tracer = initTracer(std::move(exporter));
314+
315+
auto span = tracer->StartSpan("span 1");
316+
span->AddEvent("event 1");
317+
span->AddEvent("event 2", std::chrono::system_clock::now());
318+
span->AddEvent("event 3", std::chrono::system_clock::now(), {{"attr1", 1}});
319+
span->End();
320+
321+
auto spans = span_data->GetSpans();
322+
ASSERT_EQ(1, spans.size());
323+
324+
auto &span_data_events = spans.at(0)->GetEvents();
325+
ASSERT_EQ(3, span_data_events.size());
326+
ASSERT_EQ("event 1", span_data_events[0].GetName());
327+
ASSERT_EQ("event 2", span_data_events[1].GetName());
328+
ASSERT_EQ("event 3", span_data_events[2].GetName());
329+
ASSERT_EQ(0, span_data_events[0].GetAttributes().size());
330+
ASSERT_EQ(0, span_data_events[1].GetAttributes().size());
331+
ASSERT_EQ(1, span_data_events[2].GetAttributes().size());
332+
}
333+
309334
TEST(Tracer, TestAlwaysOnSampler)
310335
{
311336
std::unique_ptr<InMemorySpanExporter> exporter(new InMemorySpanExporter());

0 commit comments

Comments
 (0)