diff --git a/pkg/store/local.go b/pkg/store/local.go index c9f05609b4..0a2dd1c7de 100644 --- a/pkg/store/local.go +++ b/pkg/store/local.go @@ -130,7 +130,7 @@ func ScanGRPCCurlProtoStreamMessages(data []byte, atEOF bool) (advance int, toke // Series returns all series for a requested time range and label matcher. The returned data may // exceed the requested time bounds. func (s *LocalStore) Series(r *storepb.SeriesRequest, srv storepb.Store_SeriesServer) error { - match, matchers, err := matchesExternalLabels(r.Matchers, s.extLabels, nil) + match, matchers, err := matchesExternalLabels(r.Matchers, s.extLabels, nil, false) if err != nil { return status.Error(codes.InvalidArgument, err.Error()) } diff --git a/pkg/store/prometheus.go b/pkg/store/prometheus.go index c18e81bc9d..76d9105844 100644 --- a/pkg/store/prometheus.go +++ b/pkg/store/prometheus.go @@ -125,7 +125,7 @@ func (p *PrometheusStore) Series(r *storepb.SeriesRequest, seriesSrv storepb.Sto extLset := p.externalLabelsFn() - match, matchers, err := matchesExternalLabels(r.Matchers, extLset, nil) + match, matchers, err := matchesExternalLabels(r.Matchers, extLset, nil, false) if err != nil { return status.Error(codes.InvalidArgument, err.Error()) } @@ -488,11 +488,11 @@ func (p *PrometheusStore) startPromRemoteRead(ctx context.Context, q *prompb.Que // matchesExternalLabels returns false if given matchers are not matching external labels. // If true, matchesExternalLabels also returns Prometheus matchers without those matching external labels. -func matchesExternalLabels(ms []storepb.LabelMatcher, externalLabels labels.Labels, mc *storepb.MatcherConverter) (bool, []*labels.Matcher, error) { +func matchesExternalLabels(ms []storepb.LabelMatcher, externalLabels labels.Labels, mc *storepb.MatcherConverter, withMatcherMetrics bool) (bool, []*labels.Matcher, error) { var tms []*labels.Matcher var err error if mc != nil { - tms, err = mc.MatchersToPromMatchers(ms...) + tms, err = mc.MatchersToPromMatchers(withMatcherMetrics, ms...) } else { tms, err = storepb.MatchersToPromMatchers(ms...) } @@ -543,7 +543,7 @@ func (p *PrometheusStore) encodeChunk(ss []prompb.Sample) (storepb.Chunk_Encodin func (p *PrometheusStore) LabelNames(ctx context.Context, r *storepb.LabelNamesRequest) (*storepb.LabelNamesResponse, error) { extLset := p.externalLabelsFn() - match, matchers, err := matchesExternalLabels(r.Matchers, extLset, nil) + match, matchers, err := matchesExternalLabels(r.Matchers, extLset, nil, false) if err != nil { return nil, status.Error(codes.InvalidArgument, err.Error()) } @@ -606,7 +606,7 @@ func (p *PrometheusStore) LabelValues(ctx context.Context, r *storepb.LabelValue extLset := p.externalLabelsFn() - match, matchers, err := matchesExternalLabels(r.Matchers, extLset, nil) + match, matchers, err := matchesExternalLabels(r.Matchers, extLset, nil, false) if err != nil { return nil, status.Error(codes.InvalidArgument, err.Error()) } diff --git a/pkg/store/proxy.go b/pkg/store/proxy.go index d9dd7b641f..a2ce71a92b 100644 --- a/pkg/store/proxy.go +++ b/pkg/store/proxy.go @@ -281,7 +281,7 @@ func (s *ProxyStore) Series(originalRequest *storepb.SeriesRequest, srv storepb. reqLogger = log.With(reqLogger, "request", originalRequest.String()) } - match, matchers, err := matchesExternalLabels(originalRequest.Matchers, s.selectorLabels, s.matcherConverter) + match, matchers, err := matchesExternalLabels(originalRequest.Matchers, s.selectorLabels, s.matcherConverter, true) if err != nil { return status.Error(codes.InvalidArgument, err.Error()) } @@ -480,7 +480,7 @@ func (s *ProxyStore) LabelNames(ctx context.Context, originalRequest *storepb.La if s.debugLogging { reqLogger = log.With(reqLogger, "request", originalRequest.String()) } - match, matchers, err := matchesExternalLabels(originalRequest.Matchers, s.selectorLabels, s.matcherConverter) + match, matchers, err := matchesExternalLabels(originalRequest.Matchers, s.selectorLabels, s.matcherConverter, false) if err != nil { return nil, status.Error(codes.InvalidArgument, err.Error()) } @@ -583,7 +583,7 @@ func (s *ProxyStore) LabelValues(ctx context.Context, originalRequest *storepb.L return nil, status.Error(codes.InvalidArgument, "label name parameter cannot be empty") } - match, matchers, err := matchesExternalLabels(originalRequest.Matchers, s.selectorLabels, s.matcherConverter) + match, matchers, err := matchesExternalLabels(originalRequest.Matchers, s.selectorLabels, s.matcherConverter, false) if err != nil { return nil, status.Error(codes.InvalidArgument, err.Error()) } diff --git a/pkg/store/storepb/custom.go b/pkg/store/storepb/custom.go index 7b3b133e37..9b45482200 100644 --- a/pkg/store/storepb/custom.go +++ b/pkg/store/storepb/custom.go @@ -462,7 +462,7 @@ func NewMatcherConverter(cacheCapacity int, reg prometheus.Registerer) (*Matcher } // MatchersToPromMatchers converts proto label matchers to Prometheus label matchers. It caches regex conversions. -func (c *MatcherConverter) MatchersToPromMatchers(ms ...LabelMatcher) ([]*labels.Matcher, error) { +func (c *MatcherConverter) MatchersToPromMatchers(withMetric bool, ms ...LabelMatcher) ([]*labels.Matcher, error) { res := make([]*labels.Matcher, 0, len(ms)) for _, m := range ms { if m.Type != LabelMatcher_RE && m.Type != LabelMatcher_NRE { @@ -474,10 +474,14 @@ func (c *MatcherConverter) MatchersToPromMatchers(ms ...LabelMatcher) ([]*labels res = append(res, pm) continue } - c.metrics.cacheTotalCount.Inc() + if withMetric { + c.metrics.cacheTotalCount.Inc() + } if pm, ok := c.cache.Get(m); ok { // cache hit - c.metrics.cacheHitCount.Inc() + if withMetric { + c.metrics.cacheHitCount.Inc() + } res = append(res, pm) continue } @@ -489,7 +493,9 @@ func (c *MatcherConverter) MatchersToPromMatchers(ms ...LabelMatcher) ([]*labels c.cache.Add(m, pm) res = append(res, pm) } - c.metrics.cacheSizeGauge.Set(float64(c.cache.Len())) + if withMetric { + c.metrics.cacheSizeGauge.Set(float64(c.cache.Len())) + } return res, nil } diff --git a/pkg/store/storepb/custom_test.go b/pkg/store/storepb/custom_test.go index 1b56d235e9..24394f54d0 100644 --- a/pkg/store/storepb/custom_test.go +++ b/pkg/store/storepb/custom_test.go @@ -742,7 +742,7 @@ func TestMatcherConverter_MatchersToPromMatchers(t *testing.T) { cacheHitsBefore := getMetricValue(converter.metrics.cacheHitCount) cacheTotalBefore := getMetricValue(converter.metrics.cacheTotalCount) - promMatchers, err := converter.MatchersToPromMatchers(c.inputMatchers...) + promMatchers, err := converter.MatchersToPromMatchers(true, c.inputMatchers...) if c.expectError { require.Error(t, err) @@ -794,12 +794,12 @@ func BenchmarkMatcherConverter_REWithAndWithoutCache(b *testing.B) { b.Run("With Cache", func(b *testing.B) { // cache warm-up for _, lm := range nonTrivialRegexes { - _, err := converter.MatchersToPromMatchers(lm) + _, err := converter.MatchersToPromMatchers(false, lm) require.NoError(b, err) } b.ResetTimer() for i := 0; i < b.N; i++ { - _, err := converter.MatchersToPromMatchers(nonTrivialRegexes...) + _, err := converter.MatchersToPromMatchers(false, nonTrivialRegexes...) require.NoError(b, err) } }) diff --git a/pkg/store/tsdb.go b/pkg/store/tsdb.go index 9d4efe5b82..e7d39e740c 100644 --- a/pkg/store/tsdb.go +++ b/pkg/store/tsdb.go @@ -255,7 +255,7 @@ func (s *TSDBStore) Series(r *storepb.SeriesRequest, seriesSrv storepb.Store_Ser srv = fs } - match, matchers, err := matchesExternalLabels(r.Matchers, s.getExtLset(), s.matcherConverter) + match, matchers, err := matchesExternalLabels(r.Matchers, s.getExtLset(), s.matcherConverter, false) if err != nil { return status.Error(codes.InvalidArgument, err.Error()) } @@ -382,7 +382,7 @@ func (s *TSDBStore) Series(r *storepb.SeriesRequest, seriesSrv storepb.Store_Ser func (s *TSDBStore) LabelNames(ctx context.Context, r *storepb.LabelNamesRequest) ( *storepb.LabelNamesResponse, error, ) { - match, matchers, err := matchesExternalLabels(r.Matchers, s.getExtLset(), s.matcherConverter) + match, matchers, err := matchesExternalLabels(r.Matchers, s.getExtLset(), s.matcherConverter, false) if err != nil { return nil, status.Error(codes.InvalidArgument, err.Error()) } @@ -444,7 +444,7 @@ func (s *TSDBStore) LabelValues(ctx context.Context, r *storepb.LabelValuesReque } } - match, matchers, err := matchesExternalLabels(r.Matchers, s.getExtLset(), s.matcherConverter) + match, matchers, err := matchesExternalLabels(r.Matchers, s.getExtLset(), s.matcherConverter, false) if err != nil { return nil, status.Error(codes.InvalidArgument, err.Error()) }