Skip to content

Commit

Permalink
Usage counter for Top N queries (#153)
Browse files Browse the repository at this point in the history
* Usage counter for Top N queries

Signed-off-by: Siddhant Deshmukh <[email protected]>

* Revert "Usage counter for Top N queries"

This reverts commit d1b551d.

Signed-off-by: Siddhant Deshmukh <[email protected]>

* Usage counter for Top N queries using OperationalMetric

Signed-off-by: Siddhant Deshmukh <[email protected]>

* Fix unit tests and spotless apply

Signed-off-by: Siddhant Deshmukh <[email protected]>

---------

Signed-off-by: Siddhant Deshmukh <[email protected]>
(cherry picked from commit a5d0de7)
Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
  • Loading branch information
github-actions[bot] committed Nov 18, 2024
1 parent 93405a9 commit 09d4bcf
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

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.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 @@ -344,6 +348,13 @@ public void validateExporterAndReaderConfig(Settings settings) {
*/
public List<SearchQueryRecord> 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())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public void testSingletonInitializationAndIncrement() {
OperationalMetricsCounter.initialize(CLUSTER_NAME, metricsRegistry);
OperationalMetricsCounter instance = OperationalMetricsCounter.getInstance();
ArgumentCaptor<String> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand All @@ -34,13 +39,20 @@ 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() {
topQueriesService = new TopQueriesService(MetricType.LATENCY, threadPool, queryInsightsExporterFactory, queryInsightsReaderFactory);
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() {
Expand Down

0 comments on commit 09d4bcf

Please sign in to comment.