Skip to content

Commit dda1aa4

Browse files
committed
Log query shape if trace log enabled
Signed-off-by: Siddhant Deshmukh <deshsid@amazon.com>
1 parent 5239f68 commit dda1aa4

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

src/main/java/org/opensearch/plugin/insights/core/listener/QueryInsightsListener.java

+14-3
Original file line numberDiff line numberDiff line change
@@ -272,14 +272,25 @@ private void constructSearchQueryRecord(final SearchPhaseContext context, final
272272
attributes.put(Attribute.PHASE_LATENCY_MAP, searchRequestContext.phaseTookMap());
273273
attributes.put(Attribute.TASK_RESOURCE_USAGES, tasksResourceUsages);
274274

275-
if (queryInsightsService.isGroupingEnabled()) {
276-
String hashcode = queryShapeGenerator.getShapeHashCodeAsString(
275+
if (queryInsightsService.isGroupingEnabled() || log.isTraceEnabled()) {
276+
// Generate the query shape only if grouping is enabled or trace logging is enabled
277+
String queryShape = queryShapeGenerator.buildShape(
277278
request.source(),
278279
groupingFieldNameEnabled,
279280
groupingFieldTypeEnabled,
280281
searchRequestContext.getSuccessfulSearchShardIndices()
281282
);
282-
attributes.put(Attribute.QUERY_HASHCODE, hashcode);
283+
284+
// Print the query shape if tracer is enabled
285+
if (log.isTraceEnabled()) {
286+
log.trace("Query Shape: {}", queryShape);
287+
}
288+
289+
// Add hashcode attribute when grouping is enabled
290+
if (queryInsightsService.isGroupingEnabled()) {
291+
String hashcode = queryShapeGenerator.getShapeHashCodeAsString(queryShape);
292+
attributes.put(Attribute.QUERY_HASHCODE, hashcode);
293+
}
283294
}
284295

285296
Map<String, Object> labels = new HashMap<>();

src/main/java/org/opensearch/plugin/insights/core/service/categorizer/QueryShapeGenerator.java

+12
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,18 @@ public String getShapeHashCodeAsString(
8787
return hashAsString;
8888
}
8989

90+
/**
91+
* Gets the hash code of the query shape as a string.
92+
*
93+
* @param queryShape query shape as input
94+
* @return Hash code of the query shape as a string
95+
*/
96+
public String getShapeHashCodeAsString(String queryShape) {
97+
final BytesRef shapeBytes = new BytesRef(queryShape);
98+
MurmurHash3.Hash128 hashcode = MurmurHash3.hash128(shapeBytes.bytes, 0, shapeBytes.length, 0, new MurmurHash3.Hash128());
99+
return Long.toHexString(hashcode.h1) + Long.toHexString(hashcode.h2);
100+
}
101+
90102
/**
91103
* Builds the search query shape given a source.
92104
*

0 commit comments

Comments
 (0)