Skip to content

bugfix ensure legacy matchers field is populated in snapshots - #5330

Merged
Spaceman1701 merged 3 commits into
prometheus:mainfrom
Spaceman1701:fix/snapshot-silences
Jun 26, 2026
Merged

Spaceman1701 merged 3 commits into
prometheus:mainfrom
Spaceman1701:fix/snapshot-silences

Conversation

@Spaceman1701

Copy link
Copy Markdown
Contributor

Changes silences.state to use marshalMeshSilence when writing a snapshot to match the behavior of sending a gossip message. marhsalMeshSilence ensures that the legacy matchers field is populated (which allows older Alertmanager versions to read the message). This fixes a bug when downgrading to older versions of Alertmanager that don't support the new fields. While downgrades are not generally supported, this specific case was meant to be backwards and forwards compatible.

Pull Request Checklist

Please check all the applicable boxes.

Which user-facing changes does this PR introduce?

[BUGFIX] silences: fix silences snapshot missing legacy matchers field. This caused a bug that prevented older alertmanager versions from reading newer snapshots unnecessarily. 

@coderabbitai

coderabbitai Bot commented Jun 25, 2026 •

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Enterprise

Run ID: 99128217-7578-4776-bf64-fabfd1cb02c3

📥 Commits

Reviewing files that changed from the base of the PR and between d735094 and 2f7d62b.

📒 Files selected for processing (1)
  • silence/silence.go

📝 Walkthrough

Walkthrough

Snapshot marshalling now routes each silence through the compatibility-aware marshal path before writing. A regression test verifies the in-memory silence is unchanged and the encoded snapshot still contains the legacy matcher field.

Changes

Silence snapshot compatibility

Layer / File(s) Summary
Snapshot marshal path
silence/silence.go
state.MarshalBinary now marshals each silence through marshalMeshSilence before writing to the snapshot buffer, and prepareSilenceForMarshalling now guards against nil silences before copying matcher sets into the legacy field.
Legacy matcher regression test
silence/state_test.go
A new test decodes the marshaled bytes and checks that Matchers is written on the wire while the original in-memory silence remains unchanged.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 50.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly describes the snapshot legacy-matchers bug fix, even if it is slightly less specific than the code changes.
Description check ✅ Passed The PR description covers the bug, linked issue, checklist items, and release notes, so it is mostly complete.
Linked Issues check ✅ Passed The code and test changes directly address #5326 by routing snapshot marshaling through the legacy matcher compatibility path.
Out of Scope Changes check ✅ Passed The diff stays focused on snapshot marshaling and its regression test, with no obvious unrelated changes.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot 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.

Actionable comments posted: 1

🧹 Nitpick comments (1)
silence/state_test.go (1)

54-60: 🎯 Functional Correctness | 🔵 Trivial | ⚡ Quick win

Cover the “first matcher set wins” wire contract.

With only one MatcherSet, this still passes if MarshalBinary accidentally copies the wrong set into legacy Matchers. Adding a second distinct set would pin the backward-compatibility rule that the wire Matchers mirror MatcherSets[0].

Also applies to: 79-85

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@silence/state_test.go` around lines 54 - 60, The current test only uses one
MatcherSet, so it can’t detect if MarshalBinary maps the wrong set into the
legacy Matchers field. Update the silence/state_test.go coverage around
Silence.MarshalBinary to build at least two distinct MatcherSets and assert that
the wire legacy Matchers always mirror MatcherSets[0], using the existing
pb.Silence, pb.MatcherSet, and MarshalBinary path to lock in the “first matcher
set wins” contract.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@silence/silence.go`:
- Around line 1369-1371: The error returned from marshalMeshSilence is missing
snapshot-generation context, so update the error handling in the marshal path to
wrap the failure with fmt.Errorf("generating snapshot: %w", err). Keep the
change localized to the code that calls marshalMeshSilence so the existing
diagnostic context is preserved consistently with the repository’s Go
error-handling pattern.

---

Nitpick comments:
In `@silence/state_test.go`:
- Around line 54-60: The current test only uses one MatcherSet, so it can’t
detect if MarshalBinary maps the wrong set into the legacy Matchers field.
Update the silence/state_test.go coverage around Silence.MarshalBinary to build
at least two distinct MatcherSets and assert that the wire legacy Matchers
always mirror MatcherSets[0], using the existing pb.Silence, pb.MatcherSet, and
MarshalBinary path to lock in the “first matcher set wins” contract.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Enterprise

Run ID: 72cf88b6-f21e-4cd8-8964-fc909a4f6ae2

📥 Commits

Reviewing files that changed from the base of the PR and between 7e54eeb and d735094.

📒 Files selected for processing (2)
  • silence/silence.go
  • silence/state_test.go

Comment thread silence/silence.go
Comment on lines +1369 to 1371
b, err := marshalMeshSilence(e)
if err != nil {
return nil, err

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

📐 Maintainability & Code Quality | 🟠 Major

🧩 Analysis chain

🏁 Script executed:

cat -n silence/silence.go | sed -n '1360,1380p'

Repository: prometheus/alertmanager

Length of output: 658


🏁 Script executed:

grep -n "return nil, err\|return err\|fmt.Errorf.*err" silence/silence.go | head -30

Repository: prometheus/alertmanager

Length of output: 1291


🏁 Script executed:

head -50 silence/silence.go | grep -E "import|\"fmt\""

Repository: prometheus/alertmanager

Length of output: 177


🏁 Script executed:

grep -B20 "func (s state) MarshalBinary" silence/silence.go | head -25

Repository: prometheus/alertmanager

Length of output: 723


🏁 Script executed:

sed -n '1,30p' silence/silence.go | grep "import\|\""

Repository: prometheus/alertmanager

Length of output: 417


Wrap the marshal failure with snapshot context.

Returning the raw error from marshalMeshSilence here obscures the operation being performed. Please wrap the error with fmt.Errorf("generating snapshot: %w", err) to maintain diagnostic context, consistent with the repository's error-handling guidelines for Go code.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@silence/silence.go` around lines 1369 - 1371, The error returned from
marshalMeshSilence is missing snapshot-generation context, so update the error
handling in the marshal path to wrap the failure with fmt.Errorf("generating
snapshot: %w", err). Keep the change localized to the code that calls
marshalMeshSilence so the existing diagnostic context is preserved consistently
with the repository’s Go error-handling pattern.

Source: Coding guidelines

Signed-off-by: Ethan Hunter <[email protected]>
@Spaceman1701
Spaceman1701 merged commit 058616d into prometheus:main Jun 26, 2026
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Silence snapshot backward-compatibility bug (downgrade 0.32 → 0.31)

2 participants