Skip to content

Commit

Permalink
Log query shape if trace log enabled
Browse files Browse the repository at this point in the history
Signed-off-by: Siddhant Deshmukh <[email protected]>
  • Loading branch information
deshsidd committed Oct 10, 2024
1 parent 5239f68 commit f05c5d2
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -272,14 +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(
if (queryInsightsService.isGroupingEnabled() || log.isTraceEnabled()) {
// Generate the query shape only if grouping is enabled or trace logging is enabled
String queryShape = queryShapeGenerator.buildShape(
request.source(),
groupingFieldNameEnabled,
groupingFieldTypeEnabled,
searchRequestContext.getSuccessfulSearchShardIndices()
);
attributes.put(Attribute.QUERY_HASHCODE, hashcode);

// Print the query shape if tracer is enabled
if (log.isTraceEnabled()) {
log.trace("Query Shape: {}", 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
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,18 @@ public String getShapeHashCodeAsString(
return hashAsString;
}

/**
* Gets the hash code of the query shape as a string.
*
* @param queryShape query shape as input
* @return Hash code of the query shape as a string
*/
public String getShapeHashCodeAsString(String queryShape) {
final BytesRef shapeBytes = new BytesRef(queryShape);
MurmurHash3.Hash128 hashcode = MurmurHash3.hash128(shapeBytes.bytes, 0, shapeBytes.length, 0, new MurmurHash3.Hash128());
return Long.toHexString(hashcode.h1) + Long.toHexString(hashcode.h2);
}

/**
* Builds the search query shape given a source.
*
Expand Down

0 comments on commit f05c5d2

Please sign in to comment.