Skip to content

fix(sdk): force_flush returns meaningful bool on MetricReader - #5085

Open
ravitheja4531-cell wants to merge 10 commits into
open-telemetry:mainfrom
ravitheja4531-cell:fix/force-flush-return-meaningful-bool
Open

fix(sdk): force_flush returns meaningful bool on MetricReader#5085
ravitheja4531-cell wants to merge 10 commits into
open-telemetry:mainfrom
ravitheja4531-cell:fix/force-flush-return-meaningful-bool

Conversation

@ravitheja4531-cell

Copy link
Copy Markdown

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
collectforce_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

  • Bug fix (non-breaking change which fixes an issue)

How Has This Been Tested?

Ran the full existing metrics test suite locally:

@ravitheja4531-cell

ravitheja4531-cell commented Apr 14, 2026

Copy link
Copy Markdown
Author

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

@IndiTheo

Copy link
Copy Markdown

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?

Comment thread opentelemetry-sdk/src/opentelemetry/sdk/metrics/_internal/export/__init__.py Outdated
Comment thread opentelemetry-sdk/src/opentelemetry/sdk/metrics/_internal/export/__init__.py Outdated
@github-project-automation github-project-automation Bot moved this to Reviewed PRs that need fixes in Python PR digest Apr 16, 2026
Comment on lines +611 to +613
collect_ok = super().force_flush(timeout_millis=timeout_millis)
exporter_ok = self._exporter.force_flush(timeout_millis=timeout_millis)
return collect_ok and exporter_ok

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Should we test this logic?
  2. Should we even call the exporter's force_flush if the general one fails?

@ravitheja4531-cell ravitheja4531-cell left a comment

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed all review feedback:

Changed Optional[bool] → bool | None on collect and _receive_metrics (per @herin049)
Removed Optional from imports entirely
Fixed force_flush short-circuit: exporter.force_flush is now skipped if super().force_flush fails (per @Asquator)
Added 4 unit tests covering success, failure, short-circuit, and detach always running in finally

Ready for re-review. Thanks!

@ravitheja4531-cell
ravitheja4531-cell force-pushed the fix/force-flush-return-meaningful-bool branch from 60da606 to 5680779 Compare April 20, 2026 12:11

@IndiTheo IndiTheo left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

Some static checks fail.

@MikeGoldsmith MikeGoldsmith left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good to me. Thanks @ravitheja4531-cell.

I've left a couple of small tweaks.

Comment thread opentelemetry-sdk/src/opentelemetry/sdk/metrics/_internal/export/__init__.py Outdated
Comment thread CHANGELOG.md Outdated
@ravitheja4531-cell

Copy link
Copy Markdown
Author

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

@ravitheja4531-cell

Copy link
Copy Markdown
Author

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

Comment thread CHANGELOG.md Outdated
@ravitheja4531-cell

Copy link
Copy Markdown
Author

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

@IndiTheo

Copy link
Copy Markdown

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.

@MikeGoldsmith MikeGoldsmith left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This now looks good to me, pending @herin049's feedback.

@ravitheja4531-cell

ravitheja4531-cell commented Apr 24, 2026

Copy link
Copy Markdown
Author

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

@IndiTheo

Copy link
Copy Markdown

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

@ravitheja4531-cell

Copy link
Copy Markdown
Author

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

Copy link
Copy Markdown
Author

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.

@IndiTheo

Copy link
Copy Markdown

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?

@ravitheja4531-cell

Copy link
Copy Markdown
Author

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?

Can someone help to move faster and merge this PR please @IndiTheo @herin049 @MikeGoldsmith

@ravitheja4531-cell

Copy link
Copy Markdown
Author

he 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?

Can someone help to move faster and merge this PR please @IndiTheo @herin049 @MikeGoldsmith

Hello Team, @MikeGoldsmith @herin049 how can we advance from here ? Need assistance to merge this asap.

@ravitheja4531-cell

Copy link
Copy Markdown
Author

he 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?

Can someone help to move faster and merge this PR please @IndiTheo @herin049 @MikeGoldsmith

Hello Team, @MikeGoldsmith @herin049 how can we advance from here ? Need assistance to merge this asap.

Hello @MikeGoldsmith @herin049: Any insights on this PR ? Requesting assistance to merge this sooner.

@ravitheja4531-cell

Copy link
Copy Markdown
Author

@emdneto @IndiTheo: Any insights on this PR ? Requesting assistance to merge this sooner.

@ravitheja4531-cell

Copy link
Copy Markdown
Author

@MikeGoldsmith @IndiTheo @emdneto @herin049: Please help me respond and lets move forward asap please.

@emdneto emdneto left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing changelog. can you add?

@IndiTheo

Copy link
Copy Markdown

Missing changelog. can you add?

@ravitheja4531-cell

@ravitheja4531-cell

Copy link
Copy Markdown
Author

Missing changelog. can you add?

@ravitheja4531-cell

Will be doing it shortly!

@ravitheja4531-cell

ravitheja4531-cell commented Jul 12, 2026

Copy link
Copy Markdown
Author

@emdneto @IndiTheo: changelog.md has been added, kindly help re-run the checks

@ravikambhampati-art

Copy link
Copy Markdown

@emdneto @IndiTheo: changelog.md has been added, kindly help re-run the checks

Did u get chance to help me run my checks ?

@ravikambhampati-art

Copy link
Copy Markdown

@

@emdneto @IndiTheo: changelog.md has been added, kindly help re-run the checks

Did u get chance to help me run my checks ?

@IndiTheo @MikeGoldsmith @lzchen @emdneto : changelog.md has been added, kindly help re-run the checks

@IndiTheo

Copy link
Copy Markdown

Hello, can we merge this?

@emdneto

@github-actions

Copy link
Copy Markdown

This PR has been automatically marked as stale because it has not had any activity for 14 days. It will be closed if no further activity occurs within 14 days of this comment.
If you're still working on this, please add a comment or push new commits.

@github-actions github-actions Bot added the Stale label Aug 14, 2026
@opentelemetry-pr-dashboard

opentelemetry-pr-dashboard Bot commented Aug 14, 2026

Copy link
Copy Markdown

Pull request dashboard status

Waiting on the author · refreshed 2026-08-25 21:33 UTC

Two things need attention:

  • Required checks are failing — investigate the failures.
  • 7 review items — respond to each (e.g. link a commit, explain why not, ask a follow-up):
    • Top-level threads: 1, 2, 3, 4, 5, 6, 7
Status above doesn't look right?
  • Just replied or pushed? Anything around or after the refresh time above may not be picked up yet — give it a few minutes.
  • Should this be with reviewers? Comment /dashboard route:reviewers to route it to them.
  • Anything wrong — including the routing? Report it with what you expected; it helps us improve the dashboard.

@tammy-baylis-swi

Copy link
Copy Markdown
Contributor

Hello, can we merge this?

@emdneto

Hi thanks for this. Please could you resolve the current merge conflicts

@github-actions github-actions Bot removed the Stale label Aug 18, 2026
@opentelemetry-pr-dashboard

Copy link
Copy Markdown

Hi @ravitheja4531-cell — just a friendly reminder that this pull request is waiting on you. The dashboard status comment has the open items and is kept current.

  • Replying is enough to hand it off — answer, explain why no change is needed, or ask a follow-up. The dashboard routes it onward once nothing on the list is waiting on you.
  • To hand it back for any other reason, including the dashboard getting this wrong, comment /dashboard route:reviewers.

@ravitheja4531-cell

Copy link
Copy Markdown
Author

Merge conflict in export/init.py is now resolved and the branch is up to date with main. This is ready for another look. Could a maintainer approve the workflow run so CI can execute? Also, there's still one "changes requested" review on record — @herin049 , if your earlier feedback is addressed, would you mind re-reviewing to clear it? Thanks all.

@ravitheja4531-cell

Copy link
Copy Markdown
Author

@IndiTheo @MikeGoldsmith @emdneto @lzchen can you please run the checks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Reviewed PRs that need fixes

Development

Successfully merging this pull request may close these issues.

force_flush should provide a way to let the caller know whether it succeeded, failed or timed out.

8 participants