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

[ENHANCEMENT] Add mimir-http-prefix option to introduce a prefix for Mimir URL when use legacy routes. #8069

Merged
merged 3 commits into from
May 27, 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 @@ -101,6 +101,7 @@

* [CHANGE] Deprecated `--rule-files` flag in favor of CLI arguments. #7756
* [BUGFIX] Fix panic in `loadgen` subcommand. #7629
* [ENHANCEMENT] Add `mimir-http-prefix` configuration to set the Mimir URL prefix when using legacy routes. #8069
* [ENHANCEMENT] `mimirtool promql format`: Format PromQL query with Prometheus' string or pretty-print formatter. #7742
* [BUGFIX] `mimirtool rules prepare`: do not add aggregation label to `on()` clause if already present in `group_left()` or `group_right()`. #7839
* [BUGFIX] Analyze Grafana: fix parsing queries with variables. #8062
Expand Down
6 changes: 5 additions & 1 deletion pkg/mimirtool/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ type Config struct {
ID string `yaml:"id"`
TLS tls.ClientConfig
UseLegacyRoutes bool `yaml:"use_legacy_routes"`
MimirHTTPPrefix string `yaml:"mimir_http_prefix"`
AuthToken string `yaml:"auth_token"`
ExtraHeaders map[string]string `yaml:"extra_headers"`
}
Expand Down Expand Up @@ -97,7 +98,10 @@ func New(cfg Config) (*MimirClient, error) {

path := rulerAPIPath
if cfg.UseLegacyRoutes {
path = legacyAPIPath
var err error
if path, err = url.JoinPath(cfg.MimirHTTPPrefix, legacyAPIPath); err != nil {
return nil, err
}
}
Copy link
Contributor

@lamida lamida May 9, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit. Should we validate that the user mustn't set MimirHTTPPrefix if UseLegacyRoutes is false? MimirHTTPPrefix is a new config, I think it might be better to strictly only allow this config when UseLegacyRoutes is set, instead of just ignoring the config.

Copy link
Contributor Author

@ying-jeanne ying-jeanne May 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might be tricky to check here, since MimirHTTPPrefix has a default value /prometheus, in client we will not able to distinguish the not set and default value.


return &MimirClient{
Expand Down
49 changes: 34 additions & 15 deletions pkg/mimirtool/client/rules_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,13 @@ func TestMimirClient_X(t *testing.T) {
}))
defer ts.Close()

client, err := New(Config{
Address: ts.URL,
ID: "my-id",
Key: "my-key",
ExtraHeaders: map[string]string{
"key1": "value1",
"key2": "value2",
},
})
require.NoError(t, err)

for _, tc := range []struct {
test string
namespace string
name string
expURLPath string
test string
namespace string
name string
expURLPath string
useLegacyRoutes bool
mimirHTTPPrefix string
}{
{
test: "regular-characters",
Expand Down Expand Up @@ -71,9 +62,37 @@ func TestMimirClient_X(t *testing.T) {
name: "last-char-slash/",
expURLPath: "/prometheus/config/v1/rules/My%2FNamespace/last-char-slash%2F",
},
{
test: "use legacy routes with mimir-http-prefix",
namespace: "my-namespace",
name: "my-name",
useLegacyRoutes: true,
mimirHTTPPrefix: "/foo",
expURLPath: "/foo/api/v1/rules/my-namespace/my-name",
},
{
test: "use non legacy routes with mimir-http-prefix ignored",
namespace: "my-namespace",
name: "my-name",
useLegacyRoutes: false,
mimirHTTPPrefix: "/foo",
expURLPath: "/prometheus/config/v1/rules/my-namespace/my-name",
},
} {
t.Run(tc.test, func(t *testing.T) {
ctx := context.Background()
client, err := New(Config{
Address: ts.URL,
ID: "my-id",
Key: "my-key",
ExtraHeaders: map[string]string{
"key1": "value1",
"key2": "value2",
},
UseLegacyRoutes: tc.useLegacyRoutes,
MimirHTTPPrefix: tc.mimirHTTPPrefix,
})
require.NoError(t, err)
require.NoError(t, client.DeleteRuleGroup(ctx, tc.namespace, tc.name))

req := <-requestCh
Expand Down
3 changes: 3 additions & 0 deletions pkg/mimirtool/commands/env_var.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ type EnvVarNames struct {
TLSInsecureSkipVerify string
TenantID string
UseLegacyRoutes string
MimirHTTPPrefix string
AuthToken string
ExtraHeaders string
}
Expand All @@ -29,6 +30,7 @@ func NewEnvVarsWithPrefix(prefix string) EnvVarNames {
useLegacyRoutes = "USE_LEGACY_ROUTES"
authToken = "AUTH_TOKEN"
extraHeaders = "EXTRA_HEADERS"
mimirHTTPPrefix = "MIMIR_HTTP_PREFIX"
)

if len(prefix) > 0 && prefix[len(prefix)-1] != '_' {
Expand All @@ -47,5 +49,6 @@ func NewEnvVarsWithPrefix(prefix string) EnvVarNames {
UseLegacyRoutes: prefix + useLegacyRoutes,
AuthToken: prefix + authToken,
ExtraHeaders: prefix + extraHeaders,
MimirHTTPPrefix: prefix + mimirHTTPPrefix,
}
}
5 changes: 5 additions & 0 deletions pkg/mimirtool/commands/rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,11 @@ func (r *RuleCommand) Register(app *kingpin.Application, envVars EnvVarNames, re
Envar(envVars.UseLegacyRoutes).
BoolVar(&r.ClientConfig.UseLegacyRoutes)

c.Flag("mimir-http-prefix", "Used when use-legacy-routes is set to true. The prefix to use for the url when contacting Grafana Mimir; alternatively, set "+envVars.MimirHTTPPrefix+".").
Default("/prometheus").
Envar(envVars.MimirHTTPPrefix).
StringVar(&r.ClientConfig.MimirHTTPPrefix)

c.Flag("tls-ca-path", "TLS CA certificate to verify Grafana Mimir API as part of mTLS; alternatively, set "+envVars.TLSCAPath+".").
Default("").
Envar(envVars.TLSCAPath).
Expand Down
Loading