Add metric to track queries with script#934
Open
shivamg2017 wants to merge 1 commit intoopensearch-project:mainfrom
Open
Add metric to track queries with script#934shivamg2017 wants to merge 1 commit intoopensearch-project:mainfrom
shivamg2017 wants to merge 1 commit intoopensearch-project:mainfrom
Conversation
Gaganjuneja
reviewed
Apr 2, 2026
| // Walk the query tree for script queries | ||
| SearchSourceBuilder source = searchContext.request().source(); | ||
| if (source != null && source.query() != null) { | ||
| ScriptQueryVisitor visitor = new ScriptQueryVisitor(); |
Collaborator
There was a problem hiding this comment.
What does SearchContext::hasScriptFields method returns?
Author
There was a problem hiding this comment.
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
Gaganjuneja
reviewed
Apr 10, 2026
|
|
||
| @Override | ||
| public void accept(QueryBuilder qb) { | ||
| if (qb instanceof ScriptQueryBuilder || qb instanceof ScriptScoreQueryBuilder) { |
Collaborator
There was a problem hiding this comment.
Can we short circuit if containsScript == true?
c5f8a79 to
963c037
Compare
Collaborator
|
LGTM. Please fix DCO. |
Signed-off-by: Shivam Gupta <gshhivam@amazon.com>
963c037 to
182eac2
Compare
Gaganjuneja
approved these changes
Apr 17, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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}}