Skip to content

Commit

Permalink
Add trace logging for response serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
danielmitterdorfer committed Jan 17, 2024
1 parent baa86c8 commit 0062799
Showing 1 changed file with 35 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.elasticsearch.common.xcontent.ChunkedToXContentHelper;
import org.elasticsearch.common.xcontent.ChunkedToXContentObject;
import org.elasticsearch.core.Nullable;
import org.elasticsearch.telemetry.tracing.TraceLogger;
import org.elasticsearch.xcontent.ToXContent;

import java.util.Collections;
Expand Down Expand Up @@ -90,14 +91,17 @@ public long getTotalSamples() {
public Iterator<? extends ToXContent> toXContentChunked(ToXContent.Params params) {
return Iterators.concat(
ChunkedToXContentHelper.startObject(),
optional("stack_traces", stackTraces, ChunkedToXContentHelper::xContentValuesMap),
optional("stack_frames", stackFrames, ChunkedToXContentHelper::xContentValuesMap),
optional("executables", executables, ChunkedToXContentHelper::map),
traced("stack_traces", optional("stack_traces", stackTraces, ChunkedToXContentHelper::xContentValuesMap)),
traced("stack_frames", optional("stack_frames", stackFrames, ChunkedToXContentHelper::xContentValuesMap)),
traced("executables", optional("executables", executables, ChunkedToXContentHelper::map)),
// render only count for backwards-compatibility
optional(
traced(
"stack_trace_events",
stackTraceEvents,
(n, v) -> ChunkedToXContentHelper.map(n, v, entry -> (b, p) -> b.field(entry.getKey(), entry.getValue().count))
optional(
"stack_trace_events",
stackTraceEvents,
(n, v) -> ChunkedToXContentHelper.map(n, v, entry -> (b, p) -> b.field(entry.getKey(), entry.getValue().count))
)
),
Iterators.single((b, p) -> b.field("total_frames", totalFrames)),
Iterators.single((b, p) -> b.field("sampling_rate", samplingRate)),
Expand All @@ -118,6 +122,31 @@ private static <T> Iterator<? extends ToXContent> optional(
return (values != null) ? supplier.apply(name, values) : Collections.emptyIterator();
}

private static <T> Iterator<? extends ToXContent> traced(String name, Iterator<? extends ToXContent> iterator) {
return new Iterator<>() {
private boolean traceStarted = false;

@Override
public boolean hasNext() {
if (traceStarted == false) {
TraceLogger.start(null, "toXContent[" + name + "]");
traceStarted = true;
}
boolean hasNext = iterator.hasNext();
if (hasNext == false) {
TraceLogger.stop(null, "toXContent[" + name + "]");
}
return hasNext;

}

@Override
public ToXContent next() {
return iterator.next();
}
};
}

@Override
public boolean equals(Object o) {
if (this == o) {
Expand Down

0 comments on commit 0062799

Please sign in to comment.