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

Set tenant id in prom analyse command #31

Merged
merged 2 commits into from
Apr 24, 2024
Merged
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Order should be `CHANGE`, `FEATURE`, `ENHANCEMENT`, and `BUGFIX`
* [CHANGE] Upgrade to v1.13.2
* [FEATURE] Make rulerAPI Path configurable
* [FEATURE] Add tool to deserialize alertmanager state file
* [BUGFIX] Set tenant id in prom analyse command

## v0.11.1
* [BUGFIX] Fix check for new version
Expand Down
21 changes: 5 additions & 16 deletions pkg/bench/query_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"flag"
"math/rand"
"net/http"
"sync"
"time"

Expand All @@ -19,6 +18,8 @@ import (
config_util "github.com/prometheus/common/config"
"github.com/thanos-io/thanos/pkg/discovery/dns"
"github.com/thanos-io/thanos/pkg/extprom"

"github.com/cortexproject/cortex-tools/pkg/httpmiddleware"
)

type QueryConfig struct {
Expand Down Expand Up @@ -139,24 +140,12 @@ func (q *queryRunner) queryWorker(queryChan chan query) {
}
}

type tenantIDRoundTripper struct {
tenantName string
next http.RoundTripper
}

func (r *tenantIDRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
if r.tenantName != "" {
req.Header.Set("X-Scope-OrgID", r.tenantName)
}
return r.next.RoundTrip(req)
}

func newQueryClient(url, tenantName, username, password string) (v1.API, error) {
apiClient, err := api.NewClient(api.Config{
Address: url,
RoundTripper: &tenantIDRoundTripper{
tenantName: tenantName,
next: config_util.NewBasicAuthRoundTripper(username, config_util.Secret(password), "", api.DefaultRoundTripper),
RoundTripper: &httpmiddleware.TenantIDRoundTripper{
TenantName: tenantName,
Next: config_util.NewBasicAuthRoundTripper(username, config_util.Secret(password), "", api.DefaultRoundTripper),
},
})

Expand Down
8 changes: 6 additions & 2 deletions pkg/commands/analyse_prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"gopkg.in/alecthomas/kingpin.v2"

"github.com/cortexproject/cortex-tools/pkg/analyse"
"github.com/cortexproject/cortex-tools/pkg/httpmiddleware"
)

type PrometheusAnalyseCommand struct {
Expand Down Expand Up @@ -72,8 +73,11 @@ func (cmd *PrometheusAnalyseCommand) run(_ *kingpin.ParseContext) error {
rt = config.NewBasicAuthRoundTripper(cmd.username, config.Secret(cmd.password), "", api.DefaultRoundTripper)
}
promClient, err := api.NewClient(api.Config{
Address: cmd.address,
RoundTripper: rt,
Address: cmd.address,
RoundTripper: &httpmiddleware.TenantIDRoundTripper{
TenantName: cmd.username,
Next: rt,
},
})
if err != nil {
return err
Expand Down
22 changes: 22 additions & 0 deletions pkg/httpmiddleware/roundtripper.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package httpmiddleware

import "net/http"

// TenantIDRoundTripper is a custom implementation of http.RoundTripper.
// It adds a tenant name to the request header before passing it to the next RoundTripper.
type TenantIDRoundTripper struct {
// TenantName is the name of the tenant to be added to the request header.
TenantName string
// Next is the next RoundTripper in the chain.
Next http.RoundTripper
}

// RoundTrip adds the tenant name to the request header and then passes the request to the next RoundTripper.
// If TenantName is not set, it simply passes the request to the next RoundTripper.
// It returns the response from the next RoundTripper and any error encountered.
func (r *TenantIDRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
if r.TenantName != "" {
req.Header.Set("X-Scope-OrgID", r.TenantName)
}
return r.Next.RoundTrip(req)
}
36 changes: 36 additions & 0 deletions pkg/httpmiddleware/roundtripper_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package httpmiddleware

import (
"net/http"
"net/http/httptest"
"testing"

"github.com/stretchr/testify/require"
)

// TestTenantIDRoundTripper_RoundTrip tests the RoundTrip method of TenantIDRoundTripper.
// It creates a new TenantIDRoundTripper with a TenantName and the default http transport as Next.
// It then sends a request and checks if the X-Scope-OrgID header of the request is correctly set to the TenantName.
// It also checks if the status code of the response is OK (200).
func TestTenantIDRoundTripper_RoundTrip(t *testing.T) {
// Create a new TenantIDRoundTripper with a TenantName and the default http transport as Next.
roundTripper := &TenantIDRoundTripper{
TenantName: "test-tenant",
Next: http.DefaultTransport,
}

// Create a new request.
req := httptest.NewRequest("GET", "https://example.com", nil)

// Send the request using the RoundTrip method of the TenantIDRoundTripper.
resp, err := roundTripper.RoundTrip(req)

// Check if there was an error.
require.NoError(t, err)

// Check if the status code of the response is OK (200).
require.Equal(t, http.StatusOK, resp.StatusCode)

// Check if the X-Scope-OrgID header of the request is correctly set to the TenantName.
require.Equal(t, "test-tenant", req.Header.Get("X-Scope-OrgID"))
}