Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for resource deletion after tracer provider shutdown #911

Merged
merged 6 commits into from
Jul 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion examples/batch/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,15 @@ void initTracer()
// We export `kNumSpans` after every `schedule_delay_millis` milliseconds.
options.max_export_batch_size = kNumSpans;

opentelemetry::sdk::resource::ResourceAttributes attributes = {{"service", "test_service"},
{"version", (uint32_t)1}};
auto resource = opentelemetry::sdk::resource::Resource::Create(attributes);

auto processor = std::unique_ptr<sdktrace::SpanProcessor>(
new sdktrace::BatchSpanProcessor(std::move(exporter), options));

auto provider = nostd::shared_ptr<opentelemetry::trace::TracerProvider>(
new sdktrace::TracerProvider(std::move(processor)));
new sdktrace::TracerProvider(std::move(processor), resource));
// Set the global trace provider.
opentelemetry::trace::Provider::SetTracerProvider(provider);
}
Expand Down
4 changes: 3 additions & 1 deletion sdk/include/opentelemetry/sdk/trace/tracer.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,10 @@ class Tracer final : public trace_api::Tracer, public std::enable_shared_from_th
Sampler &GetSampler() { return context_->GetSampler(); }

private:
std::shared_ptr<sdk::trace::TracerContext> context_;
// order of declaration is important here - instrumentation library should destroy after
// tracer-context.
std::shared_ptr<InstrumentationLibrary> instrumentation_library_;
lalitb marked this conversation as resolved.
Show resolved Hide resolved
std::shared_ptr<sdk::trace::TracerContext> context_;
};
} // namespace trace
} // namespace sdk
Expand Down
4 changes: 2 additions & 2 deletions sdk/include/opentelemetry/sdk/trace/tracer_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,11 @@ class TracerContext
bool Shutdown() noexcept;

private:
// This is an atomic pointer so we can adapt the processor pipeline dynamically.
std::unique_ptr<SpanProcessor> processor_;
// order of declaration is important here - resource object should be destroyed after processor.
opentelemetry::sdk::resource::Resource resource_;
std::unique_ptr<Sampler> sampler_;
std::unique_ptr<IdGenerator> id_generator_;
std::unique_ptr<SpanProcessor> processor_;
};

} // namespace trace
Expand Down
3 changes: 2 additions & 1 deletion sdk/include/opentelemetry/sdk/trace/tracer_provider.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,9 @@ class TracerProvider final : public opentelemetry::trace::TracerProvider
bool ForceFlush(std::chrono::microseconds timeout = (std::chrono::microseconds::max)()) noexcept;

private:
std::shared_ptr<sdk::trace::TracerContext> context_;
// order of declaration is important here - tracers should destroy only after context.
std::vector<std::shared_ptr<opentelemetry::sdk::trace::Tracer>> tracers_;
std::shared_ptr<sdk::trace::TracerContext> context_;
std::mutex lock_;
};
} // namespace trace
Expand Down
2 changes: 1 addition & 1 deletion sdk/src/trace/tracer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ namespace trace

Tracer::Tracer(std::shared_ptr<sdk::trace::TracerContext> context,
std::unique_ptr<InstrumentationLibrary> instrumentation_library) noexcept
: context_{context}, instrumentation_library_{std::move(instrumentation_library)}
: instrumentation_library_{std::move(instrumentation_library)}, context_{context}
{}

nostd::shared_ptr<trace_api::Span> Tracer::StartSpan(
Expand Down
6 changes: 3 additions & 3 deletions sdk/src/trace/tracer_context.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ TracerContext::TracerContext(std::vector<std::unique_ptr<SpanProcessor>> &&proce
opentelemetry::sdk::resource::Resource resource,
std::unique_ptr<Sampler> sampler,
std::unique_ptr<IdGenerator> id_generator) noexcept
: processor_(std::unique_ptr<SpanProcessor>(new MultiSpanProcessor(std::move(processors)))),
resource_(resource),
: resource_(resource),
sampler_(std::move(sampler)),
id_generator_(std::move(id_generator))
id_generator_(std::move(id_generator)),
processor_(std::unique_ptr<SpanProcessor>(new MultiSpanProcessor(std::move(processors))))
{}

Sampler &TracerContext::GetSampler() const noexcept
Expand Down