forked from opensearch-project/OpenSearch
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Integrate metrics framework, add counters and log query shape
Signed-off-by: Siddhant Deshmukh <[email protected]>
- Loading branch information
Showing
3 changed files
with
188 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
server/src/main/java/org/opensearch/action/search/SearchQueryCounters.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
*/ | ||
|
||
package org.opensearch.action.search; | ||
|
||
public class SearchQueryCounters { | ||
private final MetricsRegistry metricsRegistry; | ||
|
||
// Counters related to Query types | ||
public final Counter aggCounter; | ||
public final Counter boolCounter; | ||
public final Counter functionScoreCounter; | ||
public final Counter matchCounter; | ||
public final Counter matchPhrasePrefixCounter; | ||
public final Counter multiMatchCounter; | ||
public final Counter otherQueryCounter; | ||
public final Counter queryStringQueryCounter; | ||
public final Counter rangeCounter; | ||
public final Counter regexCounter; | ||
public final Counter termCounter; | ||
public final Counter totalCounter; | ||
public final Counter wildcardCounter; | ||
|
||
|
||
|
||
public SearchQueryCounters(MetricsRegistry metricsRegistry) { | ||
this.metricsRegistry = metricsRegistry; | ||
this.aggCounter = metricsRegistry.createCounter("aggSearchQueryCounter", | ||
"Counter for the number of top level and nested agg search queries", "0"); | ||
this.boolCounter = metricsRegistry.createCounter("boolSearchQueryCounter", | ||
"Counter for the number of top level and nested bool search queries", "0"); | ||
this.functionScoreCounter = metricsRegistry.createCounter("functionScoreSearchQueryCounter", | ||
"Counter for the number of top level and nested function score search queries", "0"); | ||
this.matchCounter = metricsRegistry.createCounter("matchSearchQueryCounter", | ||
"Counter for the number of top level and nested match search queries", "0"); | ||
this.matchPhrasePrefixCounter = metricsRegistry.createCounter("matchPhrasePrefixSearchQueryCounter", | ||
"Counter for the number of top level and nested match phrase prefix search queries", "0"); | ||
this.multiMatchCounter = metricsRegistry.createCounter("multiMatchSearchQueryCounter", | ||
"Counter for the number of top level and nested multi match search queries", "0"); | ||
this.otherQueryCounter = metricsRegistry.createCounter("otherSearchQueryCounter", | ||
"Counter for the number of top level and nested search queries that do not match any other categories", "0"); | ||
this.queryStringQueryCounter = metricsRegistry.createCounter("queryStringQuerySearchQueryCounter", | ||
"Counter for the number of top level and nested queryStringQuery search queries", "0"); | ||
this.rangeCounter = metricsRegistry.createCounter("rangeSearchQueryCounter", | ||
"Counter for the number of top level and nested range search queries", "0"); | ||
this.regexCounter = metricsRegistry.createCounter("regexSearchQueryCounter", | ||
"Counter for the number of top level and nested regex search queries", "0"); | ||
this.termCounter = metricsRegistry.createCounter("termSearchQueryCounter", | ||
"Counter for the number of top level and nested term search queries", "0"); | ||
this.totalCounter = metricsRegistry.createCounter("totalSearchQueryCounter", | ||
"Counter for the number of top level and nested search queries", "0"); | ||
this.wildcardCounter = metricsRegistry.createCounter("wildcardSearchQueryCounter", | ||
"Counter for the number of top level and nested wildcard search queries", "0"); | ||
} | ||
} |
82 changes: 82 additions & 0 deletions
82
server/src/main/java/org/opensearch/index/query/QueryShapeVisitor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
*/ | ||
|
||
package org.opensearch.index.query; | ||
|
||
import org.apache.lucene.search.BooleanClause; | ||
import org.opensearch.common.SetOnce; | ||
|
||
import java.util.ArrayList; | ||
import java.util.EnumMap; | ||
import java.util.List; | ||
import java.util.Locale; | ||
import java.util.Map; | ||
|
||
public class QueryShapeVisitor implements QueryBuilderVisitor { | ||
private final SetOnce<String> queryType = new SetOnce<>(); | ||
private final Map<BooleanClause.Occur, List<QueryShapeVisitor>> childVisitors = new EnumMap<>(BooleanClause.Occur.class); | ||
|
||
@Override | ||
public void accept(QueryBuilder qb) { | ||
queryType.set(qb.getName()); | ||
} | ||
|
||
@Override | ||
public QueryBuilderVisitor getChildVisitor(BooleanClause.Occur occur) { | ||
// Should get called once per Occur value | ||
if (childVisitors.containsKey(occur)) { | ||
throw new IllegalStateException("getChildVisitor already called for " + occur); | ||
} | ||
final List<QueryShapeVisitor> childVisitorList = new ArrayList<>(); | ||
QueryBuilderVisitor childVisitorWrapper = new QueryBuilderVisitor() { | ||
QueryShapeVisitor currentChild; | ||
@Override | ||
public void accept(QueryBuilder qb) { | ||
currentChild = new QueryShapeVisitor(); | ||
childVisitorList.add(currentChild); | ||
currentChild.accept(qb); | ||
} | ||
|
||
@Override | ||
public QueryBuilderVisitor getChildVisitor(BooleanClause.Occur occur) { | ||
return currentChild.getChildVisitor(occur); | ||
} | ||
}; | ||
childVisitors.put(occur, childVisitorList); | ||
return childVisitorWrapper; | ||
} | ||
|
||
public String toJson() { | ||
StringBuilder outputBuilder = new StringBuilder("{\"type\":\"").append(queryType.get()).append("\""); | ||
for (Map.Entry<BooleanClause.Occur, List<QueryShapeVisitor>> entry : childVisitors.entrySet()) { | ||
outputBuilder.append(",\"").append(entry.getKey().name().toLowerCase(Locale.ROOT)).append("\"["); | ||
boolean first = true; | ||
for (QueryShapeVisitor child : entry.getValue()) { | ||
if (!first) { | ||
outputBuilder.append(","); | ||
} | ||
outputBuilder.append(child.toJson()); | ||
first = false; | ||
} | ||
outputBuilder.append("]"); | ||
} | ||
outputBuilder.append("}"); | ||
return outputBuilder.toString(); | ||
} | ||
|
||
public String prettyPrintTree(String indent) { | ||
StringBuilder outputBuilder = new StringBuilder(indent).append(queryType.get()).append("\n"); | ||
for (Map.Entry<BooleanClause.Occur, List<QueryShapeVisitor>> entry : childVisitors.entrySet()) { | ||
outputBuilder.append(indent).append(" ").append(entry.getKey().name().toLowerCase(Locale.ROOT)).append(":\n"); | ||
for (QueryShapeVisitor child : entry.getValue()) { | ||
outputBuilder.append(child.prettyPrintTree(indent + " ")); | ||
} | ||
} | ||
return outputBuilder.toString(); | ||
} | ||
} |