Remote Alertmanager(refactor): Only parse the URL once (#78631)

* Remote Alertmanager(refactor): Only parse the URL once

Exactly what it says in the tin.

Signed-off-by: gotjosh <josue.abreu@gmail.com>

* use the existing tests

Signed-off-by: gotjosh <josue.abreu@gmail.com>

---------

Signed-off-by: gotjosh <josue.abreu@gmail.com>
This commit is contained in:
gotjosh
2023-11-24 11:05:13 +00:00
committed by GitHub
parent 9306020426
commit 8120306fea
3 changed files with 18 additions and 15 deletions

View File

@ -55,13 +55,18 @@ func NewAlertmanager(cfg AlertmanagerConfig, orgID int64) (*Alertmanager, error)
}
if cfg.URL == "" {
return nil, fmt.Errorf("empty URL for tenant %s", cfg.TenantID)
return nil, fmt.Errorf("empty remote Alertmanager URL for tenant '%s'", cfg.TenantID)
}
u, err := url.Parse(cfg.URL)
if err != nil {
return nil, fmt.Errorf("unable to parse remote Alertmanager URL: %w", err)
}
logger := log.New("ngalert.remote.alertmanager")
mcCfg := &mimirClient.Config{
Address: cfg.URL,
URL: u,
TenantID: cfg.TenantID,
Password: cfg.BasicAuthPassword,
Logger: logger,
@ -72,11 +77,6 @@ func NewAlertmanager(cfg AlertmanagerConfig, orgID int64) (*Alertmanager, error)
return nil, err
}
u, err := url.Parse(cfg.URL)
if err != nil {
return nil, err
}
u = u.JoinPath("/alertmanager", amclient.DefaultBasePath)
transport := httptransport.NewWithClient(u.Host, u.Path, []string{u.Scheme}, &client)

View File

@ -37,7 +37,15 @@ func TestNewAlertmanager(t *testing.T) {
tenantID: "1234",
password: "test",
orgID: 1,
expErr: "empty URL for tenant 1234",
expErr: "empty remote Alertmanager URL for tenant '1234'",
},
{
name: "invalid URL",
url: "asdasd%sasdsd",
tenantID: "1234",
password: "test",
orgID: 1,
expErr: "unable to parse remote Alertmanager URL: parse \"asdasd%sasdsd\": invalid URL escape \"%sa\"",
},
{
name: "valid parameters",

View File

@ -32,7 +32,7 @@ type Mimir struct {
}
type Config struct {
Address string
URL *url.URL
TenantID string
Password string
@ -61,11 +61,6 @@ func (e *errorResponse) Error() string {
}
func New(cfg *Config) (*Mimir, error) {
endpoint, err := url.Parse(cfg.Address)
if err != nil {
return nil, err
}
rt := &MimirAuthRoundTripper{
TenantID: cfg.TenantID,
Password: cfg.Password,
@ -77,7 +72,7 @@ func New(cfg *Config) (*Mimir, error) {
}
return &Mimir{
endpoint: endpoint,
endpoint: cfg.URL,
client: c,
logger: cfg.Logger,
}, nil