Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace RoundTripper in favour of custom NewRequest method #477

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
33 changes: 20 additions & 13 deletions pkg/alertmanager/alertmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,20 @@ type Config struct {
}

type AlertManager struct {
address string
client *http.Client
address string
tenantId string
client *http.Client
}

func New(config Config) (*AlertManager, error) {
if config.Address == "" {
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
}

Expand All @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down
21 changes: 21 additions & 0 deletions pkg/alertmanager/http.go
Original file line number Diff line number Diff line change
@@ -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
}
14 changes: 0 additions & 14 deletions pkg/alertmanager/transport.go

This file was deleted.

Loading