Skip to content

Conversation

javanna
Copy link
Member

@javanna javanna commented Sep 16, 2025

We already record latency of the query and fetch phase as APM metrics. We'd like to be able to slice such latencies based on some recurring categories of the request:

  • does it have agg or hit only?
  • is it sorted by field or by score?
  • does it have a time range filter?
  • does it target user data or internal indices?

This commit introduces introspection for a shard search request and stores the extracted attributes together with the shard phase latency metrics.

This builds on top of #134232 to use the same infra and store the same attributes for shard level latency metrics.

Here are some of the differences of the attributes extracted at the shard level compared to those extracted at the coord level, for the same query:

  • can match may filter shards out, hence shards that are filtered out won't have a corresponding entry for query phase
  • query rewrite may rewrite time range queries to match all, in which case shards won't see the time range filter.
  • shard level requests always have a single, concrete index
  • search scroll on the coord node does not see the query itself, hence does not report on the query introspection. Search scroll at the shard level does see the search request and can introspect it and add attributes accordingly, on each shard.

We already record latency of the query and fetch phase as APM metrics.
We'd like to be able to slice such latencies based on some recurring
categories of the request:

- does it have agg or hit only?
- is it sorted by field or by score?
- does it have a time range filter?
- does it target user data or internal indices?

This commit introduces introspection for a shard search request and stores the
extracted attributes together with the shard phase latency metrics.

This builds on top of elastic#134232 to use the same infra and store the same attributes
for shard level latency metrics.
@elasticsearchmachine
Copy link
Collaborator

Pinging @elastic/es-search-foundations (Team:Search Foundations)

@elasticsearchmachine elasticsearchmachine added the Team:Search Foundations Meta label for the Search Foundations team in Elasticsearch label Sep 16, 2025
@elasticsearchmachine
Copy link
Collaborator

Hi @javanna, I've created a changelog YAML for you.

shardSearchRequest.shardId().getIndexName()
);
boolean isSystem = ((EsExecutors.EsThread) Thread.currentThread()).isSystem();
attributes.put(SYSTEM_THREAD_ATTRIBUTE_NAME, isSystem);
Copy link
Member Author

Choose a reason for hiding this comment

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

The isSystem attribute I moved here from the ShardSearchPhaseAPMMetrics class.

// don't track unbounded ranges, they translate to either match_none if the field does not exist
// or match_all if the field is mapped
case TIMESTAMP -> {
if (range.to() != null || range.from() != null) {
Copy link
Member Author

Choose a reason for hiding this comment

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

This is something I discovered as part of working on this chance: rewrite for range queries is a bit funky (I plan on fixing that as a follow-up). A range query may rewrite to one without bounds, if all data is within the range. In that case, it is equivalent to a match_all hence I don't report it as a range. I decided to rather report only "true" range queries at the shard level.

Copy link
Contributor

@tteofili tteofili left a comment

Choose a reason for hiding this comment

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

this LGTM, also very nice to have got rid of yet another ThreadLocal :-)

Copy link
Contributor

@andreidan andreidan left a comment

Choose a reason for hiding this comment

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

LGTM (once CI is happy :) ), thanks Luca

@javanna javanna merged commit 5e7f305 into elastic:main Sep 23, 2025
34 checks passed
@javanna javanna deleted the enhancement/search_shard_latency_attributes branch September 23, 2025 09:20
javanna added a commit to javanna/elasticsearch that referenced this pull request Sep 26, 2025
We recently added relevant attributes to the existing shard search latency metrics (elastic#134798).
This commit introduces an additional attribute that analyzes the parsed time range filter against the @timestamp field and
reports back whether it is within the last 15 minutes, last hour, last 12 hours, last day, last three days, last seven days,
or last 14 days.
javanna added a commit that referenced this pull request Sep 26, 2025
#135524)

We recently added relevant attributes to the existing shard search latency metrics (#134798).
This commit introduces an additional attribute that analyzes the parsed time range filter against the @timestamp field and
reports back whether it is within the last 15 minutes, last hour, last 12 hours, last day, last three days, last seven days,
or last 14 days.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
>enhancement :Search Foundations/Search Catch all for Search Foundations Team:Search Foundations Meta label for the Search Foundations team in Elasticsearch v9.2.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants