diff --git a/x-pack/plugin/profiling/src/internalClusterTest/java/org/elasticsearch/xpack/profiling/GetStackTracesActionIT.java b/x-pack/plugin/profiling/src/internalClusterTest/java/org/elasticsearch/xpack/profiling/GetStackTracesActionIT.java index 8ad68ca7ceebc..6becc2eb6e385 100644 --- a/x-pack/plugin/profiling/src/internalClusterTest/java/org/elasticsearch/xpack/profiling/GetStackTracesActionIT.java +++ b/x-pack/plugin/profiling/src/internalClusterTest/java/org/elasticsearch/xpack/profiling/GetStackTracesActionIT.java @@ -77,6 +77,8 @@ public void testGetStackTracesFromAPMWithMatchNoDownsampling() throws Exception assertEquals(39, stackTrace.fileIds.size()); assertEquals(39, stackTrace.frameIds.size()); assertEquals(39, stackTrace.typeIds.size()); + assertTrue(stackTrace.annualCO2Tons > 0.0d); + assertTrue(stackTrace.annualCostsUSD > 0.0d); assertNotNull(response.getStackFrames()); StackFrame stackFrame = response.getStackFrames().get("fhsEKXDuxJ-jIJrZpdRuSAAAAAAAAFtj"); @@ -141,6 +143,8 @@ public int hashCode() { assertEquals(39, stackTrace.fileIds.size()); assertEquals(39, stackTrace.frameIds.size()); assertEquals(39, stackTrace.typeIds.size()); + assertTrue(stackTrace.annualCO2Tons > 0.0d); + assertTrue(stackTrace.annualCostsUSD > 0.0d); assertNotNull(response.getStackFrames()); StackFrame stackFrame = response.getStackFrames().get("fhsEKXDuxJ-jIJrZpdRuSAAAAAAAAFtj"); diff --git a/x-pack/plugin/profiling/src/main/java/org/elasticsearch/xpack/profiling/TransportGetStackTracesAction.java b/x-pack/plugin/profiling/src/main/java/org/elasticsearch/xpack/profiling/TransportGetStackTracesAction.java index 567c36e6b4404..2674893c2382f 100644 --- a/x-pack/plugin/profiling/src/main/java/org/elasticsearch/xpack/profiling/TransportGetStackTracesAction.java +++ b/x-pack/plugin/profiling/src/main/java/org/elasticsearch/xpack/profiling/TransportGetStackTracesAction.java @@ -275,11 +275,8 @@ private void searchGenericEventGroupedByStackTrace( SingleBucketAggregation sample = searchResponse.getAggregations().get("sample"); StringTerms stacktraces = sample.getAggregations().get("group_by"); - // When we switch to aggregation by (hostID, stacktraceID) we need to change the empty List to this. - // List hostEventCounts = new ArrayList<>(MAX_TRACE_EVENTS_RESULT_SIZE); - // Related: https://github.com/elastic/prodfiler/issues/4300 - // See also the aggregation in searchEventGroupedByStackTrace() for the other parts of the change. - List hostEventCounts = Collections.emptyList(); + // When we retrieve host data for generic events, we need to adapt the handler similar to searchEventGroupedByStackTrace(). + List hostEventCounts = new ArrayList<>(stacktraces.getBuckets().size()); // aggregation Map stackTraceEvents = new TreeMap<>(); @@ -288,6 +285,8 @@ private void searchGenericEventGroupedByStackTrace( totalSamples += count; String stackTraceID = stacktraceBucket.getKeyAsString(); + // For now, add a dummy-entry so CO2 and cost calculation can operate. In the future we will have one value per host. + hostEventCounts.add(new HostEventCount("unknown", stackTraceID, (int) count)); TraceEvent event = stackTraceEvents.get(stackTraceID); if (event == null) { event = new TraceEvent(stackTraceID);