Alerting: Fix client to external Alertmanager to correctly build URL for Mimir Alertmanager (#63676)

This commit is contained in:
Yuri Tseretyan 2023-02-23 13:55:26 -05:00 committed by GitHub
parent dd5b115164
commit 98e1aeaebd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 50 additions and 0 deletions

View File

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

View File

@ -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) {