Skip to content

fix: recover from collector panic and return error in Gather#1961

Open
Saflaski wants to merge 1 commit intoprometheus:mainfrom
Saflaski:fix/collector-gather-panic-to-error
Open

fix: recover from collector panic and return error in Gather#1961
Saflaski wants to merge 1 commit intoprometheus:mainfrom
Saflaski:fix/collector-gather-panic-to-error

Conversation

@Saflaski
Copy link

@Saflaski Saflaski commented Mar 7, 2026

Added a safeCollect helper function that wraps Collector.Collect and includes a deferred recover. On panic, it captures a stack trace, includes it in an InvalidMetric so Gather surfaces the error without crashing. The error and the stack trace are also appended to Gather's errs. 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

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>
@Saflaski Saflaski force-pushed the fix/collector-gather-panic-to-error branch from d60e13e to 743f933 Compare March 7, 2026 15:35
Copy link
Member

@kakkoyun kakkoyun left a comment

Choose a reason for hiding this comment

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

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.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants