Skip to content

Commit

Permalink
fix(chstorage): respect query parameter when selecting log label va…
Browse files Browse the repository at this point in the history
…lues
  • Loading branch information
tdakkota committed Jun 13, 2024
1 parent 306b12d commit a976b34
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 20 deletions.
45 changes: 31 additions & 14 deletions internal/chstorage/querier_logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
),
)
Expand All @@ -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(),
Expand All @@ -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),
Expand All @@ -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 != "" {
Expand Down
12 changes: 6 additions & 6 deletions internal/chstorage/querier_logs_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down Expand Up @@ -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)
}
}

Expand Down Expand Up @@ -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 {
Expand All @@ -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)
Expand All @@ -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)
}
}

Expand All @@ -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)
}
Expand Down

0 comments on commit a976b34

Please sign in to comment.