fix: recover from collector panic and return error in Gather#1961
Open
Saflaski wants to merge 1 commit intoprometheus:mainfrom
Open
fix: recover from collector panic and return error in Gather#1961Saflaski wants to merge 1 commit intoprometheus:mainfrom
Saflaski wants to merge 1 commit intoprometheus:mainfrom
Conversation
Add safeCollect helper that wraps Collector.Collect in a deferred recover. On panic, captures a stack trace and sends an InvalidMetric so Gather surfaces the error without crashing. Metrics sent before the panic are preserved in the gathered result. Add test to make sure that the correct error is returned on panic and no error otherwise. Signed-off-by: Saflaski <sabeehislam.si@gmail.com>
d60e13e to
743f933
Compare
kakkoyun
requested changes
Mar 9, 2026
Member
kakkoyun
left a comment
There was a problem hiding this comment.
I think we should have a MustGather method following the existing convention where we panic when something goes wrong.
Otherwise this looks good to me! Thanks for the contribution.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Added a
safeCollecthelper function that wrapsCollector.Collectand includes a deferred recover. On panic, it captures a stack trace, includes it in anInvalidMetricsoGathersurfaces the error without crashing. The error and the stack trace are also appended to Gather'serrs. Metrics sent before the panic are preserved in the gathered result. The code snippet in the deferred recover is based off of #1936.A test has also been included that checks that Gather includes the correct error if a panic happens, and none otherwise.
Addresses #1900