Skip to content

Commit

Permalink
Add utf8-strict-mode flag for mimirtool (#7227)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
grobinson-grafana authored Feb 1, 2024
1 parent 8bbb883 commit aebda28
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions pkg/mimirtool/commands/alerts.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -41,6 +45,7 @@ type AlertmanagerCommand struct {
DisableColor bool
ValidateOnly bool
OutputDir string
UTF8StrictMode bool

cli *client.MimirClient
}
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down

0 comments on commit aebda28

Please sign in to comment.