diff --git a/src/main/java/org/opensearch/plugin/insights/core/service/QueryInsightsService.java b/src/main/java/org/opensearch/plugin/insights/core/service/QueryInsightsService.java index 150f5e3a..0c0ac411 100644 --- a/src/main/java/org/opensearch/plugin/insights/core/service/QueryInsightsService.java +++ b/src/main/java/org/opensearch/plugin/insights/core/service/QueryInsightsService.java @@ -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; @@ -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()) { diff --git a/src/main/java/org/opensearch/plugin/insights/core/service/TopQueriesService.java b/src/main/java/org/opensearch/plugin/insights/core/service/TopQueriesService.java index f601c58d..57b9a55e 100644 --- a/src/main/java/org/opensearch/plugin/insights/core/service/TopQueriesService.java +++ b/src/main/java/org/opensearch/plugin/insights/core/service/TopQueriesService.java @@ -53,6 +53,8 @@ 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; /** @@ -60,6 +62,8 @@ * 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 */ @@ -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; @@ -142,6 +149,7 @@ public class TopQueriesService { topQueriesStore, topNSize ); + this.topQueriesApiUsageCounter = topQueriesApiUsageCounter; } /** @@ -344,6 +352,10 @@ public void validateExporterAndReaderConfig(Settings settings) { */ public List 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()) diff --git a/src/test/java/org/opensearch/plugin/insights/core/service/TopQueriesServiceTests.java b/src/test/java/org/opensearch/plugin/insights/core/service/TopQueriesServiceTests.java index 5ec6f643..b86d2423 100644 --- a/src/test/java/org/opensearch/plugin/insights/core/service/TopQueriesServiceTests.java +++ b/src/test/java/org/opensearch/plugin/insights/core/service/TopQueriesServiceTests.java @@ -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; @@ -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);