-
Notifications
You must be signed in to change notification settings - Fork 500
Description
Is your feature request related to a problem?
Yes, if you copy and paste code from the examples, which most users will do, you end up with the code from cleanupTracer:
void CleanupTracer()
{
// We call ForceFlush to prevent to cancel running exportings, It's optional.
opentelemetry::nostd::shared_ptr<opentelemetry::trace::TracerProvider> provider =
trace::Provider::GetTracerProvider();
if (provider)
{
static_cast<trace_sdk::TracerProvider *>(provider.get())->ForceFlush();
}
std::shared_ptr<opentelemetry::trace::TracerProvider> none;
trace::Provider::SetTracerProvider(none);
}
Which is code that does not work when called before any initialization.
This may happen at least in unit tests of client code (happened to me and made me search for the best solution for 1h).
Also, it is better to call ForceFlush with a time argument so that people know they can use it, and you won't have issues posted like "why does my app not shutdown?".
Describe the solution you'd like
I would like the example code to work in all cases, inited or not.
Describe alternatives you've considered
Changing from static_cast to dynamic cast and checking if the provider exists fixes this,
Also adding a 10s timeout to ForceFlush should cover typical cases gracefully.
void CleanupTracer()
{
// We call ForceFlush to prevent cancelling running exports, It's optional.
opentelemetry::nostd::shared_ptr<opentelemetry::trace::TracerProvider> provider =
trace::Provider::GetTracerProvider();
if (provider)
{
trace_sdk::TracerProvider* tracerProvider = dynamic_cast<trace_sdk::TracerProvider*>(provider.get());
if ( tracerProvider) {
// Remove arg if you want to wait forever
tracerProvider->ForceFlush(std::chrono::milliseconds(10000));
}
}
std::shared_ptr<opentelemetry::trace::TracerProvider> none;
trace::Provider::SetTracerProvider(none);
}
If you would like, I can provide the PR for the 4 examples that i found that have this code:
static_cast<trace_sdk::TracerProvider *>(provider.get())->ForceFlush(); |
static_cast<trace_sdk::TracerProvider *>(provider.get())->ForceFlush(); |
static_cast<trace_sdk::TracerProvider *>(provider.get())->ForceFlush(); |
static_cast<trace_sdk::TracerProvider *>(provider.get())->ForceFlush(); |