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 a7f5831..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 @@ -348,9 +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())); + 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() {