Skip to content

Commit

Permalink
Add Shutdown method to TracerProvider (open-telemetry#394)
Browse files Browse the repository at this point in the history
  • Loading branch information
ThomsonTan authored Nov 24, 2020
1 parent 34db85e commit ecf88ef
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 4 deletions.
4 changes: 3 additions & 1 deletion sdk/include/opentelemetry/sdk/trace/simple_processor.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,14 @@ class SimpleSpanProcessor : public SpanProcessor
void Shutdown(std::chrono::microseconds timeout = std::chrono::microseconds(0)) noexcept override
{
// We only call shutdown ONCE.
if (!shutdown_latch_.test_and_set(std::memory_order_acquire))
if (exporter_ != nullptr && !shutdown_latch_.test_and_set(std::memory_order_acquire))
{
exporter_->Shutdown(timeout);
}
}

~SimpleSpanProcessor() { Shutdown(); }

private:
std::unique_ptr<SpanExporter> exporter_;
opentelemetry::common::SpinLockMutex lock_;
Expand Down
5 changes: 5 additions & 0 deletions sdk/include/opentelemetry/sdk/trace/tracer_provider.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ class TracerProvider final : public opentelemetry::trace::TracerProvider
*/
std::shared_ptr<Sampler> GetSampler() const noexcept;

/**
* Shutdown the span processor associated with this tracer provider.
*/
void Shutdown() noexcept;

private:
opentelemetry::sdk::AtomicSharedPtr<SpanProcessor> processor_;
std::shared_ptr<opentelemetry::trace::Tracer> tracer_;
Expand Down
8 changes: 5 additions & 3 deletions sdk/src/trace/batch_span_processor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -175,12 +175,14 @@ void BatchSpanProcessor::DrainQueue()

void BatchSpanProcessor::Shutdown(std::chrono::microseconds timeout) noexcept
{
is_shutdown_ = true;
is_shutdown_.store(true);

cv_.notify_one();
worker_thread_.join();

exporter_->Shutdown();
if (exporter_ != nullptr)
{
exporter_->Shutdown();
}
}

BatchSpanProcessor::~BatchSpanProcessor()
Expand Down
5 changes: 5 additions & 0 deletions sdk/src/trace/tracer_provider.cc
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ std::shared_ptr<Sampler> TracerProvider::GetSampler() const noexcept
{
return sampler_;
}

void TracerProvider::Shutdown() noexcept
{
GetProcessor()->Shutdown();
}
} // namespace trace
} // namespace sdk
OPENTELEMETRY_END_NAMESPACE
12 changes: 12 additions & 0 deletions sdk/test/trace/tracer_provider_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,15 @@ TEST(TracerProvider, GetSampler)

ASSERT_EQ("AlwaysOffSampler", t3->GetDescription());
}

TEST(TracerProvider, Shutdown)
{
std::shared_ptr<SpanProcessor> processor1(new SimpleSpanProcessor(nullptr));

TracerProvider tp1(processor1);

tp1.Shutdown();

// Verify Shutdown returns.
ASSERT_TRUE(true);
}

0 comments on commit ecf88ef

Please sign in to comment.