From 7e191dd4cfdc3851162ed889f45268804a6c3e8e Mon Sep 17 00:00:00 2001 From: Christian Haudum Date: Thu, 21 Apr 2022 08:53:42 +0200 Subject: [PATCH] Do not parse string of empty matchers (#5980) This fixes a bug in the index gateway when querying values for a label. Since the gRPC handler for LabelValuesForMetricName in the index gateway allows empty matchers in the LabelValuesForMetricNameRequest, we need to check if the matchers string is an empty matcher (`{}`) before we parse the string. This bug was introduced with the index gateway api refactoring in #5892 Fixes: #5965 Signed-off-by: Christian Haudum --- pkg/logql/syntax/parser.go | 6 +++++- pkg/storage/stores/shipper/indexgateway/gateway.go | 11 ++++++++--- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/pkg/logql/syntax/parser.go b/pkg/logql/syntax/parser.go index b334c5647438..12b5f072e280 100644 --- a/pkg/logql/syntax/parser.go +++ b/pkg/logql/syntax/parser.go @@ -15,7 +15,11 @@ import ( "github.com/grafana/loki/pkg/util" ) -const errAtleastOneEqualityMatcherRequired = "queries require at least one regexp or equality matcher that does not have an empty-compatible value. For instance, app=~\".*\" does not meet this requirement, but app=~\".+\" will" +const ( + EmptyMatchers = "{}" + + errAtleastOneEqualityMatcherRequired = "queries require at least one regexp or equality matcher that does not have an empty-compatible value. For instance, app=~\".*\" does not meet this requirement, but app=~\".+\" will" +) var parserPool = sync.Pool{ New: func() interface{} { diff --git a/pkg/storage/stores/shipper/indexgateway/gateway.go b/pkg/storage/stores/shipper/indexgateway/gateway.go index b2c785fb79bd..df2bd1aa385e 100644 --- a/pkg/storage/stores/shipper/indexgateway/gateway.go +++ b/pkg/storage/stores/shipper/indexgateway/gateway.go @@ -333,9 +333,14 @@ func (g *Gateway) LabelValuesForMetricName(ctx context.Context, req *indexgatewa if err != nil { return nil, err } - matchers, err := syntax.ParseMatchers(req.Matchers) - if err != nil { - return nil, err + var matchers []*labels.Matcher + // An empty matchers string cannot be parsed, + // therefore we check the string representation of the the matchers. + if req.Matchers != syntax.EmptyMatchers { + matchers, err = syntax.ParseMatchers(req.Matchers) + if err != nil { + return nil, err + } } names, err := g.indexQuerier.LabelValuesForMetricName(ctx, instanceID, req.From, req.Through, req.MetricName, req.LabelName, matchers...) if err != nil {