Skip to content

Commit

Permalink
Javadocs
Browse files Browse the repository at this point in the history
Signed-off-by: Siddhant Deshmukh <[email protected]>
  • Loading branch information
deshsidd committed Jun 24, 2024
1 parent 2bd1a05 commit 68a226e
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ public QueryBuilderVisitor getChildVisitor(BooleanClause.Occur occur) {
return childVisitorWrapper;
}

/**
* Convert query builder tree to json
* @return
*/
public String toJson() {
StringBuilder outputBuilder = new StringBuilder("{\"type\":\"").append(queryType.get()).append("\"");
for (Map.Entry<BooleanClause.Occur, List<QueryShapeVisitor>> entry : childVisitors.entrySet()) {
Expand All @@ -75,6 +79,11 @@ public String toJson() {
return outputBuilder.toString();
}

/**
* Pretty print the query builder tree
* @param indent indent size
* @return
*/
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()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,18 @@ public class SearchQueryAggregationCategorizer {
private static final String TYPE_TAG = "type";
private final SearchQueryCounters searchQueryCounters;

/**
* Constructor for SearchQueryAggregationCategorizer
* @param searchQueryCounters
*/
public SearchQueryAggregationCategorizer(SearchQueryCounters searchQueryCounters) {
this.searchQueryCounters = searchQueryCounters;
}

/**
* Increment aggregation related counters
* @param aggregatorFactories input aggregations
*/
public void incrementSearchQueryAggregationCounters(Collection<AggregationBuilder> aggregatorFactories) {
for (AggregationBuilder aggregationBuilder : aggregatorFactories) {
incrementCountersRecursively(aggregationBuilder);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,37 @@ public final class SearchQueryCategorizer {

private static final Logger log = LogManager.getLogger(SearchQueryCategorizer.class);

/**
* Contains all the search query counters
*/
public final SearchQueryCounters searchQueryCounters;

final SearchQueryAggregationCategorizer searchQueryAggregationCategorizer;

/**
* Constructor for SearchQueryCategorizor
* @param metricsRegistry
*/
public SearchQueryCategorizer(MetricsRegistry metricsRegistry) {
searchQueryCounters = new SearchQueryCounters(metricsRegistry);
searchQueryAggregationCategorizer = new SearchQueryAggregationCategorizer(searchQueryCounters);
}

/**
* Consume records and increment counters for the records
* @param records records to consume
*/
public void consumeRecords(List<SearchQueryRecord> records) {
for (SearchQueryRecord record : records) {
SearchSourceBuilder source = (SearchSourceBuilder) record.getAttributes().get(Attribute.SOURCE);
categorize(source);
}
}

/**
* Increment categorizations counters for the given source search query
* @param source search query source
*/
public void categorize(SearchSourceBuilder source) {
QueryBuilder topLevelQueryBuilder = source.query();
logQueryShape(topLevelQueryBuilder);
Expand All @@ -58,7 +73,7 @@ public void categorize(SearchSourceBuilder source) {
private void incrementQuerySortCounters(List<SortBuilder<?>> sorts) {
if (sorts != null && sorts.size() > 0) {
for (ListIterator<SortBuilder<?>> it = sorts.listIterator(); it.hasNext();) {
SortBuilder sortBuilder = it.next();
SortBuilder<?> sortBuilder = it.next();
String sortOrder = sortBuilder.order().toString();
searchQueryCounters.sortCounter.add(1, Tags.create().addTag("sort_order", sortOrder));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,28 @@ public final class SearchQueryCounters {
private static final String LEVEL_TAG = "level";
private static final String UNIT = "1";
private final MetricsRegistry metricsRegistry;
/**
* Aggregation counter
*/
public final Counter aggCounter;
/**
* Counter for all other query types (catch all)
*/
public final Counter otherQueryCounter;
/**
* Counter for sort
*/
public final Counter sortCounter;
private final Map<Class<? extends QueryBuilder>, Counter> queryHandlers;
/**
* Counter name to Counter object map
*/
public final ConcurrentHashMap<String, Counter> nameToQueryTypeCounters;

/**
* Constructor
* @param metricsRegistry opentelemetry metrics registry
*/
public SearchQueryCounters(MetricsRegistry metricsRegistry) {
this.metricsRegistry = metricsRegistry;
this.nameToQueryTypeCounters = new ConcurrentHashMap<>();
Expand All @@ -52,6 +68,11 @@ public SearchQueryCounters(MetricsRegistry metricsRegistry) {

}

/**
* Increment counter
* @param queryBuilder query builder
* @param level level of query builder, 0 being highest level
*/
public void incrementCounter(QueryBuilder queryBuilder, int level) {
String uniqueQueryCounterName = queryBuilder.getName();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ public class QueryInsightsService extends AbstractLifecycleComponent {
* @param clusterSettings OpenSearch cluster level settings
* @param threadPool The OpenSearch thread pool to run async tasks
* @param client OS client
* @param metricsRegistry Opentelemetry Metrics registry
*/
@Inject
public QueryInsightsService(final ClusterSettings clusterSettings, final ThreadPool threadPool, final Client client, final MetricsRegistry metricsRegistry) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,13 @@

import org.opensearch.common.settings.Setting;

/**
* Settings for Query Categorization
*/
public class QueryCategorizationSettings {
/**
* Enabling search query metrics
*/
public static final Setting<Boolean> SEARCH_QUERY_METRICS_ENABLED_SETTING = Setting.boolSetting(
"search.query.metrics.enabled",
false,
Expand Down

0 comments on commit 68a226e

Please sign in to comment.