Skip to content

Commit 75d829f

Browse files
committed
make span remember the SpanProcessor it was created with
1 parent 6377e54 commit 75d829f

2 files changed

Lines changed: 10 additions & 3 deletions

File tree

sdk/src/trace/span.cc

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,11 @@ Span::Span(std::shared_ptr<Tracer> &&tracer,
8989
recordable_->SetStartTime(NowOr(options.start_system_time));
9090
start_steady_time = NowOr(options.start_steady_time);
9191
recordable_->SetResource(tracer_->GetResource());
92-
tracer_->GetProcessor().OnStart(*recordable_, parent_span_context);
92+
93+
// store the span processor, this is important to use the same processor even
94+
// in case the tracer changes from single processor to a MultiSpanProcessor
95+
span_processor_ = &tracer_->GetProcessor();
96+
span_processor_->OnStart(*recordable_, parent_span_context);
9397
}
9498

9599
Span::~Span()
@@ -203,6 +207,8 @@ void Span::End(const opentelemetry::trace::EndSpanOptions &options) noexcept
203207
{
204208
std::lock_guard<std::mutex> lock_guard{mu_};
205209

210+
// has_ended_ could be removed and instead rely on span_processor_,
211+
// resetting it after calling OnEnd. it would save 8 bytes in each Span object.
206212
if (has_ended_ == true)
207213
{
208214
return;
@@ -218,7 +224,7 @@ void Span::End(const opentelemetry::trace::EndSpanOptions &options) noexcept
218224
recordable_->SetDuration(std::chrono::steady_clock::time_point(end_steady_time) -
219225
std::chrono::steady_clock::time_point(start_steady_time));
220226

221-
tracer_->GetProcessor().OnEnd(std::move(recordable_));
227+
span_processor_->OnEnd(std::move(recordable_));
222228
recordable_.reset();
223229
}
224230

sdk/src/trace/span.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,12 @@ class Span final : public opentelemetry::trace::Span
7979
}
8080

8181
private:
82-
std::shared_ptr<Tracer> tracer_;
82+
std::shared_ptr<Tracer> tracer_; // also keeps span_processor_ alive
8383
mutable std::mutex mu_;
8484
std::unique_ptr<Recordable> recordable_;
8585
opentelemetry::common::SteadyTimestamp start_steady_time;
8686
std::unique_ptr<opentelemetry::trace::SpanContext> span_context_;
87+
SpanProcessor * span_processor_{nullptr}; // kept alive by tracer_
8788
bool has_ended_{false};
8889
};
8990
} // namespace trace

0 commit comments

Comments
 (0)