Skip to content

filter: prune StoreAPI fan-out for enumerable regex __name__ matchers - #9006

Open
HarnageaGabriel wants to merge 1 commit into
thanos-io:mainfrom
HarnageaGabriel:feature/metric-name-filter-regex-sets
Open

HarnageaGabriel wants to merge 1 commit into
thanos-io:mainfrom
HarnageaGabriel:feature/metric-name-filter-regex-sets

Conversation

@HarnageaGabriel

Copy link
Copy Markdown
  • I added CHANGELOG entry for this change.

Changes

Extends the Cuckoo metric-name store filter (--enable-feature=metric-names-filter, added in #7787) so it can prune fan-out for more queries.

Before, CuckooMetricNameStoreFilter.Matches only skipped a store when the request carried an exact __name__="x" matcher. A regex matcher such as __name__=~"a|b|c" — extremely common in Grafana dashboards, recording-rule groups and or queries — was ignored, so the querier always fanned out even when none of a, b, c exist on that store.

Matches now:

  • collects every __name__ matcher instead of returning on the first one;
  • treats a MatchRegexp matcher whose value set is fully enumerable (labels.Matcher.SetMatches()) as a constraint, using its literal alternatives;
  • keeps ignoring negative matchers and non-reducible regexes (e.g. __name__=~"a.*"), so those stay non-constraining and correctness is unchanged;
  • returns false only when, for at least one constraint, none of its candidate metric names are present in the filter (AND semantics across constraints, OR within a set).

This only tightens an already-probabilistic, false-positive-only check, so it cannot cause missed data: an enumerable regex that would previously fan out unconditionally now fans out iff at least one of its names might be present.

Touched files: pkg/filter/cuckoo.go, pkg/filter/cuckoo_test.go (new), CHANGELOG.md. No proto or API changes.

Part of #1611.

Verification

  • New table-driven TestCuckooMetricNameStoreFilter_Matches covering exact hit/miss, enumerable regex (all-miss vs partial-hit), non-enumerable regex, no __name__ matcher, empty matchers, multi-constraint AND, and non-__name__ regex being ignored.
  • New BenchmarkCuckooMetricNameStoreFilter_Matches (equal ~56 ns/op, regexp_set ~106 ns/op on a 10k-entry filter).
  • gofmt -l, go build ./pkg/filter/..., go vet ./pkg/filter/..., go test ./pkg/filter/... all pass.

The Cuckoo metric-name store filter (enabled with
`--enable-feature=metric-names-filter`) previously only excluded a store
when the query carried an exact `__name__="x"` matcher. The very common
dashboard pattern `__name__=~"a|b|c"` bypassed the filter entirely and
always triggered a fan-out request, even when none of the listed metric
names exist on that store.

`Matches` now collects every `__name__` matcher and treats regex matchers
whose value set is fully enumerable (`labels.Matcher.SetMatches()`) as
constraints too. A store is skipped when, for at least one constraint,
none of its candidate metric names are present in the filter. Regex
matchers that cannot be reduced to a literal set (e.g. `__name__=~"a.*"`)
stay non-constraining, so correctness is unchanged.

Refs thanos-io#1611

Signed-off-by: HarnageaGabriel <gabriel.harnagea06@gmail.com>
@HarnageaGabriel
HarnageaGabriel force-pushed the feature/metric-name-filter-regex-sets branch from e13452f to e2cdc22 Compare September 2, 2026 21:19
@codecov

codecov Bot commented Sep 2, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 64.65%. Comparing base (8908023) to head (e2cdc22).
⚠️ Report is 33 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #9006      +/-   ##
==========================================
+ Coverage   64.51%   64.65%   +0.14%     
==========================================
  Files         289      289              
  Lines       37352    37387      +35     
==========================================
+ Hits        24097    24174      +77     
+ Misses      11158    11128      -30     
+ Partials     2097     2085      -12     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Comment thread pkg/filter/cuckoo.go
f.mtx.RLock()
defer f.mtx.RUnlock()

var constraints [][]string

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome change! Only thing I'd ask you to do is maybe we can pool the slice here? This is a very hot path

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants