Skip to content

Commit

Permalink
Support UTF-8 label matchers: Add metrics to matchers compat package (p…
Browse files Browse the repository at this point in the history
…rometheus#3658)

* Add metrics to matchers compat package

This commit adds the following metrics to the compat package:

  alertmanager_matchers_parse
  alertmanager_matchers_disagree
  alertmanager_matchers_incompatible
  alertmanager_matchers_invalid

With a label called origin to differentiate the different sources
of inputs: the configuration file, the API, and amtool.

The disagree_total metric is incremented when an input is invalid
in both parsers, but results in different parsed representations,
then there is disagreement. This should not happen, and suggests
their is either a bug in one of the parsers or a mistake in the
backwards compatible guarantees of the matchers/parse parser.

The incompatible_total metric is incremented when an input is valid
in pkg/labels, but not the UTF-8 parser in matchers/parse. In such
case, the matcher should be updated to be compatible. This often
means adding double quotes around the right hand side of the matcher.
For example, foo="bar".

The invalid_total metric is incremented when an input is invalid
in both parsers. This was never a valid input.

The tests have been updated to check the metrics are incremented
as expected.

Signed-off-by: George Robinson <george.robinson@grafana.com>

---------

Signed-off-by: George Robinson <george.robinson@grafana.com>
Signed-off-by: Gokhan Sari <gokhan@sari.me>
  • Loading branch information
grobinson-grafana authored and th0th committed Mar 23, 2024
1 parent 382e44f commit 0a4c1cf
Show file tree
Hide file tree
Showing 12 changed files with 306 additions and 127 deletions.
2 changes: 1 addition & 1 deletion api/v2/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -684,7 +684,7 @@ func (api *API) postSilencesHandler(params silence_ops.PostSilencesParams) middl
func parseFilter(filter []string) ([]*labels.Matcher, error) {
matchers := make([]*labels.Matcher, 0, len(filter))
for _, matcherString := range filter {
matcher, err := compat.Matcher(matcherString)
matcher, err := compat.Matcher(matcherString, "api")
if err != nil {
return nil, err
}
Expand Down
6 changes: 3 additions & 3 deletions cli/alert_add.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,14 @@ func (a *alertAddCmd) addAlert(ctx context.Context, _ *kingpin.ParseContext) err
if len(a.labels) > 0 {
// Allow the alertname label to be defined implicitly as the first argument rather
// than explicitly as a key=value pair.
if _, err := compat.Matcher(a.labels[0]); err != nil {
if _, err := compat.Matcher(a.labels[0], "cli"); err != nil {
a.labels[0] = fmt.Sprintf("alertname=%s", strconv.Quote(a.labels[0]))
}
}

ls := make(models.LabelSet, len(a.labels))
for _, l := range a.labels {
matcher, err := compat.Matcher(l)
matcher, err := compat.Matcher(l, "cli")
if err != nil {
return err
}
Expand All @@ -96,7 +96,7 @@ func (a *alertAddCmd) addAlert(ctx context.Context, _ *kingpin.ParseContext) err

annotations := make(models.LabelSet, len(a.annotations))
for _, a := range a.annotations {
matcher, err := compat.Matcher(a)
matcher, err := compat.Matcher(a, "cli")
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion cli/alert_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func (a *alertQueryCmd) queryAlerts(ctx context.Context, _ *kingpin.ParseContext
// the user wants alertname=<arg> and prepend `alertname=` to
// the front.
m := a.matcherGroups[0]
_, err := compat.Matcher(m)
_, err := compat.Matcher(m, "cli")
if err != nil {
a.matcherGroups[0] = fmt.Sprintf("alertname=%s", strconv.Quote(m))
}
Expand Down
2 changes: 1 addition & 1 deletion cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func initMatchersCompat(_ *kingpin.ParseContext) error {
if err != nil {
kingpin.Fatalf("error parsing the feature flag list: %v\n", err)
}
compat.InitFromFlags(logger, featureConfig)
compat.InitFromFlags(logger, compat.RegisteredMetrics, featureConfig)
return nil
}

Expand Down
4 changes: 2 additions & 2 deletions cli/silence_add.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,15 +95,15 @@ func (c *silenceAddCmd) add(ctx context.Context, _ *kingpin.ParseContext) error
// If the parser fails then we likely don't have a (=|=~|!=|!~) so lets
// assume that the user wants alertname=<arg> and prepend `alertname=`
// to the front.
_, err := compat.Matcher(c.matchers[0])
_, err := compat.Matcher(c.matchers[0], "cli")
if err != nil {
c.matchers[0] = fmt.Sprintf("alertname=%s", strconv.Quote(c.matchers[0]))
}
}

matchers := make([]labels.Matcher, 0, len(c.matchers))
for _, s := range c.matchers {
m, err := compat.Matcher(s)
m, err := compat.Matcher(s, "cli")
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion cli/silence_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func (c *silenceQueryCmd) query(ctx context.Context, _ *kingpin.ParseContext) er
// If the parser fails then we likely don't have a (=|=~|!=|!~) so lets
// assume that the user wants alertname=<arg> and prepend `alertname=`
// to the front.
_, err := compat.Matcher(c.matchers[0])
_, err := compat.Matcher(c.matchers[0], "cli")
if err != nil {
c.matchers[0] = fmt.Sprintf("alertname=%s", strconv.Quote(c.matchers[0]))
}
Expand Down
2 changes: 1 addition & 1 deletion cli/test_routing.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func (c *routingShow) routingTestAction(ctx context.Context, _ *kingpin.ParseCon
// Parse labels to LabelSet.
ls := make(models.LabelSet, len(c.labels))
for _, l := range c.labels {
matcher, err := compat.Matcher(l)
matcher, err := compat.Matcher(l, "cli")
if err != nil {
kingpin.Fatalf("Failed to parse labels: %v\n", err)
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/alertmanager/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ func run() int {
level.Error(logger).Log("msg", "error parsing the feature flag list", "err", err)
return 1
}
compat.InitFromFlags(logger, ff)
compat.InitFromFlags(logger, compat.RegisteredMetrics, ff)

err = os.MkdirAll(*dataDir, 0o777)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -1006,7 +1006,7 @@ func (m *Matchers) UnmarshalYAML(unmarshal func(interface{}) error) error {
return err
}
for _, line := range lines {
pm, err := compat.Matchers(line)
pm, err := compat.Matchers(line, "config")
if err != nil {
return err
}
Expand All @@ -1032,7 +1032,7 @@ func (m *Matchers) UnmarshalJSON(data []byte) error {
return err
}
for _, line := range lines {
pm, err := compat.Matchers(line)
pm, err := compat.Matchers(line, "config")
if err != nil {
return err
}
Expand Down
60 changes: 60 additions & 0 deletions matchers/compat/metrics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// Copyright 2023 The Prometheus Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package compat

import (
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
)

const (
OriginAPI = "api"
OriginConfig = "config"
)

var DefaultOrigins = []string{
OriginAPI,
OriginConfig,
}

var RegisteredMetrics = NewMetrics(prometheus.DefaultRegisterer)

type Metrics struct {
Total *prometheus.GaugeVec
DisagreeTotal *prometheus.GaugeVec
IncompatibleTotal *prometheus.GaugeVec
InvalidTotal *prometheus.GaugeVec
}

func NewMetrics(r prometheus.Registerer) *Metrics {
m := &Metrics{
Total: promauto.With(r).NewGaugeVec(prometheus.GaugeOpts{
Name: "alertmanager_matchers_parse",
Help: "Total number of matcher inputs parsed, including invalid inputs.",
}, []string{"origin"}),
DisagreeTotal: promauto.With(r).NewGaugeVec(prometheus.GaugeOpts{
Name: "alertmanager_matchers_disagree",
Help: "Total number of matcher inputs which produce different parsings (disagreement).",
}, []string{"origin"}),
IncompatibleTotal: promauto.With(r).NewGaugeVec(prometheus.GaugeOpts{
Name: "alertmanager_matchers_incompatible",
Help: "Total number of matcher inputs that are incompatible with the UTF-8 parser.",
}, []string{"origin"}),
InvalidTotal: promauto.With(r).NewGaugeVec(prometheus.GaugeOpts{
Name: "alertmanager_matchers_invalid",
Help: "Total number of matcher inputs that could not be parsed.",
}, []string{"origin"}),
}
return m
}
Loading

0 comments on commit 0a4c1cf

Please sign in to comment.