fix(sdk): force_flush returns meaningful bool on MetricReader
Fixes #5020
Description
force_flush on MetricReader and PeriodicExportingMetricReader always
returned True regardless of whether the export succeeded or failed, violating
the OTel specification:
ForceFlush SHOULD provide a way to let the caller know whether it succeeded, failed or timed out.
This PR threads the actual export result up through _receive_metrics →
collect → force_flush so callers get a meaningful bool.
Also fixes a pre-existing bug where detach(token) in
PeriodicExportingMetricReader._receive_metrics was only called on the happy
path — moved to finally so it always runs.
Type of change
- [x] Bug fix (non-breaking change which fixes an issue)
How Has This Been Tested?
Ran the full existing metrics test suite locally:
could someone from @jd take a look when you have a moment? Happy to make any changes needed. Thanks!
I personally like this pretty much. After it's merged, we'll have to open a new issue to indicate failure reason.
Will look at the code with more attention soon.
Btw, are you planning to add unit tests for the new behavior?
Noted changed "must" to "should" in the docstring and updated the CHANGELOG to use the PR number. Thanks! @herin049 @MikeGoldsmith
can you guys please approve it @herin049 @Asquator @MikeGoldsmith
In the original code, detach(token) was called at the end of the method body but not inside a finally block. If an uncaught exception escaped the except Exception handler (e.g. a BaseException like KeyboardInterrupt), detach would never be called, leaking the context token. Moving it to finally ensures it always runs. Happy to remove the mention from the CHANGELOG if you'd prefer to keep the entry focused on the force_flush behavior.
@herin049
The handlng of detach(token) can be extracted into a separate PR if the project philosophy demands it. On the other hand, it's such a small fix that leaving it here will probably do no harm.
All depends on maintainer preferences and how pedantic the library is about separating changes.
Anyway, if it's left here, the merge commit message should be more generic/include both changes.
Hi @MikeGoldsmith @Asquator please approve so that the checks run
I already approved many times. We need a maintainer to approve the workflows.
You don't need to re-approve after each "update branch". A maintainer can do the final update and merge when they get to it.
Hi @lzchen — the only failing tests are test_cpu_time_callback and test_cpu_time_generator which are pre-existing failures unrelated to this PR. Could you verify these fail on main as well and override/merge if confirmed? All other tests pass and lint is green.
@ravitheja4531-cell
Doesn't seem like it: https://github.com/open-telemetry/opentelemetry-python/pull/5158
It shows that the tests are still failing
@ravitheja4531-cell please check CI. It's showing failures, likely because of the linter. Please run tox run precommit.
Thanks for the PR!
Just a heads-up: we no longer update CHANGELOG.md directly. The changelog is now generated from changelog fragments using Towncrier.
Please add the appropriate changelog fragment for this change instead of editing CHANGELOG.md manually. You can find the instructions and expected format in CONTRIBUTING.md.
Hi @emdneto: Can you with your review when you get a chance.
@emdneto: Did you get a chance to review/any other additional steps needed.
Hello, is there a chance this can be merged in the near future?
Hello, is there a chance this can be merged in the near future?
Is I want to merge soon, please help me know if anything else needs to be done.
Hello, is there a chance this can be merged in the near future?
Is I want to merge soon, please help me know if anything else needs to be done.
@emdneto @IndiTheo : Can you help move forward.
I see the feature itself as done. Maybe there are some caveats with the .md files indeed, I don't know much about that. @MikeGoldsmith @herin049 how can we advance from here?