Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Usage counter for Top N queries #153

Merged
merged 4 commits into from
Nov 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading