Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import org.opensearch.common.inject.Inject;
import org.opensearch.core.action.ActionListener;
import org.opensearch.core.action.ActionResponse;
import org.opensearch.performanceanalyzer.collectors.telemetry.RTFSearchRequestMetricsCollector;
import org.opensearch.performanceanalyzer.commons.metrics.PerformanceAnalyzerMetrics;
import org.opensearch.performanceanalyzer.commons.util.Util;
import org.opensearch.performanceanalyzer.config.PerformanceAnalyzerController;
Expand All @@ -26,10 +27,12 @@ public class PerformanceAnalyzerActionFilter implements ActionFilter {
private static AtomicLong uniqueID = new AtomicLong(0);

private final PerformanceAnalyzerController controller;
private final RTFSearchRequestMetricsCollector rtfSearchRequestMetricsCollector;

@Inject
public PerformanceAnalyzerActionFilter(final PerformanceAnalyzerController controller) {
this.controller = controller;
this.rtfSearchRequestMetricsCollector = new RTFSearchRequestMetricsCollector(controller);
}

@Override
Expand Down Expand Up @@ -73,11 +76,16 @@ public <Request extends ActionRequest, Response extends ActionResponse> void app
RequestType.search.toString(),
id,
PerformanceAnalyzerMetrics.START_FILE_NAME);
rtfSearchRequestMetricsCollector.onSearchRequest(search);
chain.proceed(task, action, request, newListener);
return;
}
}

if (request instanceof SearchRequest) {
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.

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

rtfSearchRequestMetricsCollector.onSearchRequest((SearchRequest) request);
}

chain.proceed(task, action, request, listener);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

package org.opensearch.performanceanalyzer.collectors.telemetry;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.opensearch.action.search.SearchRequest;
import org.opensearch.index.query.ScriptQueryBuilder;
import org.opensearch.performanceanalyzer.OpenSearchResources;
import org.opensearch.performanceanalyzer.commons.metrics.RTFMetrics;
import org.opensearch.performanceanalyzer.commons.util.Util;
import org.opensearch.performanceanalyzer.config.PerformanceAnalyzerController;
import org.opensearch.search.aggregations.AggregationBuilder;
import org.opensearch.search.aggregations.metrics.ScriptedMetricAggregationBuilder;
import org.opensearch.search.builder.SearchSourceBuilder;
import org.opensearch.search.sort.ScriptSortBuilder;
import org.opensearch.telemetry.metrics.Counter;
import org.opensearch.telemetry.metrics.MetricsRegistry;
import org.opensearch.telemetry.metrics.tags.Tags;

/**
* Collects per-request search metrics via the RTF telemetry pipeline. Currently tracks whether a
* search request contains a script (Painless, script_score, scripted_metric, etc.).
*
* <p>Metric emitted: {@code script_search_count} — incremented only when a script is detected.
*/
public class RTFSearchRequestMetricsCollector {

private static final Logger LOG = LogManager.getLogger(RTFSearchRequestMetricsCollector.class);

public static final String SCRIPT_SEARCH_COUNT = "script_search_count";

private final PerformanceAnalyzerController controller;
private Counter scriptSearchCounter;
private boolean metricsInitialized;

public RTFSearchRequestMetricsCollector(final PerformanceAnalyzerController controller) {
this.controller = controller;
this.metricsInitialized = false;
}

private void initializeMetricsIfNeeded(MetricsRegistry metricsRegistry) {
if (!metricsInitialized && metricsRegistry != null) {
scriptSearchCounter =
metricsRegistry.createCounter(
SCRIPT_SEARCH_COUNT,
"Count of search requests that contain a script",
RTFMetrics.MetricUnits.COUNT.toString());
metricsInitialized = true;
}
}

private boolean isEnabled() {
return controller.isPerformanceAnalyzerEnabled()
&& (controller.getCollectorsRunModeValue() == Util.CollectorMode.DUAL.getValue()
|| controller.getCollectorsRunModeValue()
== Util.CollectorMode.TELEMETRY.getValue());
}

/**
* Called on every incoming SearchRequest. Detects script usage and emits the counter via the
* RTF telemetry pipeline.
*/
public void onSearchRequest(SearchRequest searchRequest) {
if (!isEnabled()) {
return;
}
MetricsRegistry metricsRegistry = OpenSearchResources.INSTANCE.getMetricsRegistry();
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.

Better get this in the constructer itself?

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.

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
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

got it.

initializeMetricsIfNeeded(metricsRegistry);
if (scriptSearchCounter == null) {
LOG.debug("script_search_count counter not yet initialized, skipping");
return;
}
try {
if (detectScript(searchRequest)) {
scriptSearchCounter.add(1, Tags.create());
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.

Tags.empty() pls.

}
} catch (Exception e) {
LOG.debug("Error emitting script_search_count metric", e);
}
}

/**
* Detects common script patterns via direct type checks — no string serialization.
*
* <p>Detects:
* - Top-level script query: {"query": {"script": {...}}}
* - Script fields: {"script_fields": {...}}
* - Script sort: {"sort": {"_script": {...}}}
* - Scripted metric aggregation: {"aggs": {"x": {"scripted_metric": {...}}}}
*
* <p>Does NOT detect:
* - Scripts nested inside bool/dis_max/nested queries
* - Function score with script_score function
* - Bucket script / bucket selector pipeline aggregations
* - Scripts inside sub-aggregations
*/
private boolean detectScript(SearchRequest searchRequest) {
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.

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

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.

Follow naming conventions pls.

if (searchRequest == null || searchRequest.source() == null) {
return false;
}
try {
SearchSourceBuilder source = searchRequest.source();

// Check top-level query type
if (source.query() instanceof ScriptQueryBuilder) {
return true;
}

// Check script fields
if (source.scriptFields() != null && !source.scriptFields().isEmpty()) {
return true;
}

// Check script sort
if (source.sorts() != null) {
for (var sort : source.sorts()) {
if (sort instanceof ScriptSortBuilder) {
return true;
}
}
}

// Check top-level aggregations for scripted_metric
if (source.aggregations() != null) {
for (AggregationBuilder agg : source.aggregations().getAggregatorFactories()) {
if (agg instanceof ScriptedMetricAggregationBuilder) {
return true;
}
}
}

return false;
} catch (Exception e) {
LOG.debug("Error detecting scripts in search request", e);
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.

error pls.

return false;
}
}
}