Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
machine424 committed Jan 14, 2025
1 parent b90b493 commit f333107
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 9 deletions.
1 change: 1 addition & 0 deletions cmd/prometheus/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,7 @@ func main() {
slog.SetDefault(logger)
// Hacky but temporary
parser.Logger = logger.With("component", "parser")
relabel.Logger = logger.With("component", "relabel")

notifs := notifications.NewNotifications(cfg.maxNotificationsSubscribers, prometheus.DefaultRegisterer)
cfg.web.NotificationsSub = notifs.Sub
Expand Down
8 changes: 8 additions & 0 deletions model/relabel/relabel.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,15 @@ import (

"github.com/grafana/regexp"
"github.com/prometheus/common/model"
"github.com/prometheus/common/promslog"

"github.com/prometheus/prometheus/model/labels"
"github.com/prometheus/prometheus/promql/parser"
)

var (
Logger = promslog.NewNopLogger()

relabelTarget = regexp.MustCompile(`^(?:(?:[a-zA-Z_]|\$(?:\{\w+\}|\w+))+\w*)+$`)

DefaultRelabelConfig = Config{
Expand Down Expand Up @@ -255,6 +259,10 @@ func relabel(cfg *Config, lb *labels.Builder) (keep bool) {
values = make([]string, 0, len(cfg.SourceLabels))
}
for _, ln := range cfg.SourceLabels {
if ln == model.BucketLabel || ln == model.QuantileLabel {
parser.NarrowSelectors.WithLabelValues("relabel").Inc()
Logger.Debug("relabel_config concerns 'le' or 'quantile' labels, it may need to be adjusted to count for float values", "source_labels", cfg.SourceLabels, "regex", cfg.Regex)
}
values = append(values, lb.Get(string(ln)))
}
val := strings.Join(values, cfg.Separator)
Expand Down
15 changes: 8 additions & 7 deletions promql/parser/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,18 @@ import (
)

var (
Logger = promslog.NewNopLogger()
narrowSelectorOnIntegers = prometheus.NewCounter(
Logger = promslog.NewNopLogger()
NarrowSelectors = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "prometheus_parser_narrow_selectors_on_integers_count",
Help: "Number of selectors that are explicitly set to only match integers on 'quantile' and 'le' labels",
Name: "prometheus_narrow_selectors_count",
Help: "Number of selectors that may unintentionally only match integers on 'quantile' and 'le' labels",
},
[]string{"component"},
)
)

func init() {
prometheus.MustRegister(narrowSelectorOnIntegers)
prometheus.MustRegister(NarrowSelectors)
}

// isInteger is a light strconv.Atoi that avoids allocations due to Atoi's returned error and doesn't
Expand Down Expand Up @@ -97,7 +98,7 @@ Outer:
switch lm.Type {
case labels.MatchEqual, labels.MatchNotEqual:
if n, ok := isInteger(lm.Value); ok {
narrowSelectorOnIntegers.Inc()
NarrowSelectors.WithLabelValues("parser").Inc()
Logger.Debug("selector set to explicitly match an integer, but values could be floats", "narrow_matcher_label", lm.Name, "integer", n, "matchers", vs.LabelMatchers)
}
break Outer
Expand All @@ -112,7 +113,7 @@ Outer:
}
}

narrowSelectorOnIntegers.Inc()
NarrowSelectors.WithLabelValues("parser").Inc()
Logger.Debug("selector set to explicitly match integers only, but values could be floats", "narrow_matcher_label", lm.Name, "matchers", vs.LabelMatchers)
break Outer
}
Expand Down
4 changes: 2 additions & 2 deletions promql/parser/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4658,15 +4658,15 @@ func Test_checkLabelMatchers(t *testing.T) {

for _, tt := range cases {
t.Run(tt.expr, func(t *testing.T) {
counter := client_testutil.ToFloat64(narrowSelectorOnIntegers)
counter := client_testutil.ToFloat64(NarrowSelectors.WithLabelValues("parser"))
err := tt.fn(tt.expr)
require.NoError(t, err)

if tt.shouldIncrementCounter {
counter++
}

require.Equal(t, counter, client_testutil.ToFloat64(narrowSelectorOnIntegers))
require.Equal(t, counter, client_testutil.ToFloat64(NarrowSelectors.WithLabelValues("parser")))
})
}
}

0 comments on commit f333107

Please sign in to comment.