Skip to content

fix: alert state is not restored when Prometheus restarts#18364

Closed
denysvitali wants to merge 1 commit into
prometheus:mainfrom
denysvitali:feature/add-test-restore-label-template
Closed

fix: alert state is not restored when Prometheus restarts#18364
denysvitali wants to merge 1 commit into
prometheus:mainfrom
denysvitali:feature/add-test-restore-label-template

Conversation

@denysvitali

@denysvitali denysvitali commented Mar 25, 2026

Copy link
Copy Markdown

In #13980 an optimization in the ALERTS_FOR_STATE logic caused the alert restoration to fail when template variables are used.

This PR intentionally doesn't solve this (yet) because the fix might cause performance issues that the problematic PR tried to address.

I strongly believe, as shown from the test here, that the performance fix never really worked - and this bug got unnoticed for a long time.

This investigation is the result of multiple months of debugging this behavior at a previous company I worked at together with @verejoel - and it's one of the reasons why he gave this talk at PromCon 2025 (although he didn't mention this issue there).

The intent of this PR is to get acknowledgment on the problem, and understand together what is the best way forward to solve it. Ideally a revert of the problematic PR would be enough, but that would solve the performance issues that @gotjosh tried to solve in #13980.

This PR will be used to fix #16883

cc/ @bwplotka

@denysvitali
denysvitali requested a review from a team as a code owner March 25, 2026 11:32
@denysvitali
denysvitali requested a review from dgl March 25, 2026 11:32
roidelapluie added a commit to roidelapluie/prometheus that referenced this pull request Mar 26, 2026
QueryForStateSeries built Select matchers from the raw rule labels,
which can contain Go template expressions such as
`instance_{{ $labels.instance }}`. The stored ALERTS_FOR_STATE series
carry the per-instance evaluated values (e.g. `instance_0`), so the
unevaluated template string never matched, leaving seriesByLabels empty
and silently skipping restoration for every active alert.

Fix by omitting any label whose value contains `{{` from the matcher
list. Static labels (including `__name__` and `alertname`) are never
templated and continue to scope the query to the correct rule. The
in-memory lookup against evaluated alert labels that follows is
unaffected, so the single-query-per-rule optimisation introduced in
prometheus#13980 is fully preserved.

Fixes prometheus#16883
Ref prometheus#13980
Ref prometheus#18364

Signed-off-by: Julien Pivotto <[email protected]>
@roidelapluie

Copy link
Copy Markdown
Member

What do you think of #18375

@denysvitali

Copy link
Copy Markdown
Author

Seems like a very good solution @roidelapluie ! Thanks!

@denysvitali

Copy link
Copy Markdown
Author

Closing as #18375 is merged. Thank you so much @roidelapluie - you have no idea on how frustrating this bug was and how long it took us to find the root cause of our AlertManager flood on each Thanos restart!

@denysvitali
denysvitali deleted the feature/add-test-restore-label-template branch March 28, 2026 08:57
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.

Templated Alerting Rules cause refirings (state lost)

2 participants