From 98e1aeaebdbe255b657f764c3a92307bf4517ef1 Mon Sep 17 00:00:00 2001 From: Yuri Tseretyan Date: Thu, 23 Feb 2023 13:55:26 -0500 Subject: [PATCH] Alerting: Fix client to external Alertmanager to correctly build URL for Mimir Alertmanager (#63676) --- pkg/services/ngalert/sender/router.go | 14 +++++++++ pkg/services/ngalert/sender/router_test.go | 36 ++++++++++++++++++++++ 2 files changed, 50 insertions(+) diff --git a/pkg/services/ngalert/sender/router.go b/pkg/services/ngalert/sender/router.go index 4b23525e799..ed36e5bd76a 100644 --- a/pkg/services/ngalert/sender/router.go +++ b/pkg/services/ngalert/sender/router.go @@ -242,6 +242,20 @@ func (d *AlertsRouter) buildExternalURL(ds *datasources.DataSource) (string, err if err != nil { return "", fmt.Errorf("failed to parse alertmanager datasource url: %w", err) } + + // If this is a Mimir or Cortex implementation, the Alert API is under a different path than config API + if ds.JsonData != nil { + impl := ds.JsonData.Get("implementation").MustString("") + switch impl { + case "mimir", "cortex": + if parsed.Path == "" { + parsed.Path = "/" + } + parsed = parsed.JoinPath("/alertmanager") + default: + } + } + // if basic auth is enabled we need to build the url with basic auth baked in if !ds.BasicAuth { return parsed.String(), nil diff --git a/pkg/services/ngalert/sender/router_test.go b/pkg/services/ngalert/sender/router_test.go index cfab899c4b8..4310f0f3c8b 100644 --- a/pkg/services/ngalert/sender/router_test.go +++ b/pkg/services/ngalert/sender/router_test.go @@ -491,6 +491,42 @@ func TestBuildExternalURL(t *testing.T) { }, expectedURL: "http://localhost:9000/path/to/am", }, + { + name: "adds /alertmanager to path when implementation is mimir", + ds: &datasources.DataSource{ + URL: "https://localhost:9000", + JsonData: func() *simplejson.Json { + r := simplejson.New() + r.Set("implementation", "mimir") + return r + }(), + }, + expectedURL: "https://localhost:9000/alertmanager", + }, + { + name: "adds /alertmanager to path when implementation is cortex", + ds: &datasources.DataSource{ + URL: "https://localhost:9000/path/to/am", + JsonData: func() *simplejson.Json { + r := simplejson.New() + r.Set("implementation", "cortex") + return r + }(), + }, + expectedURL: "https://localhost:9000/path/to/am/alertmanager", + }, + { + name: "do nothing when implementation is prometheus", + ds: &datasources.DataSource{ + URL: "https://localhost:9000/path/to/am", + JsonData: func() *simplejson.Json { + r := simplejson.New() + r.Set("implementation", "prometheus") + return r + }(), + }, + expectedURL: "https://localhost:9000/path/to/am", + }, } for _, test := range tests { t.Run(test.name, func(t *testing.T) {