Skip to content

Commit

Permalink
Fix span SetAttribute crash (#1283)
Browse files Browse the repository at this point in the history
  • Loading branch information
esigo committed Mar 26, 2022
1 parent 91b0572 commit c1b9590
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Increment the:

## [Unreleased]

* [SDK] Bugfix: span SetAttribute crash ([#1283](https://github.com/open-telemetry/opentelemetry-cpp/pull/1283))
* [EXPORTER] Jaeger Exporter - Populate Span Links ([#1251](https://github.com/open-telemetry/opentelemetry-cpp/pull/1251))

## [1.2.0] 2022-01-31
Expand Down
4 changes: 4 additions & 0 deletions sdk/src/trace/span.cc
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@ Span::~Span()
void Span::SetAttribute(nostd::string_view key, const common::AttributeValue &value) noexcept
{
std::lock_guard<std::mutex> lock_guard{mu_};
if (recordable_ == nullptr)
{
return;
}

recordable_->SetAttribute(key, value);
}
Expand Down
28 changes: 28 additions & 0 deletions sdk/test/trace/tracer_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,34 @@ TEST(Tracer, SpanSetAttribute)
ASSERT_EQ(3.1, nostd::get<double>(cur_span_data->GetAttributes().at("abc")));
}

TEST(Tracer, TestAfterEnd)
{
std::unique_ptr<InMemorySpanExporter> exporter(new InMemorySpanExporter());
std::shared_ptr<InMemorySpanData> span_data = exporter->GetData();
auto tracer = initTracer(std::move(exporter));
auto span = tracer->StartSpan("span 1");
span->SetAttribute("abc", 3.1);

span->End();

// test after end
span->SetAttribute("testing null recordable", 3.1);
span->AddEvent("event 1");
span->AddEvent("event 2", std::chrono::system_clock::now());
span->AddEvent("event 3", std::chrono::system_clock::now(), {{"attr1", 1}});
std::string new_name{"new name"};
span->UpdateName(new_name);
span->SetAttribute("attr1", 3.1);
std::string description{"description"};
span->SetStatus(opentelemetry::trace::StatusCode::kError, description);
span->End();

auto spans = span_data->GetSpans();
ASSERT_EQ(1, spans.size());
auto &cur_span_data = spans.at(0);
ASSERT_EQ(3.1, nostd::get<double>(cur_span_data->GetAttributes().at("abc")));
}

TEST(Tracer, SpanSetEvents)
{
std::unique_ptr<InMemorySpanExporter> exporter(new InMemorySpanExporter());
Expand Down

0 comments on commit c1b9590

Please sign in to comment.