From 09d4bcfa392f623b32ea1859236689369cd97b39 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" Date: Mon, 18 Nov 2024 22:04:02 +0000 Subject: [PATCH] Usage counter for Top N queries (#153) * Usage counter for Top N queries Signed-off-by: Siddhant Deshmukh * Revert "Usage counter for Top N queries" This reverts commit d1b551d85f4a918d4a827df15856f07ee18d1c10. Signed-off-by: Siddhant Deshmukh * Usage counter for Top N queries using OperationalMetric Signed-off-by: Siddhant Deshmukh * Fix unit tests and spotless apply Signed-off-by: Siddhant Deshmukh --------- Signed-off-by: Siddhant Deshmukh (cherry picked from commit a5d0de7691996e49ceaca2794d9582bd09a0541d) Signed-off-by: github-actions[bot] --- .../insights/core/metrics/OperationalMetric.java | 3 ++- .../insights/core/service/TopQueriesService.java | 11 +++++++++++ .../core/metrics/OperationalMetricsCounterTests.java | 2 +- .../core/service/TopQueriesServiceTests.java | 12 ++++++++++++ 4 files changed, 26 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/opensearch/plugin/insights/core/metrics/OperationalMetric.java b/src/main/java/org/opensearch/plugin/insights/core/metrics/OperationalMetric.java index 9e9b2d7..626c09e 100644 --- a/src/main/java/org/opensearch/plugin/insights/core/metrics/OperationalMetric.java +++ b/src/main/java/org/opensearch/plugin/insights/core/metrics/OperationalMetric.java @@ -18,7 +18,8 @@ public enum OperationalMetric { INVALID_INDEX_PATTERN_EXCEPTIONS("Number of invalid index pattern exceptions"), DATA_INGEST_EXCEPTIONS("Number of exceptions during data ingest in Query Insights"), QUERY_CATEGORIZE_EXCEPTIONS("Number of exceptions when categorizing the queries"), - EXPORTER_FAIL_TO_CLOSE_EXCEPTION("Number of failures when closing the exporter"); + EXPORTER_FAIL_TO_CLOSE_EXCEPTION("Number of failures when closing the exporter"), + TOP_N_QUERIES_USAGE_COUNT("Number of times the top n queries API is used"); private final String description; 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 f601c58..fd90ed5 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,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.tags.Tags; import org.opensearch.threadpool.ThreadPool; /** @@ -60,6 +61,9 @@ * 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 */ @@ -344,6 +348,13 @@ public void validateExporterAndReaderConfig(Settings settings) { */ public List getTopQueriesRecords(final boolean includeLastWindow, final String from, final String to) throws IllegalArgumentException { + OperationalMetricsCounter.getInstance() + .incrementCounter( + OperationalMetric.TOP_N_QUERIES_USAGE_COUNT, + 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/metrics/OperationalMetricsCounterTests.java b/src/test/java/org/opensearch/plugin/insights/core/metrics/OperationalMetricsCounterTests.java index cf4fe80..b3d4ab0 100644 --- a/src/test/java/org/opensearch/plugin/insights/core/metrics/OperationalMetricsCounterTests.java +++ b/src/test/java/org/opensearch/plugin/insights/core/metrics/OperationalMetricsCounterTests.java @@ -35,7 +35,7 @@ public void testSingletonInitializationAndIncrement() { OperationalMetricsCounter.initialize(CLUSTER_NAME, metricsRegistry); OperationalMetricsCounter instance = OperationalMetricsCounter.getInstance(); ArgumentCaptor nameCaptor = ArgumentCaptor.forClass(String.class); - verify(metricsRegistry, times(8)).createCounter(nameCaptor.capture(), any(), eq("1")); + verify(metricsRegistry, times(9)).createCounter(nameCaptor.capture(), any(), eq("1")); assertNotNull(instance); instance.incrementCounter(OperationalMetric.LOCAL_INDEX_READER_PARSING_EXCEPTIONS); instance.incrementCounter(OperationalMetric.LOCAL_INDEX_READER_PARSING_EXCEPTIONS); 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 5ec6f64..8d701b5 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 @@ -8,7 +8,9 @@ package org.opensearch.plugin.insights.core.service; +import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; import java.util.List; import java.util.concurrent.TimeUnit; @@ -17,12 +19,15 @@ import org.opensearch.common.unit.TimeValue; import org.opensearch.plugin.insights.QueryInsightsTestUtils; import org.opensearch.plugin.insights.core.exporter.QueryInsightsExporterFactory; +import org.opensearch.plugin.insights.core.metrics.OperationalMetricsCounter; import org.opensearch.plugin.insights.core.reader.QueryInsightsReaderFactory; import org.opensearch.plugin.insights.rules.model.GroupingType; import org.opensearch.plugin.insights.rules.model.MetricType; 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.MetricsRegistry; import org.opensearch.test.OpenSearchTestCase; import org.opensearch.threadpool.ThreadPool; @@ -34,6 +39,7 @@ 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 MetricsRegistry metricsRegistry; @Before public void setup() { @@ -41,6 +47,12 @@ public void setup() { topQueriesService.setTopNSize(Integer.MAX_VALUE); topQueriesService.setWindowSize(new TimeValue(Long.MAX_VALUE)); topQueriesService.setEnabled(true); + + metricsRegistry = mock(MetricsRegistry.class); + when(metricsRegistry.createCounter(any(String.class), any(String.class), any(String.class))).thenAnswer( + invocation -> mock(Counter.class) + ); + OperationalMetricsCounter.initialize("cluster", metricsRegistry); } public void testIngestQueryDataWithLargeWindow() {