Skip to content

Commit

Permalink
Add field type to query shape and extensive unit tests (#140)
Browse files Browse the repository at this point in the history
* Add query type to shape and extensive unit tests

Signed-off-by: Siddhant Deshmukh <deshsid@amazon.com>

* Log query shape if trace log enabled

Signed-off-by: Siddhant Deshmukh <deshsid@amazon.com>

* Refactor code, handle multifield with test, remove properties cache

Signed-off-by: Siddhant Deshmukh <deshsid@amazon.com>

* Use only first index and optimization to get properties once per query

Signed-off-by: Siddhant Deshmukh <deshsid@amazon.com>

* Further refactoring

Signed-off-by: Siddhant Deshmukh <deshsid@amazon.com>

---------

Signed-off-by: Siddhant Deshmukh <deshsid@amazon.com>
  • Loading branch information
deshsidd authored Oct 10, 2024
1 parent f89eb88 commit dcf52c2
Show file tree
Hide file tree
Showing 6 changed files with 957 additions and 103 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public final class QueryInsightsListener extends SearchRequestOperationsListener
private final ClusterService clusterService;
private boolean groupingFieldNameEnabled;
private boolean groupingFieldTypeEnabled;
private final QueryShapeGenerator queryShapeGenerator;

/**
* Constructor for QueryInsightsListener
Expand Down Expand Up @@ -87,6 +88,7 @@ public QueryInsightsListener(
super(initiallyEnabled);
this.clusterService = clusterService;
this.queryInsightsService = queryInsightsService;
this.queryShapeGenerator = new QueryShapeGenerator(clusterService);

// Setting endpoints set up for top n queries, including enabling top n queries, window size, and top n size
// Expected metricTypes are Latency, CPU, and Memory.
Expand Down Expand Up @@ -270,9 +272,25 @@ private void constructSearchQueryRecord(final SearchPhaseContext context, final
attributes.put(Attribute.PHASE_LATENCY_MAP, searchRequestContext.phaseTookMap());
attributes.put(Attribute.TASK_RESOURCE_USAGES, tasksResourceUsages);

if (queryInsightsService.isGroupingEnabled()) {
String hashcode = QueryShapeGenerator.getShapeHashCodeAsString(request.source(), groupingFieldNameEnabled);
attributes.put(Attribute.QUERY_HASHCODE, hashcode);
if (queryInsightsService.isGroupingEnabled() || log.isTraceEnabled()) {
// Generate the query shape only if grouping is enabled or trace logging is enabled
final String queryShape = queryShapeGenerator.buildShape(
request.source(),
groupingFieldNameEnabled,
groupingFieldTypeEnabled,
searchRequestContext.getSuccessfulSearchShardIndices()
);

// Print the query shape if tracer is enabled
if (log.isTraceEnabled()) {
log.trace("Query Shape:\n{}", queryShape);
}

// Add hashcode attribute when grouping is enabled
if (queryInsightsService.isGroupingEnabled()) {
String hashcode = queryShapeGenerator.getShapeHashCodeAsString(queryShape);
attributes.put(Attribute.QUERY_HASHCODE, hashcode);
}
}

Map<String, Object> labels = new HashMap<>();
Expand Down
Loading

0 comments on commit dcf52c2

Please sign in to comment.