opentelemetry-python icon indicating copy to clipboard operation
opentelemetry-python copied to clipboard

fix(sdk): force_flush returns meaningful bool on MetricReader

Open ravitheja4531-cell opened this issue 7 months ago • 25 comments

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:

ravitheja4531-cell avatar Apr 13 '26 21:04 ravitheja4531-cell

could someone from @jd take a look when you have a moment? Happy to make any changes needed. Thanks!

ravitheja4531-cell avatar Apr 14 '26 18:04 ravitheja4531-cell

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?

IndiTheo avatar Apr 14 '26 21:04 IndiTheo

Noted changed "must" to "should" in the docstring and updated the CHANGELOG to use the PR number. Thanks! @herin049 @MikeGoldsmith

ravitheja4531-cell avatar Apr 21 '26 18:04 ravitheja4531-cell

can you guys please approve it @herin049 @Asquator @MikeGoldsmith

ravitheja4531-cell avatar Apr 23 '26 14:04 ravitheja4531-cell

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

ravitheja4531-cell avatar Apr 24 '26 09:04 ravitheja4531-cell

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.

IndiTheo avatar Apr 24 '26 12:04 IndiTheo

Hi @MikeGoldsmith @Asquator please approve so that the checks run

ravitheja4531-cell avatar Apr 24 '26 20:04 ravitheja4531-cell

I already approved many times. We need a maintainer to approve the workflows.

IndiTheo avatar Apr 25 '26 14:04 IndiTheo

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.

MikeGoldsmith avatar Apr 25 '26 17:04 MikeGoldsmith

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 avatar Apr 27 '26 21:04 ravitheja4531-cell

@ravitheja4531-cell

Doesn't seem like it: https://github.com/open-telemetry/opentelemetry-python/pull/5158

lzchen avatar Apr 28 '26 16:04 lzchen

It shows that the tests are still failing

IndiTheo avatar May 06 '26 19:05 IndiTheo

@ravitheja4531-cell please check CI. It's showing failures, likely because of the linter. Please run tox run precommit.

MikeGoldsmith avatar May 07 '26 14:05 MikeGoldsmith

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.

emdneto avatar May 12 '26 22:05 emdneto

Hi @emdneto: Can you with your review when you get a chance.

ravitheja4531-cell avatar May 20 '26 12:05 ravitheja4531-cell

@emdneto: Did you get a chance to review/any other additional steps needed.

ravitheja4531-cell avatar May 23 '26 16:05 ravitheja4531-cell

Hello, is there a chance this can be merged in the near future?

IndiTheo avatar May 24 '26 14:05 IndiTheo

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.

ravitheja4531-cell avatar May 25 '26 06:05 ravitheja4531-cell

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.

ravitheja4531-cell avatar May 27 '26 00:05 ravitheja4531-cell

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?

IndiTheo avatar May 28 '26 16:05 IndiTheo