@@ -337,6 +337,14 @@ private void searchEventGroupedByStackTrace(
337337 // Especially with high cardinality fields, this makes aggregations really slow.
338338 .executionHint ("map" )
339339 .subAggregation (groupByStackTraceId );
340+ TermsAggregationBuilder groupByThreadName = new TermsAggregationBuilder ("group_by" )
341+ // 'size' specifies the max number of host ID we support per request.
342+ .size (MAX_TRACE_EVENTS_RESULT_SIZE )
343+ .field ("process.thread.name" )
344+ // 'execution_hint: map' skips the slow building of ordinals that we don't need.
345+ // Especially with high cardinality fields, this makes aggregations really slow.
346+ .executionHint ("map" )
347+ .subAggregation (groupByHostId );
340348 SubGroupCollector subGroups = SubGroupCollector .attach (
341349 groupByStackTraceId ,
342350 request .getAggregationFields (),
@@ -359,7 +367,7 @@ private void searchEventGroupedByStackTrace(
359367 // 'execution_hint: map' skips the slow building of ordinals that we don't need.
360368 // Especially with high cardinality fields, this makes aggregations really slow.
361369 .executionHint ("map" )
362- .subAggregation (groupByHostId )
370+ .subAggregation (groupByThreadName )
363371 )
364372 .addAggregation (new SumAggregationBuilder ("total_count" ).field ("Stacktrace.count" ))
365373 .execute (handleEventsGroupedByStackTrace (submitTask , client , responseBuilder , submitListener , searchResponse -> {
@@ -379,39 +387,45 @@ private void searchEventGroupedByStackTrace(
379387 for (Terms .Bucket executableBucket : executableNames .getBuckets ()) {
380388 String executableName = executableBucket .getKeyAsString ();
381389
382- Terms hosts = executableBucket .getAggregations ().get ("group_by" );
383- for (Terms .Bucket hostBucket : hosts .getBuckets ()) {
384- String hostid = hostBucket .getKeyAsString ();
385-
386- Terms stacktraces = hostBucket .getAggregations ().get ("group_by" );
387- for (Terms .Bucket stacktraceBucket : stacktraces .getBuckets ()) {
388- Sum count = stacktraceBucket .getAggregations ().get ("count" );
389- int finalCount = resampler .adjustSampleCount ((int ) count .value ());
390- if (finalCount <= 0 ) {
391- continue ;
390+ Terms threads = executableBucket .getAggregations ().get ("group_by" );
391+ for (Terms .Bucket threadBucket : threads .getBuckets ()) {
392+ String threadName = threadBucket .getKeyAsString ();
393+
394+ Terms hosts = threadBucket .getAggregations ().get ("group_by" );
395+ for (Terms .Bucket hostBucket : hosts .getBuckets ()) {
396+ String hostid = hostBucket .getKeyAsString ();
397+
398+ Terms stacktraces = hostBucket .getAggregations ().get ("group_by" );
399+ for (Terms .Bucket stacktraceBucket : stacktraces .getBuckets ()) {
400+ Sum count = stacktraceBucket .getAggregations ().get ("count" );
401+ int finalCount = resampler .adjustSampleCount ((int ) count .value ());
402+ if (finalCount <= 0 ) {
403+ continue ;
404+ }
405+ totalFinalCount += finalCount ;
406+
407+ String stackTraceID = stacktraceBucket .getKeyAsString ();
408+
409+ /*
410+ The same stacktraces may come from different hosts (eventually from different datacenters).
411+ We make a list of the triples here. As soon as we have the host metadata, we can calculate
412+ the CO2 emission and the costs for each TraceEvent.
413+ */
414+ hostEventCounts .add (new HostEventCount (hostid , stackTraceID , finalCount ));
415+
416+ TraceEvent event = stackTraceEvents .get (stackTraceID );
417+ if (event == null ) {
418+ event = new TraceEvent (stackTraceID );
419+ event .executableName = executableName ; // THIS IS A HACK (need a proper data model)
420+ event .threadName = threadName ; // THIS IS A HACK (need a proper data model)
421+ stackTraceEvents .put (stackTraceID , event );
422+ }
423+ event .count += finalCount ;
424+ subGroups .collectResults (stacktraceBucket , event );
425+
426+ TraceEventMetadata meta = new TraceEventMetadata (executableName , stackTraceID );
427+ executableEvents .putIfAbsent (meta , event );
392428 }
393- totalFinalCount += finalCount ;
394-
395- String stackTraceID = stacktraceBucket .getKeyAsString ();
396-
397- /*
398- The same stacktraces may come from different hosts (eventually from different datacenters).
399- We make a list of the triples here. As soon as we have the host metadata, we can calculate
400- the CO2 emission and the costs for each TraceEvent.
401- */
402- hostEventCounts .add (new HostEventCount (hostid , stackTraceID , finalCount ));
403-
404- TraceEvent event = stackTraceEvents .get (stackTraceID );
405- if (event == null ) {
406- event = new TraceEvent (stackTraceID );
407- event .executableName = executableName ; // THIS IS A HACK (need a proper data model)
408- stackTraceEvents .put (stackTraceID , event );
409- }
410- event .count += finalCount ;
411- subGroups .collectResults (stacktraceBucket , event );
412-
413- TraceEventMetadata meta = new TraceEventMetadata (executableName , stackTraceID );
414- executableEvents .putIfAbsent (meta , event );
415429 }
416430 }
417431 }
0 commit comments