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 25, 2024
1 parent 2dd0015 commit f4250a0
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 2 deletions.
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
2 changes: 1 addition & 1 deletion cmd/alertmanager/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,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
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
}

0 comments on commit f4250a0

Please sign in to comment.