@@ -32,9 +32,22 @@ TracerContext::TracerContext(std::vector<std::unique_ptr<SpanProcessor>> &&proce
3232 : resource_(resource),
3333 sampler_ (std::move(sampler)),
3434 id_generator_(std::move(id_generator)),
35- processor_(std::unique_ptr<SpanProcessor>(new MultiSpanProcessor(std::move(processors)))),
36- tracer_configurator_(std::move(tracer_configurator))
37- {}
35+ tracer_configurator_(std::move(tracer_configurator)),
36+ num_processors_(0 )
37+ {
38+ if (processors.empty ())
39+ {
40+ processor_ = std::unique_ptr<SpanProcessor>(new MultiSpanProcessor (std::move (processors)));
41+ }
42+ else
43+ {
44+ // at least one processor is available here
45+ for (auto &&processor : processors)
46+ {
47+ AddProcessor (std::move (processor));
48+ }
49+ }
50+ }
3851
3952Sampler &TracerContext::GetSampler () const noexcept
4053{
@@ -59,9 +72,32 @@ opentelemetry::sdk::trace::IdGenerator &TracerContext::GetIdGenerator() const no
5972
6073void TracerContext::AddProcessor (std::unique_ptr<SpanProcessor> processor) noexcept
6174{
75+ if (!processor)
76+ {
77+ return ;
78+ }
79+
80+ if (num_processors_ == 0 )
81+ {
82+ // this is the first processor to be added, maybe the MultiSpanProcessor is not needed
83+ processor_ = std::move (processor);
84+ }
85+ else if (num_processors_ == 1 )
86+ {
87+ // if there already is a processor, then make a new MultiSpanProcessor to handle both
88+ std::unique_ptr<MultiSpanProcessor> multi_processor (new MultiSpanProcessor ({}));
89+ multi_processor->AddProcessor (std::move (processor_));
90+ multi_processor->AddProcessor (std::move (processor));
91+ processor_ = std::move (multi_processor);
92+ }
93+ else /* if (num_processors_ > 1)*/
94+ {
95+ // already have a MultiSpanProcessor, add the processor to it
96+ auto multi_processor = static_cast <MultiSpanProcessor *>(processor_.get ());
97+ multi_processor->AddProcessor (std::move (processor));
98+ }
6299
63- auto multi_processor = static_cast <MultiSpanProcessor *>(processor_.get ());
64- multi_processor->AddProcessor (std::move (processor));
100+ ++num_processors_;
65101}
66102
67103SpanProcessor &TracerContext::GetProcessor () const noexcept
0 commit comments