Skip to content

Commit

Permalink
Usage counter for Top N queries
Browse files Browse the repository at this point in the history
Signed-off-by: Siddhant Deshmukh <[email protected]>
  • Loading branch information
deshsidd committed Nov 7, 2024
1 parent 5958f54 commit d1b551d
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import org.opensearch.plugin.insights.rules.model.healthStats.QueryInsightsHealthStats;
import org.opensearch.plugin.insights.rules.model.healthStats.TopQueriesHealthStats;
import org.opensearch.plugin.insights.settings.QueryInsightsSettings;
import org.opensearch.telemetry.metrics.Counter;
import org.opensearch.telemetry.metrics.MetricsRegistry;
import org.opensearch.threadpool.Scheduler;
import org.opensearch.threadpool.ThreadPool;
Expand Down Expand Up @@ -122,13 +123,26 @@ public QueryInsightsService(
this.queryInsightsExporterFactory = new QueryInsightsExporterFactory(client);
this.queryInsightsReaderFactory = new QueryInsightsReaderFactory(client);
this.namedXContentRegistry = namedXContentRegistry;

Counter topQueriesApiUsageCounter = metricsRegistry.createCounter(
"search.insights.top_queries.count",
"Counter for the number of API requests for Top N queries",
"number"
);

// initialize top n queries services and configurations consumers
topQueriesServices = new HashMap<>();
for (MetricType metricType : MetricType.allMetricTypes()) {
enableCollect.put(metricType, false);
topQueriesServices.put(
metricType,
new TopQueriesService(metricType, threadPool, queryInsightsExporterFactory, queryInsightsReaderFactory)
new TopQueriesService(
metricType,
threadPool,
queryInsightsExporterFactory,
queryInsightsReaderFactory,
topQueriesApiUsageCounter
)
);
}
for (MetricType type : MetricType.allMetricTypes()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,17 @@
import org.opensearch.plugin.insights.rules.model.SearchQueryRecord;
import org.opensearch.plugin.insights.rules.model.healthStats.TopQueriesHealthStats;
import org.opensearch.plugin.insights.settings.QueryInsightsSettings;
import org.opensearch.telemetry.metrics.Counter;
import org.opensearch.telemetry.metrics.tags.Tags;
import org.opensearch.threadpool.ThreadPool;

/**
* Service responsible for gathering and storing top N queries
* with high latency or resource usage
*/
public class TopQueriesService {
private static final String METRIC_TYPE_TAG = "metric_type";
private static final String GROUPBY_TAG = "groupby";
/**
* Logger of the local index exporter
*/
Expand Down Expand Up @@ -116,11 +120,14 @@ public class TopQueriesService {

private QueryGrouper queryGrouper;

private final Counter topQueriesApiUsageCounter;

TopQueriesService(
final MetricType metricType,
final ThreadPool threadPool,
final QueryInsightsExporterFactory queryInsightsExporterFactory,
QueryInsightsReaderFactory queryInsightsReaderFactory
QueryInsightsReaderFactory queryInsightsReaderFactory,
Counter topQueriesApiUsageCounter
) {
this.enabled = false;
this.metricType = metricType;
Expand All @@ -142,6 +149,7 @@ public class TopQueriesService {
topQueriesStore,
topNSize
);
this.topQueriesApiUsageCounter = topQueriesApiUsageCounter;
}

/**
Expand Down Expand Up @@ -344,6 +352,10 @@ public void validateExporterAndReaderConfig(Settings settings) {
*/
public List<SearchQueryRecord> getTopQueriesRecords(final boolean includeLastWindow, final String from, final String to)
throws IllegalArgumentException {
this.topQueriesApiUsageCounter.add(
1,
(Tags.create().addTag(METRIC_TYPE_TAG, this.metricType.name()).addTag(GROUPBY_TAG, this.queryGrouper.getGroupingType().name()))
);
if (!enabled) {
throw new IllegalArgumentException(
String.format(Locale.ROOT, "Cannot get top n queries for [%s] when it is not enabled.", metricType.toString())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.opensearch.plugin.insights.rules.model.SearchQueryRecord;
import org.opensearch.plugin.insights.rules.model.healthStats.TopQueriesHealthStats;
import org.opensearch.plugin.insights.settings.QueryInsightsSettings;
import org.opensearch.telemetry.metrics.Counter;
import org.opensearch.test.OpenSearchTestCase;
import org.opensearch.threadpool.ThreadPool;

Expand All @@ -34,10 +35,17 @@ public class TopQueriesServiceTests extends OpenSearchTestCase {
private final ThreadPool threadPool = mock(ThreadPool.class);
private final QueryInsightsExporterFactory queryInsightsExporterFactory = mock(QueryInsightsExporterFactory.class);
private final QueryInsightsReaderFactory queryInsightsReaderFactory = mock(QueryInsightsReaderFactory.class);
private final Counter topQueriesApiUsageCounter = mock(Counter.class);

@Before
public void setup() {
topQueriesService = new TopQueriesService(MetricType.LATENCY, threadPool, queryInsightsExporterFactory, queryInsightsReaderFactory);
topQueriesService = new TopQueriesService(
MetricType.LATENCY,
threadPool,
queryInsightsExporterFactory,
queryInsightsReaderFactory,
topQueriesApiUsageCounter
);
topQueriesService.setTopNSize(Integer.MAX_VALUE);
topQueriesService.setWindowSize(new TimeValue(Long.MAX_VALUE));
topQueriesService.setEnabled(true);
Expand Down

0 comments on commit d1b551d

Please sign in to comment.