Skip to content

Commit 1404c2f

Browse files
gavinbunneyargha-c
authored andcommitted
Only capture first line of stacktrace for unexpected RequestAttempt exceptions (#1736)
1 parent b04f9d6 commit 1404c2f

File tree

2 files changed

+45
-2
lines changed

2 files changed

+45
-2
lines changed

zuul-core/src/main/java/com/netflix/zuul/niws/RequestAttempt.java

+7-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import com.fasterxml.jackson.core.JsonProcessingException;
2020
import com.fasterxml.jackson.databind.ObjectMapper;
2121
import com.fasterxml.jackson.databind.node.ObjectNode;
22-
import com.google.common.base.Throwables;
2322
import com.netflix.appinfo.AmazonInfo;
2423
import com.netflix.appinfo.InstanceInfo;
2524
import com.netflix.client.config.IClientConfig;
@@ -340,7 +339,13 @@ public void setException(Throwable t) {
340339
} else {
341340
error = t.getMessage();
342341
exceptionType = t.getClass().getSimpleName();
343-
cause = Throwables.getStackTraceAsString(t);
342+
343+
// for unexpected exceptions, just capture the first line of the stacktrace
344+
// otherwise we risk large stacktraces in memory and metrics systems
345+
StackTraceElement[] stackTraceElements = t.getStackTrace();
346+
if (stackTraceElements.length > 0) {
347+
cause = stackTraceElements[0].toString();
348+
}
344349
}
345350
}
346351
}

zuul-core/src/test/java/com/netflix/zuul/niws/RequestAttemptTest.java

+38
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818

1919
import com.netflix.zuul.exception.OutboundErrorType;
2020
import com.netflix.zuul.netty.connectionpool.OriginConnectException;
21+
import io.netty.handler.codec.http2.DefaultHttp2Connection;
22+
import io.netty.handler.codec.http2.Http2Error;
23+
import io.netty.handler.codec.http2.Http2Exception;
2124
import org.junit.jupiter.api.Test;
2225

2326
import javax.net.ssl.SSLHandshakeException;
@@ -26,6 +29,7 @@
2629

2730
import static org.junit.jupiter.api.Assertions.assertEquals;
2831
import static org.mockito.Mockito.mock;
32+
import static org.mockito.Mockito.spy;
2933
import static org.mockito.Mockito.when;
3034

3135
public class RequestAttemptTest {
@@ -77,4 +81,38 @@ void originConnectExceptionWithCauseNotUnwrapped() {
7781
assertEquals("ORIGIN_CONNECT_ERROR", attempt.getError());
7882
assertEquals("java.lang.RuntimeException: socket failure", attempt.getCause());
7983
}
84+
85+
@Test
86+
void h2ExceptionCauseHandled() {
87+
// mock out a real-ish h2 stream exception
88+
Exception h2Exception = spy(Http2Exception.streamError(
89+
100,
90+
Http2Error.REFUSED_STREAM,
91+
"Cannot create stream 100 greater than Last-Stream-ID 99 from GOAWAY.",
92+
new Object[] {100, 99}));
93+
94+
// mock a stacktrace to ensure we don't actually capture it completely
95+
when(h2Exception.getStackTrace()).thenReturn(new StackTraceElement[] {
96+
new StackTraceElement(
97+
DefaultHttp2Connection.class.getCanonicalName(),
98+
"createStream",
99+
"DefaultHttp2Connection.java",
100+
772),
101+
new StackTraceElement(
102+
DefaultHttp2Connection.class.getCanonicalName(),
103+
"checkNewStreamAllowed",
104+
"DefaultHttp2Connection.java",
105+
902)
106+
});
107+
108+
RequestAttempt attempt = new RequestAttempt(1, null, null, "target", "chosen", 200, null, null, 0, 0, 0);
109+
attempt.setException(h2Exception);
110+
111+
assertEquals("Cannot create stream 100 greater than Last-Stream-ID 99 from GOAWAY.", attempt.getError());
112+
assertEquals("StreamException", attempt.getExceptionType());
113+
114+
assertEquals(
115+
"io.netty.handler.codec.http2.DefaultHttp2Connection.createStream(DefaultHttp2Connection.java:772)",
116+
attempt.getCause());
117+
}
80118
}

0 commit comments

Comments
 (0)