Skip to content

Commit a805ae8

Browse files
Query instrumentation Minor Enhancements (#73) (#79)
* Query instrumentation minor fixes * Fix unit tests * Rename package * Spotless --------- (cherry picked from commit c6a46f5) Signed-off-by: Siddhant Deshmukh <deshsid@amazon.com> Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com> Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
1 parent 7a878b5 commit a805ae8

File tree

5 files changed

+17
-15
lines changed

5 files changed

+17
-15
lines changed

src/main/java/org/opensearch/plugin/insights/core/service/categorizer/SearchQueryAggregationCategorizer.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
*/
2121
public class SearchQueryAggregationCategorizer {
2222

23-
private static final String TYPE_TAG = "type";
23+
static final String AGGREGATION_TYPE_TAG = "agg_type";
2424
private final SearchQueryCounters searchQueryCounters;
2525

2626
/**
@@ -49,7 +49,7 @@ public void incrementSearchQueryAggregationCounters(
4949
private void incrementCountersRecursively(AggregationBuilder aggregationBuilder, Map<MetricType, Number> measurements) {
5050
// Increment counters for the current aggregation
5151
String aggregationType = aggregationBuilder.getType();
52-
searchQueryCounters.incrementAggCounter(1, Tags.create().addTag(TYPE_TAG, aggregationType), measurements);
52+
searchQueryCounters.incrementAggCounter(1, Tags.create().addTag(AGGREGATION_TYPE_TAG, aggregationType), measurements);
5353

5454
// Recursively process sub-aggregations if any
5555
Collection<AggregationBuilder> subAggregations = aggregationBuilder.getSubAggregations();
@@ -63,7 +63,7 @@ private void incrementCountersRecursively(AggregationBuilder aggregationBuilder,
6363
Collection<PipelineAggregationBuilder> pipelineAggregations = aggregationBuilder.getPipelineAggregations();
6464
for (PipelineAggregationBuilder pipelineAggregation : pipelineAggregations) {
6565
String pipelineAggregationType = pipelineAggregation.getType();
66-
searchQueryCounters.incrementAggCounter(1, Tags.create().addTag(TYPE_TAG, pipelineAggregationType), measurements);
66+
searchQueryCounters.incrementAggCounter(1, Tags.create().addTag(AGGREGATION_TYPE_TAG, pipelineAggregationType), measurements);
6767
}
6868
}
6969
}

src/main/java/org/opensearch/plugin/insights/core/service/categorizer/SearchQueryCounters.java

+9-5
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,12 @@
2323
*/
2424
public final class SearchQueryCounters {
2525
private static final String LEVEL_TAG = "level";
26-
private static final String TYPE_TAG = "type";
26+
private static final String QUERY_TYPE_TAG = "type";
2727
private static final String UNIT = "1";
28+
private static final String UNIT_MILLIS = "ms";
29+
private static final String UNIT_CPU_CYCLES = "ns";
30+
private static final String UNIT_BYTES = "bytes";
31+
2832
private final MetricsRegistry metricsRegistry;
2933
/**
3034
* Aggregation counter
@@ -83,17 +87,17 @@ public SearchQueryCounters(MetricsRegistry metricsRegistry) {
8387
this.queryTypeLatencyHistogram = metricsRegistry.createHistogram(
8488
"search.query.type.latency.histogram",
8589
"Histogram for the latency per query type",
86-
UNIT
90+
UNIT_MILLIS
8791
);
8892
this.queryTypeCpuHistogram = metricsRegistry.createHistogram(
8993
"search.query.type.cpu.histogram",
9094
"Histogram for the cpu per query type",
91-
UNIT
95+
UNIT_CPU_CYCLES
9296
);
9397
this.queryTypeMemoryHistogram = metricsRegistry.createHistogram(
9498
"search.query.type.memory.histogram",
9599
"Histogram for the memory per query type",
96-
UNIT
100+
UNIT_BYTES
97101
);
98102
this.queryHandlers = new HashMap<>();
99103
}
@@ -109,7 +113,7 @@ public void incrementCounter(QueryBuilder queryBuilder, int level, Map<MetricTyp
109113

110114
Counter counter = nameToQueryTypeCounters.computeIfAbsent(uniqueQueryCounterName, k -> createQueryCounter(k));
111115
counter.add(1, Tags.create().addTag(LEVEL_TAG, level));
112-
incrementAllHistograms(Tags.create().addTag(LEVEL_TAG, level).addTag(TYPE_TAG, uniqueQueryCounterName), measurements);
116+
incrementAllHistograms(Tags.create().addTag(LEVEL_TAG, level).addTag(QUERY_TYPE_TAG, uniqueQueryCounterName), measurements);
113117
}
114118

115119
/**

src/test/java/org/opensearch/plugin/insights/core/service/categorizor/QueryShapeGeneratorTests.java src/test/java/org/opensearch/plugin/insights/core/service/categorizer/QueryShapeGeneratorTests.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,10 @@
66
* compatible open source license.
77
*/
88

9-
package org.opensearch.plugin.insights.core.service.categorizor;
9+
package org.opensearch.plugin.insights.core.service.categorizer;
1010

1111
import org.opensearch.common.hash.MurmurHash3;
1212
import org.opensearch.plugin.insights.SearchSourceBuilderUtils;
13-
import org.opensearch.plugin.insights.core.service.categorizer.QueryShapeGenerator;
1413
import org.opensearch.search.builder.SearchSourceBuilder;
1514
import org.opensearch.test.OpenSearchTestCase;
1615

src/test/java/org/opensearch/plugin/insights/core/service/categorizor/QueryShapeVisitorTests.java src/test/java/org/opensearch/plugin/insights/core/service/categorizer/QueryShapeVisitorTests.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* compatible open source license.
77
*/
88

9-
package org.opensearch.plugin.insights.core.service.categorizor;
9+
package org.opensearch.plugin.insights.core.service.categorizer;
1010

1111
import org.opensearch.index.query.BoolQueryBuilder;
1212
import org.opensearch.index.query.ConstantScoreQueryBuilder;
@@ -16,7 +16,6 @@
1616
import org.opensearch.index.query.RegexpQueryBuilder;
1717
import org.opensearch.index.query.TermQueryBuilder;
1818
import org.opensearch.index.query.TermsQueryBuilder;
19-
import org.opensearch.plugin.insights.core.service.categorizer.QueryShapeVisitor;
2019
import org.opensearch.test.OpenSearchTestCase;
2120

2221
public final class QueryShapeVisitorTests extends OpenSearchTestCase {

src/test/java/org/opensearch/plugin/insights/core/service/categorizor/SearchQueryCategorizerTests.java src/test/java/org/opensearch/plugin/insights/core/service/categorizer/SearchQueryCategorizerTests.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* compatible open source license.
77
*/
88

9-
package org.opensearch.plugin.insights.core.service.categorizor;
9+
package org.opensearch.plugin.insights.core.service.categorizer;
1010

1111
import static org.mockito.ArgumentMatchers.any;
1212
import static org.mockito.ArgumentMatchers.eq;
@@ -15,6 +15,7 @@
1515
import static org.mockito.Mockito.verify;
1616
import static org.mockito.Mockito.when;
1717
import static org.opensearch.plugin.insights.QueryInsightsTestUtils.generateQueryInsightRecords;
18+
import static org.opensearch.plugin.insights.core.service.categorizer.SearchQueryAggregationCategorizer.AGGREGATION_TYPE_TAG;
1819

1920
import java.util.Arrays;
2021
import java.util.HashMap;
@@ -36,7 +37,6 @@
3637
import org.opensearch.index.query.TermQueryBuilder;
3738
import org.opensearch.index.query.WildcardQueryBuilder;
3839
import org.opensearch.index.query.functionscore.FunctionScoreQueryBuilder;
39-
import org.opensearch.plugin.insights.core.service.categorizer.SearchQueryCategorizer;
4040
import org.opensearch.plugin.insights.rules.model.MetricType;
4141
import org.opensearch.plugin.insights.rules.model.SearchQueryRecord;
4242
import org.opensearch.search.aggregations.bucket.range.RangeAggregationBuilder;
@@ -114,7 +114,7 @@ public void testAggregationsQuery() {
114114
verify(searchQueryCategorizer.getSearchQueryCounters().getAggCounter()).add(valueCaptor.capture(), tagsCaptor.capture());
115115

116116
double actualValue = valueCaptor.getValue();
117-
String actualTag = (String) tagsCaptor.getValue().getTagsMap().get("type");
117+
String actualTag = (String) tagsCaptor.getValue().getTagsMap().get(AGGREGATION_TYPE_TAG);
118118

119119
assertEquals(1.0d, actualValue, 0.0001);
120120
assertEquals(MULTI_TERMS_AGGREGATION, actualTag);

0 commit comments

Comments
 (0)