Skip to content

[API] reduce allocation in no span case#4254

Open
proost wants to merge 5 commits into
open-telemetry:mainfrom
proost:perf-get-span-context
Open

[API] reduce allocation in no span case#4254
proost wants to merge 5 commits into
open-telemetry:mainfrom
proost:perf-get-span-context

Conversation

@proost

@proost proost commented Jul 16, 2026

Copy link
Copy Markdown
Contributor

Changes

Comment: #4191 (comment)

Reducing allocation in the no span case. Because a new default span return nostd::shared_ptr<Span>(new DefaultSpan(SpanContext::GetInvalid())); is created when we get context form GetSpan.

  • CHANGELOG.md updated for non-trivial changes
  • Unit tests have been added
  • Changes in public API reviewed

@proost
proost requested a review from a team as a code owner July 16, 2026 14:12
@proost proost mentioned this pull request Jul 16, 2026
3 tasks
@proost proost changed the title perf: reduce allocation perf: reduce allocation in no span case Jul 16, 2026
@codecov

codecov Bot commented Jul 16, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 75.00000% with 5 lines in your changes missing coverage. Please review.
✅ Project coverage is 78.15%. Comparing base (0e2406e) to head (e4cfad2).

Files with missing lines Patch % Lines
api/include/opentelemetry/trace/context.h 63.64% 4 Missing ⚠️
...pentelemetry/sdk/metrics/exemplar/reservoir_cell.h 66.67% 1 Missing ⚠️
Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #4254      +/-   ##
==========================================
- Coverage   78.15%   78.15%   -0.00%     
==========================================
  Files         440      440              
  Lines       18741    18736       -5     
==========================================
- Hits        14645    14641       -4     
+ Misses       4096     4095       -1     
Files with missing lines Coverage Δ
...de/opentelemetry/trace/propagation/b3_propagator.h 93.66% <100.00%> (ø)
...entelemetry/trace/propagation/http_trace_context.h 92.86% <100.00%> (ø)
...i/include/opentelemetry/trace/propagation/jaeger.h 97.83% <100.00%> (ø)
sdk/src/logs/logger.cc 94.12% <100.00%> (+3.61%) ⬆️
sdk/src/trace/tracer.cc 87.50% <100.00%> (ø)
...pentelemetry/sdk/metrics/exemplar/reservoir_cell.h 56.53% <66.67%> (-1.81%) ⬇️
api/include/opentelemetry/trace/context.h 82.61% <63.64%> (-17.39%) ⬇️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@dbarker dbarker left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Looks good to me. Thanks for the PR!

It needs additional review and approval as this is an expansion of the API.

const context::Context &context) noexcept override
{
SpanContext span_context = trace::GetSpan(context)->GetContext();
SpanContext span_context = trace::GetSpanContext(context);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Not an ABI break. No observable behavior change.

@dbarker dbarker added the pr:please-review This PR is ready for review label Jul 16, 2026

@marcalff marcalff left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM, thanks for the fix.

@marcalff

Copy link
Copy Markdown
Member

Do not merge:

Release 1.28.0 needs to be published first, and this PR can be merged after.

This is to limit last minute changes to API just when releasing.

@marcalff marcalff added pr:do-not-merge This PR is not ready to be merged. and removed pr:please-review This PR is ready for review pr:do-not-merge This PR is not ready to be merged. labels Jul 16, 2026
@marcalff marcalff changed the title perf: reduce allocation in no span case [API] reduce allocation in no span case Jul 16, 2026

// Get the span metadata from the active span in the context.
if (const nostd::shared_ptr<Span> *maybe_span =
nostd::get_if<nostd::shared_ptr<Span>>(&context_value))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Could the active variant alternative contain an empty nostd::shared_ptr<Span> here? nostd::get_if would still return a non-null pointer to the stored shared_ptr, so this condition would succeed even though *maybe_span is null, and the next line would dereference it.

The same issue applies to maybe_span_context below. Should both branches also check that the stored shared_ptr is non-null and return SpanContext::GetInvalid() otherwise?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants