diff --git a/CHANGELOG.md b/CHANGELOG.md index 4f66ec617a..bd41c75626 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/sdk/src/trace/span.cc b/sdk/src/trace/span.cc index dff84a22b7..13ea99126c 100644 --- a/sdk/src/trace/span.cc +++ b/sdk/src/trace/span.cc @@ -94,6 +94,10 @@ Span::~Span() void Span::SetAttribute(nostd::string_view key, const common::AttributeValue &value) noexcept { std::lock_guard lock_guard{mu_}; + if (recordable_ == nullptr) + { + return; + } recordable_->SetAttribute(key, value); } diff --git a/sdk/test/trace/tracer_test.cc b/sdk/test/trace/tracer_test.cc index 9d1029cd02..dc806b079f 100644 --- a/sdk/test/trace/tracer_test.cc +++ b/sdk/test/trace/tracer_test.cc @@ -383,6 +383,34 @@ TEST(Tracer, SpanSetAttribute) ASSERT_EQ(3.1, nostd::get(cur_span_data->GetAttributes().at("abc"))); } +TEST(Tracer, TestAfterEnd) +{ + std::unique_ptr exporter(new InMemorySpanExporter()); + std::shared_ptr 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(cur_span_data->GetAttributes().at("abc"))); +} + TEST(Tracer, SpanSetEvents) { std::unique_ptr exporter(new InMemorySpanExporter());