From fb2ea7af46bf40157ea47a72014355bdfcad7e96 Mon Sep 17 00:00:00 2001 From: Florian Derler Date: Fri, 7 Mar 2025 10:40:31 +0100 Subject: [PATCH] add matcher handling to prometheus LabelNames/Values queries --- prometheus/querier.go | 36 ++++++++++++++++++++++++++++++++---- 1 file changed, 32 insertions(+), 4 deletions(-) diff --git a/prometheus/querier.go b/prometheus/querier.go index 32a1fee2..7ccd10d7 100644 --- a/prometheus/querier.go +++ b/prometheus/querier.go @@ -13,6 +13,7 @@ import ( "github.com/prometheus/prometheus/util/annotations" "github.com/lomik/graphite-clickhouse/config" + "github.com/lomik/graphite-clickhouse/finder" "github.com/lomik/graphite-clickhouse/helper/clickhouse" "github.com/lomik/graphite-clickhouse/pkg/scope" "github.com/lomik/graphite-clickhouse/pkg/where" @@ -33,9 +34,25 @@ func (q *Querier) Close() error { // LabelValues returns all potential values for a label name. func (q *Querier) LabelValues(ctx context.Context, label string, hints *storage.LabelHints, matchers ...*labels.Matcher) ([]string, annotations.Annotations, error) { - // @TODO: support matchers - w := where.New() - w.And(where.HasPrefix("Tag1", label+"=")) + terms := []finder.TaggedTerm{ + { + Key: strings.ReplaceAll(label, `_`, `\_`), + Op: finder.TaggedTermEq, + Value: "*", + HasWildcard: true, + }, + } + + matcherTerms, err := makeTaggedFromPromQL(matchers) + if err != nil { + return nil, nil, err + } + terms = append(terms, matcherTerms...) + + w, _, err := finder.TaggedWhere(terms, q.config.FeatureFlags.UseCarbonBehavior, q.config.FeatureFlags.DontMatchMissingTags) + if err != nil { + return nil, nil, err + } fromDate := timeNow().AddDate(0, 0, -q.config.ClickHouse.TaggedAutocompleDays) w.Andf("Date >= '%s'", fromDate.Format("2006-01-02")) @@ -70,8 +87,19 @@ func (q *Querier) LabelValues(ctx context.Context, label string, hints *storage. // LabelNames returns all the unique label names present in the block in sorted order. func (q *Querier) LabelNames(ctx context.Context, hints *storage.LabelHints, matchers ...*labels.Matcher) ([]string, annotations.Annotations, error) { - // @TODO support matchers + terms, err := makeTaggedFromPromQL(matchers) + if err != nil { + return nil, nil, err + } w := where.New() + // @TODO: this is duplicate to the for in finder.TaggedWhere. (different start...) + for i := 0; i < len(terms); i++ { + and, err := finder.TaggedTermWhereN(&terms[i], q.config.FeatureFlags.UseCarbonBehavior, q.config.FeatureFlags.DontMatchMissingTags) + if err != nil { + return nil, nil, err + } + w.And(and) + } fromDate := time.Now().AddDate(0, 0, -q.config.ClickHouse.TaggedAutocompleDays).UTC() w.Andf("Date >= '%s'", fromDate.Format("2006-01-02"))