diff --git a/pkg/services/ngalert/sender/router.go b/pkg/services/ngalert/sender/router.go index 077901d9613..a1e20abd80c 100644 --- a/pkg/services/ngalert/sender/router.go +++ b/pkg/services/ngalert/sender/router.go @@ -6,6 +6,7 @@ import ( "errors" "fmt" "net/url" + "path" "sort" "sync" "time" @@ -270,7 +271,10 @@ func (d *AlertsRouter) buildExternalURL(ds *datasources.DataSource) (string, err if parsed.Path == "" { parsed.Path = "/" } - parsed = parsed.JoinPath("/alertmanager") + lastSegment := path.Base(parsed.Path) + if lastSegment != "alertmanager" { + parsed = parsed.JoinPath("/alertmanager") + } default: } } diff --git a/pkg/services/ngalert/sender/router_test.go b/pkg/services/ngalert/sender/router_test.go index 361d738191f..20cce2e4691 100644 --- a/pkg/services/ngalert/sender/router_test.go +++ b/pkg/services/ngalert/sender/router_test.go @@ -540,6 +540,42 @@ func TestBuildExternalURL(t *testing.T) { }, expectedURL: "https://localhost:9000/path/to/am", }, + { + name: "do not add /alertmanager to path when last segment already contains it", + ds: &datasources.DataSource{ + URL: "https://localhost:9000/path/to/alertmanager", + JsonData: func() *simplejson.Json { + r := simplejson.New() + r.Set("implementation", "mimir") + return r + }(), + }, + expectedURL: "https://localhost:9000/path/to/alertmanager", + }, + { + name: "add /alertmanager to path when last segment does not exactly match", + ds: &datasources.DataSource{ + URL: "https://localhost:9000/path/to/alertmanagerasdf", + JsonData: func() *simplejson.Json { + r := simplejson.New() + r.Set("implementation", "mimir") + return r + }(), + }, + expectedURL: "https://localhost:9000/path/to/alertmanagerasdf/alertmanager", + }, + { + name: "add /alertmanager to path when exists but is not last segment", + ds: &datasources.DataSource{ + URL: "https://localhost:9000/alertmanager/path/to/am", + JsonData: func() *simplejson.Json { + r := simplejson.New() + r.Set("implementation", "mimir") + return r + }(), + }, + expectedURL: "https://localhost:9000/alertmanager/path/to/am/alertmanager", + }, } for _, test := range tests { t.Run(test.name, func(t *testing.T) {