Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Backport 2.x] Add field type to query shape and extensive unit tests #141

Merged
merged 1 commit into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading