Skip to content

Commit 1b8cba3

Browse files
authored
Make search query counters dynamic to support all query types (#12601) (#13397)
Signed-off-by: Siddhant Deshmukh <deshsid@amazon.com>
1 parent e87bb18 commit 1b8cba3

File tree

3 files changed

+35
-255
lines changed

3 files changed

+35
-255
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
2424
- Add an individual setting of rate limiter for segment replication ([#12959](https://github.com/opensearch-project/OpenSearch/pull/12959))
2525
- [Streaming Indexing] Ensure support of the new transport by security plugin ([#13174](https://github.com/opensearch-project/OpenSearch/pull/13174))
2626
- Add cluster setting to dynamically configure the buckets for filter rewrite optimization. ([#13179](https://github.com/opensearch-project/OpenSearch/pull/13179))
27+
- Make search query counters dynamic to support all query types ([#12601](https://github.com/opensearch-project/OpenSearch/pull/12601))
2728

2829
### Dependencies
2930
- Bump `org.apache.commons:commons-configuration2` from 2.10.0 to 2.10.1 ([#12896](https://github.com/opensearch-project/OpenSearch/pull/12896))

server/src/main/java/org/opensearch/action/search/SearchQueryCounters.java

+15-237
Original file line numberDiff line numberDiff line change
@@ -8,39 +8,14 @@
88

99
package org.opensearch.action.search;
1010

11-
import org.opensearch.index.query.BoolQueryBuilder;
12-
import org.opensearch.index.query.BoostingQueryBuilder;
13-
import org.opensearch.index.query.ConstantScoreQueryBuilder;
14-
import org.opensearch.index.query.DisMaxQueryBuilder;
15-
import org.opensearch.index.query.DistanceFeatureQueryBuilder;
16-
import org.opensearch.index.query.ExistsQueryBuilder;
17-
import org.opensearch.index.query.FieldMaskingSpanQueryBuilder;
18-
import org.opensearch.index.query.FuzzyQueryBuilder;
19-
import org.opensearch.index.query.GeoBoundingBoxQueryBuilder;
20-
import org.opensearch.index.query.GeoDistanceQueryBuilder;
21-
import org.opensearch.index.query.GeoPolygonQueryBuilder;
22-
import org.opensearch.index.query.GeoShapeQueryBuilder;
23-
import org.opensearch.index.query.IntervalQueryBuilder;
24-
import org.opensearch.index.query.MatchAllQueryBuilder;
25-
import org.opensearch.index.query.MatchPhraseQueryBuilder;
26-
import org.opensearch.index.query.MatchQueryBuilder;
27-
import org.opensearch.index.query.MultiMatchQueryBuilder;
28-
import org.opensearch.index.query.PrefixQueryBuilder;
2911
import org.opensearch.index.query.QueryBuilder;
30-
import org.opensearch.index.query.QueryStringQueryBuilder;
31-
import org.opensearch.index.query.RangeQueryBuilder;
32-
import org.opensearch.index.query.RegexpQueryBuilder;
33-
import org.opensearch.index.query.ScriptQueryBuilder;
34-
import org.opensearch.index.query.SimpleQueryStringBuilder;
35-
import org.opensearch.index.query.TermQueryBuilder;
36-
import org.opensearch.index.query.WildcardQueryBuilder;
37-
import org.opensearch.index.query.functionscore.FunctionScoreQueryBuilder;
3812
import org.opensearch.telemetry.metrics.Counter;
3913
import org.opensearch.telemetry.metrics.MetricsRegistry;
4014
import org.opensearch.telemetry.metrics.tags.Tags;
4115

4216
import java.util.HashMap;
4317
import java.util.Map;
18+
import java.util.concurrent.ConcurrentHashMap;
4419

4520
/**
4621
* Class contains all the Counters related to search query types.
@@ -49,244 +24,47 @@ final class SearchQueryCounters {
4924
private static final String LEVEL_TAG = "level";
5025
private static final String UNIT = "1";
5126
private final MetricsRegistry metricsRegistry;
52-
53-
// Counters related to Query types
5427
public final Counter aggCounter;
55-
public final Counter boolCounter;
56-
public final Counter boostingCounter;
57-
public final Counter constantScoreCounter;
58-
public final Counter disMaxCounter;
59-
public final Counter distanceFeatureCounter;
60-
public final Counter existsCounter;
61-
public final Counter fieldMaskingSpanCounter;
62-
public final Counter functionScoreCounter;
63-
public final Counter fuzzyCounter;
64-
public final Counter geoBoundingBoxCounter;
65-
public final Counter geoDistanceCounter;
66-
public final Counter geoPolygonCounter;
67-
public final Counter geoShapeCounter;
68-
public final Counter intervalCounter;
69-
public final Counter matchCounter;
70-
public final Counter matchallCounter;
71-
public final Counter matchPhrasePrefixCounter;
72-
public final Counter multiMatchCounter;
7328
public final Counter otherQueryCounter;
74-
public final Counter prefixCounter;
75-
public final Counter queryStringCounter;
76-
public final Counter rangeCounter;
77-
public final Counter regexpCounter;
78-
public final Counter scriptCounter;
79-
public final Counter simpleQueryStringCounter;
8029
public final Counter sortCounter;
81-
public final Counter skippedCounter;
82-
public final Counter termCounter;
83-
public final Counter totalCounter;
84-
public final Counter wildcardCounter;
85-
public final Counter numberOfInputFieldsCounter;
8630
private final Map<Class<? extends QueryBuilder>, Counter> queryHandlers;
31+
public final ConcurrentHashMap<String, Counter> nameToQueryTypeCounters;
8732

8833
public SearchQueryCounters(MetricsRegistry metricsRegistry) {
8934
this.metricsRegistry = metricsRegistry;
35+
this.nameToQueryTypeCounters = new ConcurrentHashMap<>();
9036
this.aggCounter = metricsRegistry.createCounter(
9137
"search.query.type.agg.count",
9238
"Counter for the number of top level agg search queries",
9339
UNIT
9440
);
95-
this.boolCounter = metricsRegistry.createCounter(
96-
"search.query.type.bool.count",
97-
"Counter for the number of top level and nested bool search queries",
98-
UNIT
99-
);
100-
this.boostingCounter = metricsRegistry.createCounter(
101-
"search.query.type.boost.count",
102-
"Counter for the number of top level and nested boost search queries",
103-
UNIT
104-
);
105-
this.constantScoreCounter = metricsRegistry.createCounter(
106-
"search.query.type.counstantscore.count",
107-
"Counter for the number of top level and nested constant score search queries",
108-
UNIT
109-
);
110-
this.disMaxCounter = metricsRegistry.createCounter(
111-
"search.query.type.dismax.count",
112-
"Counter for the number of top level and nested disjuntion max search queries",
113-
UNIT
114-
);
115-
this.distanceFeatureCounter = metricsRegistry.createCounter(
116-
"search.query.type.distancefeature.count",
117-
"Counter for the number of top level and nested distance feature search queries",
118-
UNIT
119-
);
120-
this.existsCounter = metricsRegistry.createCounter(
121-
"search.query.type.exists.count",
122-
"Counter for the number of top level and nested exists search queries",
123-
UNIT
124-
);
125-
this.fieldMaskingSpanCounter = metricsRegistry.createCounter(
126-
"search.query.type.fieldmaskingspan.count",
127-
"Counter for the number of top level and nested field masking span search queries",
128-
UNIT
129-
);
130-
this.functionScoreCounter = metricsRegistry.createCounter(
131-
"search.query.type.functionscore.count",
132-
"Counter for the number of top level and nested function score search queries",
133-
UNIT
134-
);
135-
this.fuzzyCounter = metricsRegistry.createCounter(
136-
"search.query.type.fuzzy.count",
137-
"Counter for the number of top level and nested fuzzy search queries",
138-
UNIT
139-
);
140-
this.geoBoundingBoxCounter = metricsRegistry.createCounter(
141-
"search.query.type.geoboundingbox.count",
142-
"Counter for the number of top level and nested geo bounding box queries",
143-
UNIT
144-
);
145-
this.geoDistanceCounter = metricsRegistry.createCounter(
146-
"search.query.type.geodistance.count",
147-
"Counter for the number of top level and nested geo distance queries",
148-
UNIT
149-
);
150-
this.geoPolygonCounter = metricsRegistry.createCounter(
151-
"search.query.type.geopolygon.count",
152-
"Counter for the number of top level and nested geo polygon queries",
153-
UNIT
154-
);
155-
this.geoShapeCounter = metricsRegistry.createCounter(
156-
"search.query.type.geoshape.count",
157-
"Counter for the number of top level and nested geo shape queries",
158-
UNIT
159-
);
160-
this.intervalCounter = metricsRegistry.createCounter(
161-
"search.query.type.interval.count",
162-
"Counter for the number of top level and nested interval queries",
163-
UNIT
164-
);
165-
this.matchCounter = metricsRegistry.createCounter(
166-
"search.query.type.match.count",
167-
"Counter for the number of top level and nested match search queries",
168-
UNIT
169-
);
170-
this.matchallCounter = metricsRegistry.createCounter(
171-
"search.query.type.matchall.count",
172-
"Counter for the number of top level and nested match all search queries",
173-
UNIT
174-
);
175-
this.matchPhrasePrefixCounter = metricsRegistry.createCounter(
176-
"search.query.type.matchphrase.count",
177-
"Counter for the number of top level and nested match phrase prefix search queries",
178-
UNIT
179-
);
180-
this.multiMatchCounter = metricsRegistry.createCounter(
181-
"search.query.type.multimatch.count",
182-
"Counter for the number of top level and nested multi match search queries",
183-
UNIT
184-
);
18541
this.otherQueryCounter = metricsRegistry.createCounter(
18642
"search.query.type.other.count",
18743
"Counter for the number of top level and nested search queries that do not match any other categories",
18844
UNIT
18945
);
190-
this.prefixCounter = metricsRegistry.createCounter(
191-
"search.query.type.prefix.count",
192-
"Counter for the number of top level and nested search queries that match prefix queries",
193-
UNIT
194-
);
195-
this.queryStringCounter = metricsRegistry.createCounter(
196-
"search.query.type.querystringquery.count",
197-
"Counter for the number of top level and nested queryStringQuery search queries",
198-
UNIT
199-
);
200-
this.rangeCounter = metricsRegistry.createCounter(
201-
"search.query.type.range.count",
202-
"Counter for the number of top level and nested range search queries",
203-
UNIT
204-
);
205-
this.regexpCounter = metricsRegistry.createCounter(
206-
"search.query.type.regex.count",
207-
"Counter for the number of top level and nested regex search queries",
208-
UNIT
209-
);
210-
this.scriptCounter = metricsRegistry.createCounter(
211-
"search.query.type.script.count",
212-
"Counter for the number of top level and nested script search queries",
213-
UNIT
214-
);
215-
this.simpleQueryStringCounter = metricsRegistry.createCounter(
216-
"search.query.type.simplequerystring.count",
217-
"Counter for the number of top level and nested script simple query string search queries",
218-
UNIT
219-
);
220-
this.skippedCounter = metricsRegistry.createCounter(
221-
"search.query.type.skipped.count",
222-
"Counter for the number queries skipped due to error",
223-
UNIT
224-
);
22546
this.sortCounter = metricsRegistry.createCounter(
22647
"search.query.type.sort.count",
22748
"Counter for the number of top level sort search queries",
22849
UNIT
22950
);
230-
this.termCounter = metricsRegistry.createCounter(
231-
"search.query.type.term.count",
232-
"Counter for the number of top level and nested term search queries",
233-
UNIT
234-
);
235-
this.totalCounter = metricsRegistry.createCounter(
236-
"search.query.type.total.count",
237-
"Counter for the number of top level and nested search queries",
238-
UNIT
239-
);
240-
this.wildcardCounter = metricsRegistry.createCounter(
241-
"search.query.type.wildcard.count",
242-
"Counter for the number of top level and nested wildcard search queries",
243-
UNIT
244-
);
245-
this.numberOfInputFieldsCounter = metricsRegistry.createCounter(
246-
"search.query.type.numberofinputfields.count",
247-
"Counter for the number of input fields in the search queries",
248-
UNIT
249-
);
25051
this.queryHandlers = new HashMap<>();
251-
initializeQueryHandlers();
52+
25253
}
25354

25455
public void incrementCounter(QueryBuilder queryBuilder, int level) {
255-
Counter counter = queryHandlers.get(queryBuilder.getClass());
256-
if (counter != null) {
257-
counter.add(1, Tags.create().addTag(LEVEL_TAG, level));
258-
} else {
259-
otherQueryCounter.add(1, Tags.create().addTag(LEVEL_TAG, level));
260-
}
261-
}
56+
String uniqueQueryCounterName = queryBuilder.getName();
26257

263-
private void initializeQueryHandlers() {
58+
Counter counter = nameToQueryTypeCounters.computeIfAbsent(uniqueQueryCounterName, k -> createQueryCounter(k));
59+
counter.add(1, Tags.create().addTag(LEVEL_TAG, level));
60+
}
26461

265-
queryHandlers.put(BoolQueryBuilder.class, boolCounter);
266-
queryHandlers.put(FunctionScoreQueryBuilder.class, functionScoreCounter);
267-
queryHandlers.put(MatchQueryBuilder.class, matchCounter);
268-
queryHandlers.put(MatchPhraseQueryBuilder.class, matchPhrasePrefixCounter);
269-
queryHandlers.put(MultiMatchQueryBuilder.class, multiMatchCounter);
270-
queryHandlers.put(QueryStringQueryBuilder.class, queryStringCounter);
271-
queryHandlers.put(RangeQueryBuilder.class, rangeCounter);
272-
queryHandlers.put(RegexpQueryBuilder.class, regexpCounter);
273-
queryHandlers.put(TermQueryBuilder.class, termCounter);
274-
queryHandlers.put(WildcardQueryBuilder.class, wildcardCounter);
275-
queryHandlers.put(BoostingQueryBuilder.class, boostingCounter);
276-
queryHandlers.put(ConstantScoreQueryBuilder.class, constantScoreCounter);
277-
queryHandlers.put(DisMaxQueryBuilder.class, disMaxCounter);
278-
queryHandlers.put(DistanceFeatureQueryBuilder.class, distanceFeatureCounter);
279-
queryHandlers.put(ExistsQueryBuilder.class, existsCounter);
280-
queryHandlers.put(FieldMaskingSpanQueryBuilder.class, fieldMaskingSpanCounter);
281-
queryHandlers.put(FuzzyQueryBuilder.class, fuzzyCounter);
282-
queryHandlers.put(GeoBoundingBoxQueryBuilder.class, geoBoundingBoxCounter);
283-
queryHandlers.put(GeoDistanceQueryBuilder.class, geoDistanceCounter);
284-
queryHandlers.put(GeoPolygonQueryBuilder.class, geoPolygonCounter);
285-
queryHandlers.put(GeoShapeQueryBuilder.class, geoShapeCounter);
286-
queryHandlers.put(IntervalQueryBuilder.class, intervalCounter);
287-
queryHandlers.put(MatchAllQueryBuilder.class, matchallCounter);
288-
queryHandlers.put(PrefixQueryBuilder.class, prefixCounter);
289-
queryHandlers.put(ScriptQueryBuilder.class, scriptCounter);
290-
queryHandlers.put(SimpleQueryStringBuilder.class, simpleQueryStringCounter);
62+
private Counter createQueryCounter(String counterName) {
63+
Counter counter = metricsRegistry.createCounter(
64+
"search.query.type." + counterName + ".count",
65+
"Counter for the number of top level and nested " + counterName + " search queries",
66+
UNIT
67+
);
68+
return counter;
29169
}
29270
}

0 commit comments

Comments
 (0)