Skip to content

[Infra] Post-enrichment filtering for Hosts exclusion filters#260426

Merged
rmyz merged 6 commits intoelastic:mainfrom
rmyz:fix/hosts-exclusion-filter-post-enrichment
Apr 1, 2026
Merged

[Infra] Post-enrichment filtering for Hosts exclusion filters#260426
rmyz merged 6 commits intoelastic:mainfrom
rmyz:fix/hosts-exclusion-filter-post-enrichment

Conversation

@rmyz
Copy link
Copy Markdown
Contributor

@rmyz rmyz commented Mar 31, 2026

Summary

Closes #256157

Hosts that should be excluded by negative filters (e.g., NOT cloud.provider: gcp) were still appearing in the Hosts UI. This happened because APM documents for some hosts lack the filtered field entirely, which causes Elasticsearch must_not clauses to pass those documents through. After enrichment (getAllHosts), these hosts would show the excluded metadata value (e.g., cloud.provider: gcp), contradicting the user's filter.

Changes

  • Post-enrichment filter in getHosts: After getAllHosts enriches host data with metadata (cloud.provider, host.os.name, host.ip), a new filter checks each host's metadata against must_not exclusion values from the user's query. Hosts whose enriched metadata matches an excluded value are removed from the final result.
  • New extractExcludedMetadataValues utility: Parses must_not clauses from the query and extracts excluded field/value pairs for metadata fields. Supports match_phrase, match, term, terms, and nested bool.should inside must_not.
  • Aligned getHostsCount: The count endpoint now follows the same pattern — gets the host name union, and when there are excluded metadata values, runs a lightweight metadata-only query to post-filter before counting.
  • Simplified getFilteredHostNames: Returns string[] instead of { allHosts, filteredHosts }. The allHosts sub-aggregation was removed since it's no longer needed.
  • Simplified getHostNames: Reverted to a plain union of infra + APM hosts. The previous infra-based exclusion logic was insufficient for APM-only hosts not recognized by infra.
  • Removed getHasDataFromSystemIntegration call: The pre-check was unnecessary since getFilteredHostNames handles empty results gracefully.
  • Extracted MAX_HOST_COUNT_LIMIT to common/constants.ts instead of hardcoding.
  • Removed queryHasNegation utility (no longer needed).

Demo

Before

Kapture.2026-03-31.at.15.37.33.mp4

After

Kapture.2026-03-31.at.12.43.39.mp4

How it works

User filter: NOT cloud.provider: gcp

1. getHostNames: union of filteredHosts (infra) + apmHosts → candidate list
2. getAllHosts: enriches candidates with metadata (cloud.provider, host.os.name, host.ip)
3. extractExcludedMetadataValues: parses must_not → { 'cloud.provider': Set(['gcp']) }
4. Post-filter: removes any host where metadata matches excluded values
   e.g., host with enriched cloud.provider: gcp → removed

Testing

  • 8 unit tests for getHosts covering union behavior and post-enrichment filtering (including the exact reproduction scenario)
  • 9 unit tests for extractExcludedMetadataValues covering all query clause types
  • All existing tests updated and passing
Hosts that should be excluded by negative filters (e.g., NOT cloud.provider: gcp)
were still appearing because APM documents often lack the filtered field,
allowing them to bypass Elasticsearch must_not clauses. This adds a
post-enrichment filter that checks enriched metadata values against the
exclusion criteria after data retrieval, catching hosts that slip through
earlier query-level filters.

Closes elastic#256157

Made-with: Cursor
@rmyz rmyz added release_note:fix backport:skip This PR does not require backporting Team:obs-presentation Focus: APM UI, Infra UI, Hosts UI, Universal Profiling, Obs Overview and left Navigation labels Mar 31, 2026
@rmyz rmyz self-assigned this Mar 31, 2026
@rmyz
Copy link
Copy Markdown
Contributor Author

rmyz commented Mar 31, 2026

/ci

@rmyz rmyz changed the title fix: [Infra] Post-enrichment filtering for Hosts exclusion filters Mar 31, 2026
@elasticmachine
Copy link
Copy Markdown
Contributor

elasticmachine commented Mar 31, 2026

💔 Build Failed

Failed CI Steps

Test Failures

  • [job] [logs] FTR Configs #128 / X-Pack Accessibility Tests - Group 1 Management index management indices indices with data index details index details - overview
  • [job] [logs] FTR Configs #128 / X-Pack Accessibility Tests - Group 1 Management index management indices indices with data index details index details - overview

Metrics [docs]

✅ unchanged

History

cc @rmyz

@rmyz
Copy link
Copy Markdown
Contributor Author

rmyz commented Mar 31, 2026

/ci

@rmyz rmyz requested a review from Copilot March 31, 2026 14:14
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Fixes Hosts UI negative-filter exclusions by post-filtering enriched host metadata so hosts whose APM docs lack the filtered field don’t “slip through” must_not and reappear after enrichment.

Changes:

  • Adds extractExcludedMetadataValues to parse must_not exclusions for enriched host metadata fields.
  • Applies post-enrichment filtering in getHosts to remove hosts whose enriched metadata matches excluded values.
  • Updates getHostsCount to align with the new behavior by post-filtering via a lightweight metadata aggregation.

Reviewed changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 6 comments.

Show a summary per file
File Description
x-pack/solutions/observability/plugins/infra/server/routes/infra/lib/utils.ts Adds utility to extract excluded metadata values from must_not query clauses.
x-pack/solutions/observability/plugins/infra/server/routes/infra/lib/utils.test.ts Adds unit tests for excluded-metadata extraction.
x-pack/solutions/observability/plugins/infra/server/routes/infra/lib/host/get_hosts_count.ts Aligns count logic with post-enrichment exclusion filtering using metadata aggregations.
x-pack/solutions/observability/plugins/infra/server/routes/infra/lib/host/get_hosts.ts Applies post-enrichment filtering to host results and simplifies host-name collection.
x-pack/solutions/observability/plugins/infra/server/routes/infra/lib/host/get_hosts.test.ts Adds unit tests covering union behavior and post-enrichment filtering scenarios.
x-pack/solutions/observability/plugins/infra/server/routes/infra/lib/host/get_filtered_hosts.ts Simplifies API to return string[] and adjusts query filter composition/agg naming.
x-pack/solutions/observability/plugins/infra/common/constants.ts Extracts MAX_HOST_COUNT_LIMIT constant.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread x-pack/solutions/observability/plugins/infra/server/routes/infra/lib/utils.ts Outdated
- Wrap documentsFilter in nested bool inside filter to enforce minimum_should_match
- Handle multi-valued metadata (host.ip) in post-enrichment filtering
- Guard bool.should extraction with minimum_should_match check

Made-with: Cursor
@rmyz
Copy link
Copy Markdown
Contributor Author

rmyz commented Mar 31, 2026

/ci

rmyz added 2 commits April 1, 2026 08:56
…ction

When must/filter clauses are present in a bool query, ES defaults
minimum_should_match to 0, making should clauses optional. Only extract
excluded values from bool.should when it is actually required.

Made-with: Cursor
@rmyz
Copy link
Copy Markdown
Contributor Author

rmyz commented Apr 1, 2026

/ci

@rmyz rmyz marked this pull request as ready for review April 1, 2026 07:17
@rmyz rmyz requested a review from a team as a code owner April 1, 2026 07:17
@elasticmachine
Copy link
Copy Markdown
Contributor

Pinging @elastic/obs-presentation-team (Team:obs-presentation)

@macroscopeapp
Copy link
Copy Markdown
Contributor

macroscopeapp Bot commented Apr 1, 2026

Approvability

Verdict: Needs human review

This PR introduces post-enrichment filtering that changes which hosts are returned and counted based on metadata exclusion filters - a significant runtime behavior change affecting data processing pipelines. Additionally, the author does not own any of the modified files, which are all owned by @elastic/obs-presentation-team.

You can customize Macroscope's approvability policy. Learn more.

@rmyz rmyz added backport:version Backport to applied version labels v9.3.3 v9.2.8 and removed backport:skip This PR does not require backporting labels Apr 1, 2026
Copy link
Copy Markdown
Contributor

@sbelastic sbelastic left a comment

Choose a reason for hiding this comment

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

code LGTM, also tested locally and everything worked :)

@rmyz rmyz merged commit 705ddb9 into elastic:main Apr 1, 2026
30 checks passed
@kibanamachine
Copy link
Copy Markdown
Contributor

Starting backport for target branches: 9.2, 9.3

https://github.com/elastic/kibana/actions/runs/23841119069

@rmyz rmyz deleted the fix/hosts-exclusion-filter-post-enrichment branch April 1, 2026 09:16
kibanamachine pushed a commit to kibanamachine/kibana that referenced this pull request Apr 1, 2026
@kibanamachine
Copy link
Copy Markdown
Contributor

💔 Some backports could not be created

Status Branch Result
9.2 Backport failed because of merge conflicts
9.3

Note: Successful backport PRs will be merged automatically after passing CI.

Manual backport

To create the backport manually run:

node scripts/backport --pr 260426

Questions ?

Please refer to the Backport tool documentation

rmyz added a commit to rmyz/kibana that referenced this pull request Apr 1, 2026
…c#260426)

(cherry picked from commit 705ddb9)

# Conflicts:
#	x-pack/solutions/observability/plugins/infra/server/routes/infra/lib/host/get_filtered_hosts.ts
#	x-pack/solutions/observability/plugins/infra/server/routes/infra/lib/host/get_hosts_count.ts
@rmyz
Copy link
Copy Markdown
Contributor Author

rmyz commented Apr 1, 2026

💚 All backports created successfully

Status Branch Result
9.2

Note: Successful backport PRs will be merged automatically after passing CI.

Questions ?

Please refer to the Backport tool documentation

kibanamachine added a commit that referenced this pull request Apr 1, 2026
…260426) (#260649)

# Backport

This will backport the following commits from `main` to `9.3`:
- [[Infra] Post-enrichment filtering for Hosts exclusion filters
(#260426)](#260426)

<!--- Backport version: 9.6.6 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sorenlouv/backport)

<!--BACKPORT [{"author":{"name":"Sergi
Romeu","email":"sergi.romeu@elastic.co"},"sourceCommit":{"committedDate":"2026-04-01T09:11:10Z","message":"[Infra]
Post-enrichment filtering for Hosts exclusion filters
(#260426)","sha":"705ddb99317ff5143909e5c8352db962affac98b","branchLabelMapping":{"^v9.4.0$":"main","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["release_note:fix","backport:version","v9.4.0","Team:obs-presentation","v9.3.3","v9.2.8"],"title":"[Infra]
Post-enrichment filtering for Hosts exclusion
filters","number":260426,"url":"https://github.com/elastic/kibana/pull/260426","mergeCommit":{"message":"[Infra]
Post-enrichment filtering for Hosts exclusion filters
(#260426)","sha":"705ddb99317ff5143909e5c8352db962affac98b"}},"sourceBranch":"main","suggestedTargetBranches":["9.3","9.2"],"targetPullRequestStates":[{"branch":"main","label":"v9.4.0","branchLabelMappingKey":"^v9.4.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/260426","number":260426,"mergeCommit":{"message":"[Infra]
Post-enrichment filtering for Hosts exclusion filters
(#260426)","sha":"705ddb99317ff5143909e5c8352db962affac98b"}},{"branch":"9.3","label":"v9.3.3","branchLabelMappingKey":"^v(\\d+).(\\d+).\\d+$","isSourceBranch":false,"state":"NOT_CREATED"},{"branch":"9.2","label":"v9.2.8","branchLabelMappingKey":"^v(\\d+).(\\d+).\\d+$","isSourceBranch":false,"state":"NOT_CREATED"}]}]
BACKPORT-->

Co-authored-by: Sergi Romeu <sergi.romeu@elastic.co>
rmyz added a commit that referenced this pull request Apr 1, 2026
…260426) (#260658)

# Backport

This will backport the following commits from `main` to `9.2`:
- [[Infra] Post-enrichment filtering for Hosts exclusion filters
(#260426)](#260426)

<!--- Backport version: 11.0.1 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sorenlouv/backport)

<!--BACKPORT [{"author":{"name":"Sergi
Romeu","email":"sergi.romeu@elastic.co"},"sourceCommit":{"committedDate":"2026-04-01T09:11:10Z","message":"[Infra]
Post-enrichment filtering for Hosts exclusion filters
(#260426)","sha":"705ddb99317ff5143909e5c8352db962affac98b","branchLabelMapping":{"^v9.4.0$":"main","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["release_note:fix","backport:version","v9.4.0","Team:obs-presentation","v9.3.3","v9.2.8"],"title":"[Infra]
Post-enrichment filtering for Hosts exclusion
filters","number":260426,"url":"https://github.com/elastic/kibana/pull/260426","mergeCommit":{"message":"[Infra]
Post-enrichment filtering for Hosts exclusion filters
(#260426)","sha":"705ddb99317ff5143909e5c8352db962affac98b"}},"sourceBranch":"main","suggestedTargetBranches":["9.2"],"targetPullRequestStates":[{"branch":"main","label":"v9.4.0","branchLabelMappingKey":"^v9.4.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/260426","number":260426,"mergeCommit":{"message":"[Infra]
Post-enrichment filtering for Hosts exclusion filters
(#260426)","sha":"705ddb99317ff5143909e5c8352db962affac98b"}},{"branch":"9.3","label":"v9.3.3","branchLabelMappingKey":"^v(\\d+).(\\d+).\\d+$","isSourceBranch":false,"url":"https://github.com/elastic/kibana/pull/260649","number":260649,"state":"OPEN"},{"branch":"9.2","label":"v9.2.8","branchLabelMappingKey":"^v(\\d+).(\\d+).\\d+$","isSourceBranch":false,"state":"NOT_CREATED"}]}]
BACKPORT-->
jeramysoucy pushed a commit to jeramysoucy/kibana that referenced this pull request Apr 1, 2026
eokoneyo pushed a commit to davismcphee/kibana that referenced this pull request Apr 2, 2026
paulinashakirova pushed a commit to paulinashakirova/kibana that referenced this pull request Apr 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backport:version Backport to applied version labels release_note:fix Team:obs-presentation Focus: APM UI, Infra UI, Hosts UI, Universal Profiling, Obs Overview and left Navigation v9.2.8 v9.3.3 v9.4.0

5 participants