diff --git a/x-pack/plugin/profiling/src/main/java/org/elasticsearch/xpack/profiling/action/GetStackTracesResponseBuilder.java b/x-pack/plugin/profiling/src/main/java/org/elasticsearch/xpack/profiling/action/GetStackTracesResponseBuilder.java index 76e858b54ef6f..bf1111e2ff1d5 100644 --- a/x-pack/plugin/profiling/src/main/java/org/elasticsearch/xpack/profiling/action/GetStackTracesResponseBuilder.java +++ b/x-pack/plugin/profiling/src/main/java/org/elasticsearch/xpack/profiling/action/GetStackTracesResponseBuilder.java @@ -137,6 +137,22 @@ public void setTotalSamples(long totalSamples) { } public GetStackTracesResponse build() { + // Merge the TraceEvent data into the StackTraces. + if (stackTraces != null) { + for (Map.Entry entry : stackTraceEvents.entrySet()) { + TraceEventID traceEventID = entry.getKey(); + StackTrace stackTrace = stackTraces.get(traceEventID.stacktraceID()); + if (stackTrace != null) { + TraceEvent event = entry.getValue(); + if (event.subGroups != null) { + stackTrace.subGroups = event.subGroups; + } + stackTrace.count += event.count; + stackTrace.annualCO2Tons += event.annualCO2Tons; + stackTrace.annualCostsUSD += event.annualCostsUSD; + } + } + } return new GetStackTracesResponse(stackTraces, stackFrames, executables, stackTraceEvents, totalFrames, samplingRate, totalSamples); } } diff --git a/x-pack/plugin/profiling/src/main/java/org/elasticsearch/xpack/profiling/action/StackTrace.java b/x-pack/plugin/profiling/src/main/java/org/elasticsearch/xpack/profiling/action/StackTrace.java index 2a1281614acda..48c66d91abfb4 100644 --- a/x-pack/plugin/profiling/src/main/java/org/elasticsearch/xpack/profiling/action/StackTrace.java +++ b/x-pack/plugin/profiling/src/main/java/org/elasticsearch/xpack/profiling/action/StackTrace.java @@ -27,12 +27,18 @@ final class StackTrace implements ToXContentObject { String[] frameIds; int[] typeIds; SubGroup subGroups; + double annualCO2Tons; + double annualCostsUSD; + long count; StackTrace(int[] addressOrLines, String[] fileIds, String[] frameIds, int[] typeIds) { this.addressOrLines = addressOrLines; this.fileIds = fileIds; this.frameIds = frameIds; this.typeIds = typeIds; + annualCO2Tons = 0.0d; + annualCostsUSD = 0.0d; + count = 0; } private static final int BASE64_FRAME_ID_LENGTH = 32; @@ -210,12 +216,15 @@ public void forNativeAndKernelFrames(Consumer consumer) { @Override public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { - builder.startObject(); - builder.field("address_or_lines", this.addressOrLines); - builder.field("file_ids", this.fileIds); - builder.field("frame_ids", this.frameIds); - builder.field("type_ids", this.typeIds); - builder.endObject(); + builder.startObject() + .field("address_or_lines", this.addressOrLines) + .field("file_ids", this.fileIds) + .field("frame_ids", this.frameIds) + .field("type_ids", this.typeIds) + .field("annual_co2_tons", this.annualCO2Tons) + .field("annual_costs_usd", this.annualCostsUSD) + .field("count", this.count) + .endObject(); return builder; } @@ -230,7 +239,7 @@ public boolean equals(Object o) { && Arrays.equals(fileIds, that.fileIds) && Arrays.equals(frameIds, that.frameIds) && Arrays.equals(typeIds, that.typeIds); - // Don't compare metadata like subGroups. + // Don't compare metadata like annualized co2, annualized costs, subGroups and count. } // Don't hash metadata like annualized co2, annualized costs, subGroups and count.