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

Support UTF-8 label matchers: Add metrics to matchers compat package #3658

Merged
merged 7 commits into from
Jan 5, 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
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",
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Used present tense here to be consistent with the other metrics which are all present tense.

Copy link
Member

Choose a reason for hiding this comment

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

Sgtm!

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