Skip to content

Commit 96f4336

Browse files
authored
Pass request with context on client call (#274)
1 parent 2f905b9 commit 96f4336

File tree

4 files changed

+14
-13
lines changed

4 files changed

+14
-13
lines changed

pkg/client/alerts.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func (r *CortexClient) CreateAlertmanagerConfig(ctx context.Context, cfg string,
2626
return err
2727
}
2828

29-
res, err := r.doRequest(alertmanagerAPIPath, "POST", payload)
29+
res, err := r.doRequest(ctx, alertmanagerAPIPath, "POST", payload)
3030
if err != nil {
3131
return err
3232
}
@@ -38,7 +38,7 @@ func (r *CortexClient) CreateAlertmanagerConfig(ctx context.Context, cfg string,
3838

3939
// DeleteAlermanagerConfig deletes the users alertmanagerconfig
4040
func (r *CortexClient) DeleteAlermanagerConfig(ctx context.Context) error {
41-
res, err := r.doRequest(alertmanagerAPIPath, "DELETE", nil)
41+
res, err := r.doRequest(ctx, alertmanagerAPIPath, "DELETE", nil)
4242
if err != nil {
4343
return err
4444
}
@@ -50,7 +50,7 @@ func (r *CortexClient) DeleteAlermanagerConfig(ctx context.Context) error {
5050

5151
// GetAlertmanagerConfig retrieves a rule group
5252
func (r *CortexClient) GetAlertmanagerConfig(ctx context.Context) (string, map[string]string, error) {
53-
res, err := r.doRequest(alertmanagerAPIPath, "GET", nil)
53+
res, err := r.doRequest(ctx, alertmanagerAPIPath, "GET", nil)
5454
if err != nil {
5555
log.Debugln("no alert config present in response")
5656
return "", nil, err

pkg/client/client.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -103,16 +103,16 @@ func (r *CortexClient) Query(ctx context.Context, query string) (*http.Response,
103103
query = fmt.Sprintf("query=%s&time=%d", query, time.Now().Unix())
104104
escapedQuery := url.PathEscape(query)
105105

106-
res, err := r.doRequest("/api/prom/api/v1/query?"+escapedQuery, "GET", nil)
106+
res, err := r.doRequest(ctx, "/api/prom/api/v1/query?"+escapedQuery, "GET", nil)
107107
if err != nil {
108108
return nil, err
109109
}
110110

111111
return res, nil
112112
}
113113

114-
func (r *CortexClient) doRequest(path, method string, payload []byte) (*http.Response, error) {
115-
req, err := buildRequest(path, method, *r.endpoint, payload)
114+
func (r *CortexClient) doRequest(ctx context.Context, path, method string, payload []byte) (*http.Response, error) {
115+
req, err := buildRequest(ctx, path, method, *r.endpoint, payload)
116116
if err != nil {
117117
return nil, err
118118
}
@@ -205,7 +205,7 @@ func joinPath(baseURLPath, targetPath string) string {
205205
return strings.TrimSuffix(baseURLPath, "/") + targetPath
206206
}
207207

208-
func buildRequest(p, m string, endpoint url.URL, payload []byte) (*http.Request, error) {
208+
func buildRequest(ctx context.Context, p, m string, endpoint url.URL, payload []byte) (*http.Request, error) {
209209
// parse path parameter again (as it already contains escaped path information
210210
pURL, err := url.Parse(p)
211211
if err != nil {
@@ -217,5 +217,5 @@ func buildRequest(p, m string, endpoint url.URL, payload []byte) (*http.Request,
217217
endpoint.RawPath = joinPath(endpoint.EscapedPath(), pURL.EscapedPath())
218218
}
219219
endpoint.Path = joinPath(endpoint.Path, pURL.Path)
220-
return http.NewRequest(m, endpoint.String(), bytes.NewBuffer(payload))
220+
return http.NewRequestWithContext(ctx, m, endpoint.String(), bytes.NewBuffer(payload))
221221
}

pkg/client/client_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package client
22

33
import (
4+
"context"
45
"net/http"
56
"net/url"
67
"testing"
@@ -86,7 +87,7 @@ func TestBuildURL(t *testing.T) {
8687
url, err := url.Parse(tt.url)
8788
require.NoError(t, err)
8889

89-
req, err := buildRequest(tt.path, tt.method, *url, []byte{})
90+
req, err := buildRequest(context.Background(), tt.path, tt.method, *url, []byte{})
9091
require.NoError(t, err)
9192
require.Equal(t, tt.resultURL, req.URL.String())
9293
})

pkg/client/rules.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ func (r *CortexClient) CreateRuleGroup(ctx context.Context, namespace string, rg
2323
escapedNamespace := url.PathEscape(namespace)
2424
path := r.apiPath + "/" + escapedNamespace
2525

26-
res, err := r.doRequest(path, "POST", payload)
26+
res, err := r.doRequest(ctx, path, "POST", payload)
2727
if err != nil {
2828
return err
2929
}
@@ -39,7 +39,7 @@ func (r *CortexClient) DeleteRuleGroup(ctx context.Context, namespace, groupName
3939
escapedGroupName := url.PathEscape(groupName)
4040
path := r.apiPath + "/" + escapedNamespace + "/" + escapedGroupName
4141

42-
res, err := r.doRequest(path, "DELETE", nil)
42+
res, err := r.doRequest(ctx, path, "DELETE", nil)
4343
if err != nil {
4444
return err
4545
}
@@ -56,7 +56,7 @@ func (r *CortexClient) GetRuleGroup(ctx context.Context, namespace, groupName st
5656
path := r.apiPath + "/" + escapedNamespace + "/" + escapedGroupName
5757

5858
fmt.Println(path)
59-
res, err := r.doRequest(path, "GET", nil)
59+
res, err := r.doRequest(ctx, path, "GET", nil)
6060
if err != nil {
6161
return nil, err
6262
}
@@ -88,7 +88,7 @@ func (r *CortexClient) ListRules(ctx context.Context, namespace string) (map[str
8888
path = path + "/" + namespace
8989
}
9090

91-
res, err := r.doRequest(path, "GET", nil)
91+
res, err := r.doRequest(ctx, path, "GET", nil)
9292
if err != nil {
9393
return nil, err
9494
}

0 commit comments

Comments
 (0)