Skip to content

Commit d8ad645

Browse files
committed
pointer implementation instead of counter
1 parent c570255 commit d8ad645

2 files changed

Lines changed: 16 additions & 11 deletions

File tree

sdk/include/opentelemetry/sdk/trace/tracer_context.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ namespace sdk
2323
namespace trace
2424
{
2525

26+
// forward declare to be able to have a raw pointer to it
27+
class MultiSpanProcessor;
28+
2629
/**
2730
* A class which stores the TracerProvider context.
2831
*
@@ -120,7 +123,8 @@ class TracerContext
120123
std::unique_ptr<IdGenerator> id_generator_;
121124
std::unique_ptr<SpanProcessor> processor_;
122125
std::unique_ptr<instrumentationscope::ScopeConfigurator<TracerConfig>> tracer_configurator_;
123-
uint16_t num_processors_;
126+
// shares the pointer with processor_ if it is a MultiSpanProcessor, null otherwise
127+
MultiSpanProcessor *multi_processor_;
124128
};
125129

126130
} // namespace trace

sdk/src/trace/tracer_context.cc

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ TracerContext::TracerContext(std::vector<std::unique_ptr<SpanProcessor>> &&proce
3333
sampler_(std::move(sampler)),
3434
id_generator_(std::move(id_generator)),
3535
tracer_configurator_(std::move(tracer_configurator)),
36-
num_processors_(0)
36+
multi_processor_(nullptr)
3737
{
3838
if (processors.empty())
3939
{
@@ -77,27 +77,28 @@ void TracerContext::AddProcessor(std::unique_ptr<SpanProcessor> processor) noexc
7777
return;
7878
}
7979

80-
if (num_processors_ == 0)
80+
if (!processor_)
8181
{
82-
// this is the first processor to be added, maybe the MultiSpanProcessor is not needed
82+
// this is the first processor to be added
8383
processor_ = std::move(processor);
8484
}
85-
else if (num_processors_ == 1)
85+
else if (multi_processor_ == nullptr)
8686
{
87-
// if there already is a processor, then make a new MultiSpanProcessor to handle both
87+
// if there already is a processor, but its not a MultiSpanProcessor. make a new MultiSpanProcessor
8888
std::unique_ptr<MultiSpanProcessor> multi_processor(new MultiSpanProcessor({}));
8989
multi_processor->AddProcessor(std::move(processor_));
9090
multi_processor->AddProcessor(std::move(processor));
91+
92+
// duplicate the pointer before it gets type erased
93+
multi_processor_ = multi_processor.get();
94+
9195
processor_ = std::move(multi_processor);
9296
}
93-
else /*if (num_processors_ > 1)*/
97+
else /*if (multi_processor_ != nullptr)*/
9498
{
9599
// 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));
100+
multi_processor_->AddProcessor(std::move(processor));
98101
}
99-
100-
++num_processors_;
101102
}
102103

103104
SpanProcessor &TracerContext::GetProcessor() const noexcept

0 commit comments

Comments
 (0)