Skip to content

Commit

Permalink
Limit overhead of capturing stacktrace in REST Client
Browse files Browse the repository at this point in the history
This is done by utilizing the StackWalker API
and limiting the number of frames captured

Closes: quarkusio#42508
  • Loading branch information
geoand committed Aug 26, 2024
1 parent 2105d46 commit b999a90
Showing 1 changed file with 14 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package org.jboss.resteasy.reactive.client.handlers;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Stream;

import org.jboss.resteasy.reactive.client.impl.ClientRequestContextImpl;
import org.jboss.resteasy.reactive.client.impl.RestClientRequestContext;
Expand Down Expand Up @@ -35,21 +37,18 @@ public void handle(RestClientRequestContext requestContext) throws Exception {
}

private void captureCallerStackTrace(ClientRequestContextImpl clientRequestContext) {
StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
List<StackTraceElement> effectiveStackTrace = new ArrayList<>(stackTrace.length);
boolean foundUserMethod = false;
// skip first trace which is Thread.getStackTrace
for (int i = 1; i < stackTrace.length; i++) {
StackTraceElement trace = stackTrace[i];
if (foundUserMethod) {
effectiveStackTrace.add(trace);
} else if (!trace.getClassName().startsWith(RESTEASY_REACTIVE_PACKAGE)
&& !trace.getClassName().contains(AUTOGENERATED_TAG)) {
// Skip the latest traces that starts with the "org.jboss.resteasy.reactive" package,
effectiveStackTrace.add(trace);
foundUserMethod = true;
List<StackTraceElement> effectiveStackTrace = StackWalker.getInstance().walk(new Function<>() {
@Override
public List<StackTraceElement> apply(Stream<StackWalker.StackFrame> stream) {
return stream.dropWhile(new Predicate<>() {
@Override
public boolean test(StackWalker.StackFrame stackFrame) {
String className = stackFrame.getClassName();
return className.startsWith(RESTEASY_REACTIVE_PACKAGE) || className.contains(AUTOGENERATED_TAG);
}
}).limit(5).map(StackWalker.StackFrame::toStackTraceElement).toList();
}
}
});

clientRequestContext.getRestClientRequestContext()
.setCallerStackTrace(effectiveStackTrace.toArray(EMPTY_ARRAY));
Expand Down

0 comments on commit b999a90

Please sign in to comment.