-
Notifications
You must be signed in to change notification settings - Fork 440
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
[EXAMPLES] Improve ForceFlush call to also work with NoopTracerProvider #2506
Comments
Make sense, but void CleanupTracer() {
opentelemetry::nostd::shared_ptr<opentelemetry::trace::TracerProvider> provider =
trace::Provider::GetTracerProvider();
if (provider) {
#ifdef OPENTELEMETRY_RTTI_ENABLED
// When RTTI is enabled, use dynamic_cast for safety
trace_sdk::TracerProvider* sdkProvider = dynamic_cast<trace_sdk::TracerProvider*>(provider.get());
if (sdkProvider) {
sdkProvider->ForceFlush();
}
#else
// Without RTTI, use static_cast
// Note: This is safe only if the TracerProvider is correctly initialized as trace_sdk::TracerProvider
static_cast<trace_sdk::TracerProvider*>(provider.get())->ForceFlush();
#endif
}
std::shared_ptr<opentelemetry::trace::TracerProvider> none;
trace::Provider::SetTracerProvider(none);
}
Assigned to you @ecourreges-orange. Thanks for troubleshooting :) |
Calling A possible change is for the code to keep a global variable on the SDK provider used instead, and never call Instrumented code still can call |
This issue is available for anyone to work on. Make sure to reference this issue in your pull request. |
Agree. This would be the correct approach. |
I think there is more to this bug report. Calling ForceFlush on the NoopTracerProvider / NoopTracer should be illegal in the first place. The API spec only defines:
there is no Flush at the API level. opentelemetry-cpp exposed extra These methods belong to the SDK only. For ABI_VERSION_1, removing these will be a breaking change, so the best is to just leave them. For ABI_VERSION_2, I think ForceFlush() and Close() should be removed from the API, and only be available in the SDK. cc @open-telemetry/cpp-approvers |
Instead of having two code paths (nightmares) for static_cast and dynamic_cast - can't we use something akin to Qt's cast - https://doc.qt.io/qt-6/qobject.html#qobject_cast - it's probably more work to add this - but might be worth doing instead. dynamic_cast also tends to get much slower with larger apps (though it doesn't look like it's called in the critical path, but may one day). |
Or do not cast at all, see PR #2664 |
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:
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.
If you would like, I can provide the PR for the 4 examples that i found that have this code:
opentelemetry-cpp/examples/otlp/http_log_main.cc
Line 77 in a232328
opentelemetry-cpp/examples/otlp/grpc_log_main.cc
Line 61 in a232328
opentelemetry-cpp/examples/otlp/http_main.cc
Line 51 in a232328
opentelemetry-cpp/examples/otlp/grpc_main.cc
Line 45 in a232328
The text was updated successfully, but these errors were encountered: