Skip to content

Add metric to count queries with script#923

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

Add metric to count queries with script#923
shivamg2017 wants to merge 1 commit intoopensearch-project:mainfrom
shivamg2017:script-metric

Conversation

@shivamg2017
Copy link

Description

This will emit metric script_search_count when script query is made.

Sample metric

metric: ImmutableMetricData{resource=Resource{schemaUrl=null, attributes={service.name="OpenSearch"}}, instrumentationScopeInfo=InstrumentationScopeInfo{name=org.opensearch.telemetry, version=null, schemaUrl=null, attributes={}}, name=script_search_count, description=Count of search requests tagged by whether they contain a script, unit=count, type=DOUBLE_SUM, data=ImmutableSumData{points=[ImmutableDoublePointData{startEpochNanos=1773264920824685781, epochNanos=1773264980824673234, attributes={}, value=1.0, exemplars=[]}], monotonic=true, aggregationTemporality=DELTA}}

"Count of search requests tagged by whether they contain a script",
RTFMetrics.MetricUnits.COUNT.toString());
metricsInitialized = true;
System.out.println("PA_DEBUG: script_search_count counter initialized");
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why Sysout?

Copy link
Author

Choose a reason for hiding this comment

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

Debug log. Removed

if (!isEnabled()) {
return;
}
MetricsRegistry metricsRegistry = OpenSearchResources.INSTANCE.getMetricsRegistry();
Copy link
Collaborator

Choose a reason for hiding this comment

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

Better get this in the constructer itself?

Copy link
Author

Choose a reason for hiding this comment

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

MetricsRegistry is null at construction time because this collector is instantiated inside getActionFilters(), which OpenSearch calls before createComponents() where the registry is injected. Its similar to https://github.com/opensearch-project/performance-analyzer/blob/main/src/main/java/org/opensearch/performanceanalyzer/ShardMetricsCollector.java#L62

Copy link
Collaborator

Choose a reason for hiding this comment

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

got it.

ActionListener<Response> listener,
ActionFilterChain<Request, Response> chain) {

if (controller.isPerformanceAnalyzerEnabled()
Copy link
Collaborator

Choose a reason for hiding this comment

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

This code only works for RCA and DUAL mode. So your code changes will not run for RTF mode. Better write separate ActionFilter.

Copy link
Author

Choose a reason for hiding this comment

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

Added below for RTF mode as well.

try {
SearchSourceBuilder source = searchRequest.source();
String sourceString = source.toString();
return sourceString.contains("\"script\"")
Copy link
Collaborator

Choose a reason for hiding this comment

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

How heavy is this operation?

Copy link
Author

Choose a reason for hiding this comment

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

Remove string serialization. Now its O(1) operation

}
}

if (request instanceof SearchRequest) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Let's create a separate ActionFilter for RTF and avoid touching this one.

if (!isEnabled()) {
return;
}
MetricsRegistry metricsRegistry = OpenSearchResources.INSTANCE.getMetricsRegistry();
Copy link
Collaborator

Choose a reason for hiding this comment

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

got it.

* - Bucket script / bucket selector pipeline aggregations
* - Scripts inside sub-aggregations
*/
private boolean detectScript(SearchRequest searchRequest) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Just wondering, Can't we publish this metric write creating the source in core?

* - Bucket script / bucket selector pipeline aggregations
* - Scripts inside sub-aggregations
*/
private boolean detectScript(SearchRequest searchRequest) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Follow naming conventions pls.


return false;
} catch (Exception e) {
LOG.debug("Error detecting scripts in search request", e);
Copy link
Collaborator

Choose a reason for hiding this comment

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

error pls.

}
try {
if (detectScript(searchRequest)) {
scriptSearchCounter.add(1, Tags.create());
Copy link
Collaborator

Choose a reason for hiding this comment

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

Tags.empty() pls.

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