Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

span SetAttribute crash #1283

Merged
merged 8 commits into from
Mar 26, 2022
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
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)
{
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we need emit a log message in this case (recordable_ == nullptr)?

Copy link
Member Author

Choose a reason for hiding this comment

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

We don't emit in the other methods.

return;
}
Copy link
Member

Choose a reason for hiding this comment

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

nit, consider adding an empty line between L100 and L101

Copy link
Member Author

Choose a reason for hiding this comment

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

thanks, done


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