Skip to content

Add metric to track queries with script#934

Open
shivamg2017 wants to merge 1 commit intoopensearch-project:mainfrom
shivamg2017:script-query-metric
Open

Add metric to track queries with script#934
shivamg2017 wants to merge 1 commit intoopensearch-project:mainfrom
shivamg2017:script-query-metric

Conversation

@shivamg2017
Copy link
Copy Markdown

Description

Adds a new RTF counter shard_query_with_script_count in RTFPerformanceAnalyzerSearchListener that tracks whether a search query contains a script.

Uses QueryBuilderVisitor to recursively walk the query tree and detect ScriptQueryBuilder / ScriptScoreQueryBuilder at any nesting depth (including inside bool, dis_max, etc.). Also checks hasScriptFields() for script_fields usage.

Emitted per shard in queryPhase(), tagged with index_name, index_uuid, shard_id. Gated behind DUAL/TELEMETRY collector mode.

Sample metric
[2026-04-02T09:20:45,372][WARN ][stderr ] [dev-dsk-gshhivam-1a-f8b252b1.eu-west-1.amazon.com] INFO: metric: ImmutableMetricData{resource=Resource{schemaUrl=null, attributes={service.name="OpenSearch"}}, instrumentationScopeInfo=InstrumentationScopeInfo{name=org.opensearch.telemetry, version=null, schemaUrl=null, attributes={}}, name=shard_query_with_script_count, description=Count of shard query phases where a script was used, unit=count, type=DOUBLE_SUM, data=ImmutableSumData{points=[ImmutableDoublePointData{startEpochNanos=1775121585366924999, epochNanos=1775121645366956216, attributes={index_name="test-index", index_uuid="LlP0tKOESyi5Bq7IVi0sDw", shard_id=0}, value=1.0, exemplars=[]}], monotonic=true, aggregationTemporality=DELTA}}

// Walk the query tree for script queries
SearchSourceBuilder source = searchContext.request().source();
if (source != null && source.query() != null) {
ScriptQueryVisitor visitor = new ScriptQueryVisitor();
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

What does SearchContext::hasScriptFields method returns?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Returns true when the request has script_fields — scripts that compute new fields in the response (e.g. "script_fields": {"doubled": {"script": ...}}). These are separate from scripts in the query DSL, so we check both: hasScriptFields() for script_fields, and the visitor for script queries inside the query tree


@Override
public void accept(QueryBuilder qb) {
if (qb instanceof ScriptQueryBuilder || qb instanceof ScriptScoreQueryBuilder) {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Can we short circuit if containsScript == true?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Done

@shivamg2017 shivamg2017 force-pushed the script-query-metric branch from c5f8a79 to 963c037 Compare April 14, 2026 12:38
@Gaganjuneja
Copy link
Copy Markdown
Collaborator

LGTM. Please fix DCO.

Signed-off-by: Shivam Gupta <gshhivam@amazon.com>
@shivamg2017 shivamg2017 force-pushed the script-query-metric branch from 963c037 to 182eac2 Compare April 16, 2026 09:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants