diff --git a/CHANGELOG.md b/CHANGELOG.md index 843aadea..597e2288 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Fixed + +- Replace Alertmanager RoundTripper with custom NewRequest. + ## [0.12.0] - 2024-11-05 ### Added diff --git a/pkg/alertmanager/alertmanager.go b/pkg/alertmanager/alertmanager.go index bf255d40..206e6b4c 100644 --- a/pkg/alertmanager/alertmanager.go +++ b/pkg/alertmanager/alertmanager.go @@ -18,8 +18,9 @@ type Config struct { } type AlertManager struct { - address string - client *http.Client + address string + tenantId string + client *http.Client } func New(config Config) (*AlertManager, error) { @@ -27,15 +28,10 @@ func New(config Config) (*AlertManager, error) { return nil, microerror.Maskf(invalidConfigError, "%T.Address must not be empty", config) } - client := http.Client{ - Transport: &customTransport{ - tenantId: config.TenantId, - }, - } - return &AlertManager{ - address: config.Address, - client: &client, + address: config.Address, + client: http.DefaultClient, + tenantId: config.TenantId, }, nil } @@ -62,7 +58,13 @@ func (am *AlertManager) CreateSilence(s *Silence) error { return microerror.Mask(err) } - resp, err := am.client.Post(endpoint, "application/json", bytes.NewBuffer(jsonValues)) + req, err := am.NewRequest(http.MethodPost, endpoint, bytes.NewBuffer(jsonValues)) + if err != nil { + return microerror.Mask(err) + } + req.Header.Add("Content-Type", "application/json") + + resp, err := am.client.Do(req) if err != nil { return microerror.Mask(err) } @@ -101,7 +103,12 @@ func (am *AlertManager) ListSilences() ([]Silence, error) { var silences []Silence - resp, err := am.client.Get(endpoint) + req, err := am.NewRequest(http.MethodGet, endpoint, nil) + if err != nil { + return nil, microerror.Mask(err) + } + + resp, err := am.client.Do(req) if err != nil { return nil, microerror.Mask(err) } @@ -132,7 +139,7 @@ func (am *AlertManager) ListSilences() ([]Silence, error) { func (am *AlertManager) DeleteSilenceByID(id string) error { endpoint := fmt.Sprintf("%s/api/v2/silence/%s", am.address, id) - req, err := http.NewRequest("DELETE", endpoint, nil) + req, err := am.NewRequest(http.MethodDelete, endpoint, nil) if err != nil { return microerror.Mask(err) } diff --git a/pkg/alertmanager/http.go b/pkg/alertmanager/http.go new file mode 100644 index 00000000..0ea03c5b --- /dev/null +++ b/pkg/alertmanager/http.go @@ -0,0 +1,21 @@ +package alertmanager + +import ( + "io" + "net/http" +) + +// NewRequest creates a new http.Request with the given method, url and body. +// It adds the tenantId as X-Scope-OrgID header to the request if it is set. +func (am *AlertManager) NewRequest(method, url string, body io.Reader) (*http.Request, error) { + req, err := http.NewRequest(method, url, body) + if err != nil { + return nil, err + } + + if am.tenantId != "" { + req.Header.Add("X-Scope-OrgID", am.tenantId) + } + + return req, nil +} diff --git a/pkg/alertmanager/transport.go b/pkg/alertmanager/transport.go deleted file mode 100644 index 94b19f41..00000000 --- a/pkg/alertmanager/transport.go +++ /dev/null @@ -1,14 +0,0 @@ -package alertmanager - -import "net/http" - -type customTransport struct { - tenantId string -} - -func (t *customTransport) RoundTrip(req *http.Request) (*http.Response, error) { - if t.tenantId != "" { - req.Header.Add("X-Scope-OrgID", t.tenantId) - } - return http.DefaultTransport.RoundTrip(req) -}