Skip to content

Commit 4fdaaf7

Browse files
authored
Set tenant id in prom analyse command (#31)
* Set tenant id in prom analyse command - refactor roundtripper to be used in query_runner too - fixes #13 Signed-off-by: Charlie Le <[email protected]> * Update tests, changelog, docs Signed-off-by: Charlie Le <[email protected]> --------- Signed-off-by: Charlie Le <[email protected]>
1 parent be36bcf commit 4fdaaf7

File tree

5 files changed

+70
-18
lines changed

5 files changed

+70
-18
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ Order should be `CHANGE`, `FEATURE`, `ENHANCEMENT`, and `BUGFIX`
77
* [CHANGE] Updates version of Go to 1.22 and Alpine to 3.19.1 in Dockerfiles
88
* [FEATURE] Make rulerAPI Path configurable
99
* [FEATURE] Add tool to deserialize alertmanager state file
10+
* [BUGFIX] Set tenant id in prom analyse command
1011

1112
## v0.11.1
1213
* [BUGFIX] Fix check for new version

pkg/bench/query_runner.go

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"context"
55
"flag"
66
"math/rand"
7-
"net/http"
87
"sync"
98
"time"
109

@@ -19,6 +18,8 @@ import (
1918
config_util "github.com/prometheus/common/config"
2019
"github.com/thanos-io/thanos/pkg/discovery/dns"
2120
"github.com/thanos-io/thanos/pkg/extprom"
21+
22+
"github.com/cortexproject/cortex-tools/pkg/httpmiddleware"
2223
)
2324

2425
type QueryConfig struct {
@@ -139,24 +140,12 @@ func (q *queryRunner) queryWorker(queryChan chan query) {
139140
}
140141
}
141142

142-
type tenantIDRoundTripper struct {
143-
tenantName string
144-
next http.RoundTripper
145-
}
146-
147-
func (r *tenantIDRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
148-
if r.tenantName != "" {
149-
req.Header.Set("X-Scope-OrgID", r.tenantName)
150-
}
151-
return r.next.RoundTrip(req)
152-
}
153-
154143
func newQueryClient(url, tenantName, username, password string) (v1.API, error) {
155144
apiClient, err := api.NewClient(api.Config{
156145
Address: url,
157-
RoundTripper: &tenantIDRoundTripper{
158-
tenantName: tenantName,
159-
next: config_util.NewBasicAuthRoundTripper(username, config_util.Secret(password), "", api.DefaultRoundTripper),
146+
RoundTripper: &httpmiddleware.TenantIDRoundTripper{
147+
TenantName: tenantName,
148+
Next: config_util.NewBasicAuthRoundTripper(username, config_util.Secret(password), "", api.DefaultRoundTripper),
160149
},
161150
})
162151

pkg/commands/analyse_prometheus.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"gopkg.in/alecthomas/kingpin.v2"
1818

1919
"github.com/cortexproject/cortex-tools/pkg/analyse"
20+
"github.com/cortexproject/cortex-tools/pkg/httpmiddleware"
2021
)
2122

2223
type PrometheusAnalyseCommand struct {
@@ -71,8 +72,11 @@ func (cmd *PrometheusAnalyseCommand) run(_ *kingpin.ParseContext) error {
7172
rt = config.NewBasicAuthRoundTripper(cmd.username, config.Secret(cmd.password), "", api.DefaultRoundTripper)
7273
}
7374
promClient, err := api.NewClient(api.Config{
74-
Address: cmd.address,
75-
RoundTripper: rt,
75+
Address: cmd.address,
76+
RoundTripper: &httpmiddleware.TenantIDRoundTripper{
77+
TenantName: cmd.username,
78+
Next: rt,
79+
},
7680
})
7781
if err != nil {
7882
return err

pkg/httpmiddleware/roundtripper.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package httpmiddleware
2+
3+
import "net/http"
4+
5+
// TenantIDRoundTripper is a custom implementation of http.RoundTripper.
6+
// It adds a tenant name to the request header before passing it to the next RoundTripper.
7+
type TenantIDRoundTripper struct {
8+
// TenantName is the name of the tenant to be added to the request header.
9+
TenantName string
10+
// Next is the next RoundTripper in the chain.
11+
Next http.RoundTripper
12+
}
13+
14+
// RoundTrip adds the tenant name to the request header and then passes the request to the next RoundTripper.
15+
// If TenantName is not set, it simply passes the request to the next RoundTripper.
16+
// It returns the response from the next RoundTripper and any error encountered.
17+
func (r *TenantIDRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
18+
if r.TenantName != "" {
19+
req.Header.Set("X-Scope-OrgID", r.TenantName)
20+
}
21+
return r.Next.RoundTrip(req)
22+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package httpmiddleware
2+
3+
import (
4+
"net/http"
5+
"net/http/httptest"
6+
"testing"
7+
8+
"github.com/stretchr/testify/require"
9+
)
10+
11+
// TestTenantIDRoundTripper_RoundTrip tests the RoundTrip method of TenantIDRoundTripper.
12+
// It creates a new TenantIDRoundTripper with a TenantName and the default http transport as Next.
13+
// It then sends a request and checks if the X-Scope-OrgID header of the request is correctly set to the TenantName.
14+
// It also checks if the status code of the response is OK (200).
15+
func TestTenantIDRoundTripper_RoundTrip(t *testing.T) {
16+
// Create a new TenantIDRoundTripper with a TenantName and the default http transport as Next.
17+
roundTripper := &TenantIDRoundTripper{
18+
TenantName: "test-tenant",
19+
Next: http.DefaultTransport,
20+
}
21+
22+
// Create a new request.
23+
req := httptest.NewRequest("GET", "https://example.com", nil)
24+
25+
// Send the request using the RoundTrip method of the TenantIDRoundTripper.
26+
resp, err := roundTripper.RoundTrip(req)
27+
28+
// Check if there was an error.
29+
require.NoError(t, err)
30+
31+
// Check if the status code of the response is OK (200).
32+
require.Equal(t, http.StatusOK, resp.StatusCode)
33+
34+
// Check if the X-Scope-OrgID header of the request is correctly set to the TenantName.
35+
require.Equal(t, "test-tenant", req.Header.Get("X-Scope-OrgID"))
36+
}

0 commit comments

Comments
 (0)