From a976b34af1569170ac923d3945aac19c3fc7a210 Mon Sep 17 00:00:00 2001 From: tdakkota Date: Fri, 14 Jun 2024 02:10:19 +0300 Subject: [PATCH] fix(chstorage): respect `query` parameter when selecting log label values --- internal/chstorage/querier_logs.go | 45 ++++++++++++++++-------- internal/chstorage/querier_logs_query.go | 12 +++---- 2 files changed, 37 insertions(+), 20 deletions(-) diff --git a/internal/chstorage/querier_logs.go b/internal/chstorage/querier_logs.go index 158dd763..c250551d 100644 --- a/internal/chstorage/querier_logs.go +++ b/internal/chstorage/querier_logs.go @@ -126,6 +126,8 @@ func (q *Querier) LabelValues(ctx context.Context, labelName string, opts logsto attribute.String("chstorage.label", labelName), xattribute.UnixNano("chstorage.range.start", opts.Start), xattribute.UnixNano("chstorage.range.end", opts.End), + attribute.Stringer("chstorage.matchers", opts.Query), + attribute.String("chstorage.table", table), ), ) @@ -140,6 +142,7 @@ func (q *Querier) LabelValues(ctx context.Context, labelName string, opts logsto switch labelName { case logstorage.LabelBody, logstorage.LabelSpanID, logstorage.LabelTraceID: case logstorage.LabelSeverity: + // FIXME(tdakkota): do a proper query with filtering values = []string{ plog.SeverityNumberUnspecified.String(), plog.SeverityNumberTrace.String(), @@ -151,19 +154,23 @@ func (q *Querier) LabelValues(ctx context.Context, labelName string, opts logsto } slices.Sort(values) default: - { - mapping, err := q.getLabelMapping(ctx, []string{labelName}) - if err != nil { - return nil, errors.Wrap(err, "get label mapping") - } - if key, ok := mapping[labelName]; ok { - labelName = key - } + queryLabels := make([]string, 1+len(opts.Query.Matchers)) + queryLabels = append(queryLabels, labelName) + for _, m := range opts.Query.Matchers { + queryLabels = append(queryLabels, string(m.Label)) } - var value proto.ColStr - if err := q.do(ctx, selectQuery{ - Query: chsql.Select(table, chsql.ResultColumn{ + mapping, err := q.getLabelMapping(ctx, queryLabels) + if err != nil { + return nil, errors.Wrap(err, "get label mapping") + } + if key, ok := mapping[labelName]; ok { + labelName = key + } + + var ( + value proto.ColStr + query = chsql.Select(table, chsql.ResultColumn{ Name: "value", Expr: chsql.ArrayJoin(chsql.Array( attrSelector(colAttrs, labelName), @@ -173,9 +180,19 @@ func (q *Querier) LabelValues(ctx context.Context, labelName string, opts logsto Data: &value, }). Distinct(true). - Where(chsql.InTimeRange("timestamp", opts.Start, opts.End)). - Order(chsql.Ident("value"), chsql.Asc). - Limit(1000), + Where(chsql.InTimeRange("timestamp", opts.Start, opts.End)) + ) + for _, m := range opts.Query.Matchers { + expr, err := q.logQLLabelMatcher(m, mapping) + if err != nil { + return nil, err + } + query.Where(expr) + } + query.Order(chsql.Ident("value"), chsql.Asc).Limit(1000) + + if err := q.do(ctx, selectQuery{ + Query: query, OnResult: func(ctx context.Context, block proto.Block) error { for i := 0; i < value.Rows(); i++ { if v := value.Row(i); v != "" { diff --git a/internal/chstorage/querier_logs_query.go b/internal/chstorage/querier_logs_query.go index 57537e02..72fabe5e 100644 --- a/internal/chstorage/querier_logs_query.go +++ b/internal/chstorage/querier_logs_query.go @@ -367,7 +367,7 @@ func (q *Querier) lineFilter(m logql.LineFilter) (e chsql.Expr, rerr error) { case logql.OpRe, logql.OpNotRe: return chsql.Match(chsql.Ident("body"), chsql.String(m.By.Value)), nil default: - return e, errors.Errorf("unexpected op %q", m.Op) + return e, errors.Errorf("unexpected line matcher op %v", m.Op) } } @@ -398,7 +398,7 @@ func (q *Querier) logQLLabelMatcher( chsql.String(m.Value), ), nil default: - return e, errors.Errorf("unexpected op %q", m.Op) + return e, errors.Errorf("unexpected label matcher op %v", m.Op) } } @@ -436,7 +436,7 @@ func (q *Querier) logQLLabelMatcher( } return chsql.In(chsql.Ident("severity_number"), chsql.TupleValues(matches...)), nil default: - return e, errors.Errorf("unexpected op %q", m.Op) + return e, errors.Errorf("unexpected label matcher op %v", m.Op) } case logstorage.LabelBody: switch m.Op { @@ -445,7 +445,7 @@ func (q *Querier) logQLLabelMatcher( case logql.OpRe, logql.OpNotRe: return chsql.Match(chsql.Ident("body"), chsql.String(m.Value)), nil default: - return e, errors.Errorf("unexpected op %q", m.Op) + return e, errors.Errorf("unexpected label matcher op %v", m.Op) } case logstorage.LabelSpanID: return matchHex(chsql.Ident("span_id"), m) @@ -460,7 +460,7 @@ func (q *Querier) logQLLabelMatcher( case logql.OpRe, logql.OpNotRe: return chsql.Match(expr, chsql.String(m.Value)), nil default: - return e, errors.Errorf("unexpected op %q", m.Op) + return e, errors.Errorf("unexpected label matcher op %v", m.Op) } } @@ -482,7 +482,7 @@ func (q *Querier) logQLLabelMatcher( case logql.OpRe, logql.OpNotRe: sub = chsql.Match(selector, chsql.String(m.Value)) default: - return e, errors.Errorf("unexpected op %q", m.Op) + return e, errors.Errorf("unexpected label matcher op %v", m.Op) } exprs = append(exprs, sub) }