Skip to content

Commit

Permalink
Add config property for disabling REST Client stacktrace capture
Browse files Browse the repository at this point in the history
  • Loading branch information
geoand committed Sep 22, 2023
1 parent af4208a commit 9b08ab6
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public class RestClientConfig {
EMPTY.userAgent = Optional.empty();
EMPTY.http2 = Optional.empty();
EMPTY.alpn = Optional.empty();
EMPTY.captureStacktrace = Optional.empty();
}

public RestClientMultipartConfig multipart;
Expand Down Expand Up @@ -267,6 +268,13 @@ public class RestClientConfig {
@ConfigItem
public Optional<Boolean> alpn;

/**
* If {@code true}, the stacktrace of the invocation of the REST Client method is captured.
* This stacktrace will be used if the invocation throws an exception
*/
@ConfigItem
public Optional<Boolean> captureStacktrace;

public static RestClientConfig load(String configKey) {
final RestClientConfig instance = new RestClientConfig();

Expand Down Expand Up @@ -299,6 +307,7 @@ public static RestClientConfig load(String configKey) {
instance.name = getConfigValue(configKey, "name", String.class);
instance.userAgent = getConfigValue(configKey, "user-agent", String.class);
instance.http2 = getConfigValue(configKey, "http2", Boolean.class);
instance.captureStacktrace = getConfigValue(configKey, "capture-stacktrace", Boolean.class);

instance.multipart = new RestClientMultipartConfig();
instance.multipart.maxChunkSize = getConfigValue(configKey, "multipart.max-chunk-size", Integer.class);
Expand Down Expand Up @@ -339,6 +348,7 @@ public static RestClientConfig load(Class<?> interfaceClass) {
instance.userAgent = getConfigValue(interfaceClass, "user-agent", String.class);
instance.http2 = getConfigValue(interfaceClass, "http2", Boolean.class);
instance.alpn = getConfigValue(interfaceClass, "alpn", Boolean.class);
instance.captureStacktrace = getConfigValue(interfaceClass, "capture-stacktrace", Boolean.class);

instance.multipart = new RestClientMultipartConfig();
instance.multipart.maxChunkSize = getConfigValue(interfaceClass, "multipart.max-chunk-size", Integer.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,13 @@ public class RestClientsConfig {
@ConfigItem
public Optional<Boolean> alpn;

/**
* If {@code true}, the stacktrace of the invocation of the REST Client method is captured.
* This stacktrace will be used if the invocation throws an exception
*/
@ConfigItem(defaultValue = "true")
public boolean captureStacktrace;

public RestClientConfig getClientConfig(String configKey) {
if (configKey == null) {
return RestClientConfig.EMPTY;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,10 @@ private void configureCustomProperties(QuarkusRestClientBuilder builder) {
if (alpn.isPresent()) {
builder.property(QuarkusRestClientProperties.ALPN, alpn.get());
}

Boolean captureStacktrace = oneOf(clientConfigByClassName().captureStacktrace,
clientConfigByConfigKey().captureStacktrace).orElse(configRoot.captureStacktrace);
builder.property(QuarkusRestClientProperties.CAPTURE_STACKTRACE, captureStacktrace);
}

private void configureProxy(QuarkusRestClientBuilder builder) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,9 @@ public class QuarkusRestClientProperties {
*/
public static final String ALPN = "io.quarkus.rest.client.alpn";

/**
* If set to true, the stacktrace of the invocation of the REST Client method is captured
*/
public static final String CAPTURE_STACKTRACE = "io.quarkus.rest.client.capture-stacktrace";

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ public class ClientCaptureCurrentContextRestHandler implements ClientRestHandler

private static final String RESTEASY_REACTIVE_PACKAGE = "org.jboss.resteasy.reactive";
private static final String AUTOGENERATED_TAG = "$$";
private static final StackTraceElement[] EMPTY_ARRAY = new StackTraceElement[0];
private final boolean captureStacktrace;

public ClientCaptureCurrentContextRestHandler(boolean captureStacktrace) {
this.captureStacktrace = captureStacktrace;
}

@Override
public void handle(RestClientRequestContext requestContext) throws Exception {
Expand All @@ -23,7 +29,9 @@ public void handle(RestClientRequestContext requestContext) throws Exception {
return;
}

captureCallerStackTrace(clientRequestContext);
if (captureStacktrace) {
captureCallerStackTrace(clientRequestContext);
}
}

private void captureCallerStackTrace(ClientRequestContextImpl clientRequestContext) {
Expand All @@ -44,6 +52,6 @@ private void captureCallerStackTrace(ClientRequestContextImpl clientRequestConte
}

clientRequestContext.getRestClientRequestContext()
.setCallerStackTrace(effectiveStackTrace.toArray(new StackTraceElement[0]));
.setCallerStackTrace(effectiveStackTrace.toArray(EMPTY_ARRAY));
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.jboss.resteasy.reactive.client.impl;

import static org.jboss.resteasy.reactive.client.api.QuarkusRestClientProperties.CAPTURE_STACKTRACE;
import static org.jboss.resteasy.reactive.client.api.QuarkusRestClientProperties.CONNECTION_POOL_SIZE;
import static org.jboss.resteasy.reactive.client.api.QuarkusRestClientProperties.CONNECTION_TTL;
import static org.jboss.resteasy.reactive.client.api.QuarkusRestClientProperties.CONNECT_TIMEOUT;
Expand Down Expand Up @@ -201,10 +202,19 @@ public Vertx get() {
});
}

handlerChain = new HandlerChain(options.getMaxChunkSize(), followRedirects, loggingScope,
handlerChain = new HandlerChain(isCaptureStacktrace(configuration), options.getMaxChunkSize(), followRedirects,
loggingScope,
clientContext.getMultipartResponsesData(), clientLogger);
}

private boolean isCaptureStacktrace(ConfigurationImpl configuration) {
Object captureStacktraceObj = configuration.getProperty(CAPTURE_STACKTRACE);
if (captureStacktraceObj == null) {
return false;
}
return (boolean) captureStacktraceObj;
}

public ClientContext getClientContext() {
return clientContext;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ class HandlerChain {

private ClientRestHandler preClientSendHandler = null;

public HandlerChain(int maxChunkSize, boolean followRedirects, LoggingScope loggingScope,
public HandlerChain(boolean captureStacktrace, int maxChunkSize, boolean followRedirects, LoggingScope loggingScope,
Map<Class<?>, MultipartResponseData> multipartData, ClientLogger clientLogger) {
this.clientCaptureCurrentContextRestHandler = new ClientCaptureCurrentContextRestHandler();
this.clientCaptureCurrentContextRestHandler = new ClientCaptureCurrentContextRestHandler(captureStacktrace);
this.clientSwitchToRequestContextRestHandler = new ClientSwitchToRequestContextRestHandler();
this.clientSendHandler = new ClientSendRequestHandler(maxChunkSize, followRedirects, loggingScope, clientLogger,
multipartData);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class HandlerChainTest {
@Test
public void preSendHandlerIsAlwaysFirst() throws Exception {

var chain = new HandlerChain(8096, true, LoggingScope.NONE, Collections.emptyMap(), new DefaultClientLogger());
var chain = new HandlerChain(false, 8096, true, LoggingScope.NONE, Collections.emptyMap(), new DefaultClientLogger());

ClientRestHandler preHandler = ctx -> {
};
Expand Down

0 comments on commit 9b08ab6

Please sign in to comment.