From aebda28c121bb22817afa7293d605d1b7b970fe8 Mon Sep 17 00:00:00 2001 From: George Robinson Date: Thu, 1 Feb 2024 10:45:37 +0000 Subject: [PATCH] Add utf8-strict-mode flag for mimirtool (#7227) * Add utf8-strict-mode flag for mimirtool This commit adds a new flag to mimirtool called utf8-strict-mode. It is the client version the utf8-strict-mode-enabled flag in Mimir. The default mode of mimirtool is to first use the new parser for UTF-8 matchers, and if it fails, fallback to the classic parser. If this happens, it also logs a warning to stdout. It is possible to disable the fallback, and use just the UTF-8 parser, with the -utf8-strict-mode flag. This will help operators ensure their configurations are compatible with the new parser going forward. --- pkg/mimirtool/commands/alerts.go | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/pkg/mimirtool/commands/alerts.go b/pkg/mimirtool/commands/alerts.go index 20bc7a1f0b3..561a71c05ae 100644 --- a/pkg/mimirtool/commands/alerts.go +++ b/pkg/mimirtool/commands/alerts.go @@ -19,9 +19,13 @@ import ( "time" "github.com/alecthomas/kingpin/v2" + gokitlog "github.com/go-kit/log" + "github.com/go-kit/log/level" "github.com/grafana/dskit/cancellation" "github.com/pkg/errors" "github.com/prometheus/alertmanager/config" + "github.com/prometheus/alertmanager/featurecontrol" + "github.com/prometheus/alertmanager/matchers/compat" "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/collectors" "github.com/prometheus/client_golang/prometheus/promauto" @@ -41,6 +45,7 @@ type AlertmanagerCommand struct { DisableColor bool ValidateOnly bool OutputDir string + UTF8StrictMode bool cli *client.MimirClient } @@ -71,6 +76,8 @@ func (a *AlertmanagerCommand) Register(app *kingpin.Application, envVars EnvVarN alertCmd.Flag("tls-key-path", "TLS client certificate private key to authenticate with the Grafana Mimir API as part of mTLS; alternatively, set "+envVars.TLSKeyPath+".").Default("").Envar(envVars.TLSKeyPath).StringVar(&a.ClientConfig.TLS.KeyPath) alertCmd.Flag("tls-insecure-skip-verify", "Skip TLS certificate verification; alternatively, set "+envVars.TLSInsecureSkipVerify+".").Default("false").Envar(envVars.TLSInsecureSkipVerify).BoolVar(&a.ClientConfig.TLS.InsecureSkipVerify) alertCmd.Flag("auth-token", "Authentication token bearer authentication; alternatively, set "+envVars.AuthToken+".").Default("").Envar(envVars.AuthToken).StringVar(&a.ClientConfig.AuthToken) + alertCmd.Flag("utf8-strict-mode", "Enable UTF-8 strict mode. Allows UTF-8 characters in the matchers for routes and inhibition rules, in silences, and in the labels for alerts.").Default("false").BoolVar(&a.UTF8StrictMode) + // Get Alertmanager Configs Command getAlertsCmd := alertCmd.Command("get", "Get the Alertmanager configuration that is currently in the Grafana Mimir Alertmanager.").Action(a.getConfig) getAlertsCmd.Flag("disable-color", "disable colored output").BoolVar(&a.DisableColor) @@ -93,6 +100,22 @@ func (a *AlertmanagerCommand) Register(app *kingpin.Application, envVars EnvVarN } func (a *AlertmanagerCommand) setup(_ *kingpin.ParseContext) error { + // The default mode for mimirtool is to first use the new parser for UTF-8 matchers, + // and if it fails, fallback to the classic parser. If this happens, it also logs a + // warning to stdout. It is possible to disable to fallback, and use just the UTF-8 + // parser, with the -utf8-strict-mode flag. This will help operators ensure their + // configurations are compatible with the new parser going forward. + l := level.NewFilter(gokitlog.NewLogfmtLogger(os.Stdout), level.AllowInfo()) + features := "" + if a.UTF8StrictMode { + features = featurecontrol.FeatureUTF8StrictMode + } + flags, err := featurecontrol.NewFlags(l, features) + if err != nil { + return err + } + compat.InitFromFlags(l, compat.RegisteredMetrics, flags) + cli, err := client.New(a.ClientConfig) if err != nil { return err