Alerting: Only append /alertmanager when sending alerts to mimir targets if not already present (#85543)

Don't append alertmanager if not present
This commit is contained in:
Alexander Weaver 2024-04-04 11:58:41 -05:00 committed by GitHub
parent 5687243d0b
commit 623ee3a2be
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 41 additions and 1 deletions

View File

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

View File

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