Skip to content

alertmanager: fix regression from update to 0.33 - #7988

Open
timonegk wants to merge 2 commits into
cortexproject:masterfrom
timonegk:fix-alertmanager2
Open

timonegk wants to merge 2 commits into
cortexproject:masterfrom
timonegk:fix-alertmanager2

Conversation

@timonegk

@timonegk timonegk commented Dec 3, 2026

Copy link
Copy Markdown

What this PR does:
There is a regression in Cortex 1.22 that shows alerts as "unprocessed" in Grafana. This is due to an API breaking change in alertmanager 0.33.

Checklist

  • Tests updated
  • Documentation added
  • CHANGELOG.md updated - the order of entries should be [CHANGE], [FEATURE], [ENHANCEMENT], [BUGFIX]
  • docs/configuration/v1-guarantees.md updated if this PR introduces experimental flags
  • I did not look at code from AGPL licensed projects to make this PR

Signed-off-by: Timon Engelke <timon.engelke@inovex.de>
@timonegk
timonegk requested a review from a team as a code owner December 3, 2026 12:28
@timonegk
timonegk requested a review from SungJin1212 December 3, 2026 12:28
@timonegk

timonegk commented Dec 3, 2026

Copy link
Copy Markdown
Author

@friedrichg @SungJin1212 @sandy2008 PTAL

@friedrichg friedrichg 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.

I was able to trigger data race with this test

package alertmanager

import (
	"bytes"
	"encoding/json"
	"net/http"
	"net/http/httptest"
	"net/url"
	"sync"
	"testing"
	"time"

	"github.com/go-kit/log"
	"github.com/prometheus/alertmanager/config"
	"github.com/prometheus/client_golang/prometheus"
	"github.com/stretchr/testify/require"
)

// TestRaceReproApplyConfigVsGetAlerts reloads the config while API requests
// read alerts. GET /api/v2/alerts calls the status callback for each alert,
// and the callback reads am.inhibitor, which ApplyConfig writes.
func TestRaceReproApplyConfigVsGetAlerts(t *testing.T) {
	const user = "test"

	am, err := New(&Config{
		UserID:          user,
		Logger:          log.NewNopLogger(),
		Limits:          &mockAlertManagerLimits{},
		TenantDataDir:   t.TempDir(),
		ExternalURL:     &url.URL{Path: "/am"},
		ShardingEnabled: false,
		GCInterval:      30 * time.Minute,
	}, prometheus.NewPedanticRegistry())
	require.NoError(t, err)
	defer am.StopAndWait()

	cfgRaw := `receivers:
- name: 'prod'

route:
  group_by: ['alertname']
  group_wait: 10ms
  group_interval: 10ms
  receiver: 'prod'`
	cfg, err := config.Load(cfgRaw)
	require.NoError(t, err)
	require.NoError(t, am.ApplyConfig(user, cfg, cfgRaw))

	now := time.Now().UTC()
	b, err := json.Marshal([]map[string]any{{
		"labels":   map[string]string{"alertname": "A"},
		"startsAt": now.Add(-time.Minute).Format(time.RFC3339),
		"endsAt":   now.Add(time.Hour).Format(time.RFC3339),
	}})
	require.NoError(t, err)
	req := httptest.NewRequest(http.MethodPost, "http://alertmanager/am/api/v2/alerts", bytes.NewReader(b))
	req.Header.Set("Content-Type", "application/json")
	rec := httptest.NewRecorder()
	am.mux.ServeHTTP(rec, req)
	require.Equal(t, http.StatusOK, rec.Code, rec.Body.String())

	stop := make(chan struct{})
	var wg sync.WaitGroup
	wg.Add(1)
	go func() {
		defer wg.Done()
		for {
			select {
			case <-stop:
				return
			default:
			}
			rec := httptest.NewRecorder()
			am.mux.ServeHTTP(rec, httptest.NewRequest(http.MethodGet, "http://alertmanager/am/api/v2/alerts", nil))
		}
	}()

	for range 20 {
		require.NoError(t, am.ApplyConfig(user, cfg, cfgRaw))
	}
	close(stop)
	wg.Wait()
}

go test -race -tags "netgo slicelabels" -count=1 -v -run '^TestRaceReproApplyConfigVsGetAlerts$

=== RUN   TestRaceReproApplyConfigVsGetAlerts
==================
WARNING: DATA RACE
Write at 0x00c000168228 by goroutine 42:
  github.com/cortexproject/cortex/pkg/alertmanager.(*Alertmanager).ApplyConfig()
      /Users/fgonzalez/source/github.com/cortexproject/cortex/pr-7988/pkg/alertmanager/alertmanager.go:353 +0x41c
  github.com/cortexproject/cortex/pkg/alertmanager.(*Alertmanager).ApplyConfig()
      /Users/fgonzalez/source/github.com/cortexproject/cortex/pr-7988/pkg/alertmanager/alertmanager.go:350 +0x2f8
  github.com/cortexproject/cortex/pkg/alertmanager.TestRaceReproApplyConfigVsGetAlerts()
      /Users/fgonzalez/source/github.com/cortexproject/cortex/pr-7988/pkg/alertmanager/race_repro_test.go:79 +0xd18
  net/http.HandlerFunc.ServeHTTP()
      /opt/homebrew/Cellar/go/1.27.0/libexec/src/net/http/server.go:2338 +0x48
  github.com/prometheus/client_golang/prometheus/promhttp.InstrumentHandlerDuration.func1()
      /Users/fgonzalez/source/github.com/cortexproject/cortex/pr-7988/vendor/github.com/prometheus/client_golang/prometheus/promhttp/instrument_server.go:109 +0xc4
  net/http.HandlerFunc.ServeHTTP()
      /opt/homebrew/Cellar/go/1.27.0/libexec/src/net/http/server.go:2338 +0x48
  github.com/prometheus/alertmanager/api.(*API).instrumentHandler.func1()
      /Users/fgonzalez/source/github.com/cortexproject/cortex/pr-7988/vendor/github.com/prometheus/alertmanager/api/api.go:240 +0x204
  net/http.HandlerFunc.ServeHTTP()
      /opt/homebrew/Cellar/go/1.27.0/libexec/src/net/http/server.go:2338 +0x48
  net/http.(*ServeMux).ServeHTTP()
      /opt/homebrew/Cellar/go/1.27.0/libexec/src/net/http/server.go:2903 +0x1a8
  github.com/cortexproject/cortex/pkg/alertmanager.TestRaceReproApplyConfigVsGetAlerts()
      /Users/fgonzalez/source/github.com/cortexproject/cortex/pr-7988/pkg/alertmanager/race_repro_test.go:59 +0xacc
  github.com/cortexproject/cortex/pkg/alertmanager.TestRaceReproApplyConfigVsGetAlerts()
      /Users/fgonzalez/source/github.com/cortexproject/cortex/pr-7988/pkg/alertmanager/race_repro_test.go:47 +0x4b0
  sort.insertionSort()

@timonegk

timonegk commented Dec 4, 2026

Copy link
Copy Markdown
Author

@friedrichg I could reproduce it and committed a fix

Signed-off-by: Timon Engelke <timon.engelke@inovex.de>

This branch has not been deployed

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants