Skip to content

Commit a0c89bd

Browse files
authored
alertmanager: Avoid breaking /api/v1/alerts if legacy prefix is empty (#3905)
If the legacy prefix is an empty string, it's interpreted as a handler for `/*`. Any handlers added afterwards are silently ignored, so requests to `/api/v1/alerts` are routed to the legacy handler, rather than to `am.*UserConfig` as would be expected. Meanwhile any handlers added BEFORE the `/*` legacy handler still work as expected - making this very difficult to debug! Signed-off-by: Nick Parker <nick@nickbp.com>
1 parent df80ec1 commit a0c89bd

File tree

2 files changed

+10
-7
lines changed

2 files changed

+10
-7
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@
9999
* [BUGFIX] Prevent panic at start if the http_prefix setting doesn't have a valid value. #3796
100100
* [BUGFIX] Memberlist: fixed panic caused by race condition in `armon/go-metrics` used by memberlist client. #3724
101101
* [BUGFIX] Querier: returning 422 (instead of 500) when query hits `max_chunks_per_query` limit with block storage. #3895
102+
* [BUGFIX] Alertmanager: Ensure that experimental `/api/v1/alerts` endpoints work when `-http.prefix` is empty. #3905
102103

103104
## 1.7.0
104105

pkg/api/api.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -172,19 +172,21 @@ func (a *API) RegisterAlertmanager(am *alertmanager.MultitenantAlertmanager, tar
172172
a.RegisterRoutesWithPrefix(a.cfg.AlertmanagerHTTPPrefix, am, true)
173173
level.Debug(a.logger).Log("msg", "api: registering alertmanager", "path_prefix", a.cfg.AlertmanagerHTTPPrefix)
174174

175-
// If the target is Alertmanager, enable the legacy behaviour. Otherwise only enable
176-
// the component routed API.
177-
if target {
178-
a.RegisterRoute("/status", am.GetStatusHandler(), false, "GET")
179-
a.RegisterRoutesWithPrefix(a.cfg.LegacyHTTPPrefix, am, true)
180-
}
181-
182175
// MultiTenant Alertmanager Experimental API routes
183176
if apiEnabled {
184177
a.RegisterRoute("/api/v1/alerts", http.HandlerFunc(am.GetUserConfig), true, "GET")
185178
a.RegisterRoute("/api/v1/alerts", http.HandlerFunc(am.SetUserConfig), true, "POST")
186179
a.RegisterRoute("/api/v1/alerts", http.HandlerFunc(am.DeleteUserConfig), true, "DELETE")
187180
}
181+
182+
// If the target is Alertmanager, enable the legacy behaviour. Otherwise only enable
183+
// the component routed API.
184+
if target {
185+
a.RegisterRoute("/status", am.GetStatusHandler(), false, "GET")
186+
// WARNING: If LegacyHTTPPrefix is an empty string, any other paths added after this point will be
187+
// silently ignored by the HTTP service. Therefore, this must be the last route to be configured.
188+
a.RegisterRoutesWithPrefix(a.cfg.LegacyHTTPPrefix, am, true)
189+
}
188190
}
189191

190192
// RegisterAPI registers the standard endpoints associated with a running Cortex.

0 commit comments

Comments
 (0)