Commit 403bd8d7 by Dave Syer

Move debug logging to TRaceRepository

parent ea80ac17
package org.springframework.platform.netflix.zuul;
import com.netflix.zuul.context.ContextLifecycleFilter;
import com.netflix.zuul.http.ZuulServlet;
import java.util.ArrayList;
import java.util.Collection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.actuate.trace.TraceRepository;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.boot.context.embedded.FilterRegistrationBean;
......@@ -11,14 +13,12 @@ import org.springframework.boot.context.properties.EnableConfigurationProperties
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.platform.netflix.zuul.filters.post.SendResponseFilter;
import org.springframework.platform.netflix.zuul.filters.post.StatsFilter;
import org.springframework.platform.netflix.zuul.filters.pre.DebugFilter;
import org.springframework.platform.netflix.zuul.filters.pre.DebugRequestFilter;
import org.springframework.platform.netflix.zuul.filters.pre.PreDecorationFilter;
import org.springframework.platform.netflix.zuul.filters.route.RibbonRoutingFilter;
import java.util.ArrayList;
import java.util.Collection;
import com.netflix.zuul.context.ContextLifecycleFilter;
import com.netflix.zuul.http.ZuulServlet;
/**
* @author Spencer Gibb
......@@ -32,6 +32,9 @@ public class ZuulProxyConfiguration {
@Autowired
private ZuulProxyProperties props;
@Autowired(required=false)
private TraceRepository traces;
@Bean
public FilterRegistrationBean contextLifecycleFilter() {
Collection<String> urlPatterns = new ArrayList<>();
......@@ -65,11 +68,6 @@ public class ZuulProxyConfiguration {
}
@Bean
public DebugRequestFilter debugRequestFilter() {
return new DebugRequestFilter();
}
@Bean
public PreDecorationFilter preDecorationFilter() {
return new PreDecorationFilter();
}
......@@ -77,7 +75,11 @@ public class ZuulProxyConfiguration {
// route filters
@Bean
public RibbonRoutingFilter ribbonRoutingFilter() {
return new RibbonRoutingFilter();
RibbonRoutingFilter filter = new RibbonRoutingFilter();
if (traces!=null) {
filter.setTraces(traces);
}
return filter;
}
// post filters
......@@ -86,9 +88,4 @@ public class ZuulProxyConfiguration {
return new SendResponseFilter();
}
@Bean
public StatsFilter statsFilter() {
return new StatsFilter();
}
}
package org.springframework.platform.netflix.zuul.filters.post;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
import java.util.zip.GZIPInputStream;
import javax.servlet.http.HttpServletResponse;
import com.google.common.base.Throwables;
import com.netflix.config.DynamicBooleanProperty;
import com.netflix.config.DynamicIntProperty;
......@@ -8,27 +17,21 @@ import com.netflix.util.Pair;
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.constants.ZuulConstants;
import com.netflix.zuul.constants.ZuulHeaders;
import com.netflix.zuul.context.Debug;
import com.netflix.zuul.context.RequestContext;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
import java.util.zip.GZIPInputStream;
public class SendResponseFilter extends ZuulFilter {
static DynamicBooleanProperty INCLUDE_DEBUG_HEADER =
DynamicPropertyFactory.getInstance().getBooleanProperty(ZuulConstants.ZUUL_INCLUDE_DEBUG_HEADER, false);
static DynamicBooleanProperty INCLUDE_DEBUG_HEADER = DynamicPropertyFactory
.getInstance().getBooleanProperty(ZuulConstants.ZUUL_INCLUDE_DEBUG_HEADER,
false);
static DynamicIntProperty INITIAL_STREAM_BUFFER_SIZE =
DynamicPropertyFactory.getInstance().getIntProperty(ZuulConstants.ZUUL_INITIAL_STREAM_BUFFER_SIZE, 1024);
static DynamicIntProperty INITIAL_STREAM_BUFFER_SIZE = DynamicPropertyFactory
.getInstance().getIntProperty(ZuulConstants.ZUUL_INITIAL_STREAM_BUFFER_SIZE,
1024);
static DynamicBooleanProperty SET_CONTENT_LENGTH = DynamicPropertyFactory.getInstance().getBooleanProperty(ZuulConstants.ZUUL_SET_CONTENT_LENGTH, false);
static DynamicBooleanProperty SET_CONTENT_LENGTH = DynamicPropertyFactory
.getInstance().getBooleanProperty(ZuulConstants.ZUUL_SET_CONTENT_LENGTH,
false);
@Override
public String filterType() {
......@@ -41,16 +44,17 @@ public class SendResponseFilter extends ZuulFilter {
}
public boolean shouldFilter() {
return !RequestContext.getCurrentContext().getZuulResponseHeaders().isEmpty() ||
RequestContext.getCurrentContext().getResponseDataStream() != null ||
RequestContext.getCurrentContext().getResponseBody() != null;
return !RequestContext.getCurrentContext().getZuulResponseHeaders().isEmpty()
|| RequestContext.getCurrentContext().getResponseDataStream() != null
|| RequestContext.getCurrentContext().getResponseBody() != null;
}
public Object run() {
try {
addResponseHeaders();
writeResponse();
} catch (Exception e) {
}
catch (Exception e) {
Throwables.propagate(e);
}
return null;
......@@ -60,7 +64,8 @@ public class SendResponseFilter extends ZuulFilter {
RequestContext context = RequestContext.getCurrentContext();
// there is no body to send
if (context.getResponseBody() == null && context.getResponseDataStream() == null) return;
if (context.getResponseBody() == null && context.getResponseDataStream() == null)
return;
HttpServletResponse servletResponse = context.getResponse();
servletResponse.setCharacterEncoding("UTF-8");
......@@ -75,7 +80,8 @@ public class SendResponseFilter extends ZuulFilter {
}
boolean isGzipRequested = false;
final String requestEncoding = context.getRequest().getHeader(ZuulHeaders.ACCEPT_ENCODING);
final String requestEncoding = context.getRequest().getHeader(
ZuulHeaders.ACCEPT_ENCODING);
if (requestEncoding != null && requestEncoding.equals("gzip"))
isGzipRequested = true;
......@@ -83,16 +89,21 @@ public class SendResponseFilter extends ZuulFilter {
InputStream inputStream = is;
if (is != null) {
if (context.sendZuulResponse()) {
// if origin response is gzipped, and client has not requested gzip, decompress stream
// if origin response is gzipped, and client has not requested gzip,
// decompress stream
// before sending to client
// else, stream gzip directly to client
if (context.getResponseGZipped() && !isGzipRequested)
try {
inputStream = new GZIPInputStream(is);
} catch (java.util.zip.ZipException e) {
System.out.println("gzip expected but not received assuming unencoded response" +
RequestContext.getCurrentContext().getRequest().getRequestURL().toString());
}
catch (java.util.zip.ZipException e) {
System.out
.println("gzip expected but not received assuming unencoded response"
+ RequestContext.getCurrentContext()
.getRequest().getRequestURL()
.toString());
inputStream = is;
}
else if (context.getResponseGZipped() && isGzipRequested)
......@@ -101,14 +112,16 @@ public class SendResponseFilter extends ZuulFilter {
}
}
} finally {
}
finally {
try {
if (is != null)
is.close();
outStream.flush();
outStream.close();
} catch (IOException e) {
}
catch (IOException e) {
}
}
......@@ -118,15 +131,16 @@ public class SendResponseFilter extends ZuulFilter {
byte[] bytes = new byte[INITIAL_STREAM_BUFFER_SIZE.get()];
int bytesRead = -1;
while ((bytesRead = zin.read(bytes)) != -1) {
// if (Debug.debugRequest() && !Debug.debugRequestHeadersOnly()) {
// Debug.addRequestDebug("OUTBOUND: < " + new String(bytes, 0, bytesRead));
// }
// if (Debug.debugRequest() && !Debug.debugRequestHeadersOnly()) {
// Debug.addRequestDebug("OUTBOUND: < " + new String(bytes, 0, bytesRead));
// }
try {
out.write(bytes, 0, bytesRead);
out.flush();
} catch (IOException e) {
//ignore
}
catch (IOException e) {
// ignore
e.printStackTrace();
}
......@@ -141,29 +155,20 @@ public class SendResponseFilter extends ZuulFilter {
RequestContext context = RequestContext.getCurrentContext();
HttpServletResponse servletResponse = context.getResponse();
List<Pair<String, String>> zuulResponseHeaders = context.getZuulResponseHeaders();
String debugHeader = "";
@SuppressWarnings("unchecked")
List<String> rd = (List<String>) RequestContext.getCurrentContext().get("routingDebug");
List<String> rd = (List<String>) RequestContext.getCurrentContext().get(
"routingDebug");
if (rd != null) {
StringBuilder debugHeader = new StringBuilder();
for (String it : rd) {
debugHeader += "[[["+it+"]]]";
debugHeader.append("[[[" + it + "]]]");
}
/*
rd = (List<String>) RequestContext.getCurrentContext().get("requestDebug");
rd?.each {
debugHeader += "[[[REQUEST_DEBUG::${it}]]]";
if (INCLUDE_DEBUG_HEADER.get())
servletResponse.addHeader("X-Zuul-Debug-Header", debugHeader.toString());
}
*/
if (INCLUDE_DEBUG_HEADER.get()) servletResponse.addHeader("X-Zuul-Debug-Header", debugHeader);
if (Debug.debugRequest() && zuulResponseHeaders != null) {
for (Pair<String, String> it : zuulResponseHeaders) {
servletResponse.addHeader(it.first(), it.second());
Debug.addRequestDebug("OUTBOUND: < " + it.first() + ":" + it.second());
}
} else if (zuulResponseHeaders != null) {
if (zuulResponseHeaders != null) {
for (Pair<String, String> it : zuulResponseHeaders) {
servletResponse.addHeader(it.first(), it.second());
}
......@@ -172,7 +177,8 @@ public class SendResponseFilter extends ZuulFilter {
RequestContext ctx = RequestContext.getCurrentContext();
Integer contentLength = ctx.getOriginContentLength();
// only inserts Content-Length if origin provides it and origin response is not gzipped
// Only inserts Content-Length if origin provides it and origin response is not
// gzipped
if (SET_CONTENT_LENGTH.get()) {
if (contentLength != null && !ctx.getResponseGZipped())
servletResponse.setContentLength(contentLength);
......
package org.springframework.platform.netflix.zuul.filters.post;
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import java.util.List;
public class StatsFilter extends ZuulFilter {
@Override
public String filterType() {
return "post";
}
@Override
public int filterOrder() {
return 2000;
}
@Override
public boolean shouldFilter() {
return true;
}
@Override
public Object run() {
dumpRoutingDebug();
dumpRequestDebug();
return null;
}
public void dumpRequestDebug() {
@SuppressWarnings("unchecked")
List<String> rd = (List<String>) RequestContext.getCurrentContext().get("requestDebug");
if (rd != null) {
for (String it : rd) {
System.out.println("REQUEST_DEBUG::" + it);
}
}
}
public void dumpRoutingDebug() {
@SuppressWarnings("unchecked")
List<String> rd = (List<String>) RequestContext.getCurrentContext().get("routingDebug");
if (rd != null) {
for (String it : rd) {
System.out.println("ZUUL_DEBUG::"+it);
}
}
}
}
......@@ -12,9 +12,9 @@ import javax.servlet.http.HttpServletRequest;
public class DebugFilter extends ZuulFilter {
static final DynamicBooleanProperty routingDebug = DynamicPropertyFactory.getInstance()
.getBooleanProperty(ZuulConstants.ZUUL_DEBUG_REQUEST, true);
.getBooleanProperty(ZuulConstants.ZUUL_DEBUG_REQUEST, false);
static final DynamicStringProperty debugParameter = DynamicPropertyFactory.getInstance()
.getStringProperty(ZuulConstants.ZUUL_DEBUG_PARAMETER, "d");
.getStringProperty(ZuulConstants.ZUUL_DEBUG_PARAMETER, "debug");
@Override
public String filterType() {
......
package org.springframework.platform.netflix.zuul.filters.pre;
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.Debug;
import com.netflix.zuul.context.RequestContext;
import org.apache.commons.io.IOUtils;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
public class DebugRequestFilter extends ZuulFilter {
@Override
public String filterType() {
return "pre";
}
@Override
public int filterOrder() {
return 10000;
}
@Override
public boolean shouldFilter() {
return Debug.debugRequest();
}
@Override
public Object run() {
HttpServletRequest req = RequestContext.getCurrentContext().getRequest();
Debug.addRequestDebug("REQUEST:: " + req.getScheme() + " " + req.getRemoteAddr() + ":" + req.getRemotePort());
Debug.addRequestDebug("REQUEST:: > " + req.getMethod() + " " + req.getRequestURI() + " " + req.getProtocol());
Enumeration<String> headerIt = req.getHeaderNames();
while (headerIt.hasMoreElements()) {
String name = headerIt.nextElement();
String value = req.getHeader(name);
Debug.addRequestDebug("REQUEST:: > " + name + ":" + value);
}
final RequestContext ctx = RequestContext.getCurrentContext();
if (!ctx.isChunkedRequestBody()) {
try {
InputStream inp = ctx.getRequest().getInputStream();
if (inp != null) {
String body = IOUtils.toString(inp);
Debug.addRequestDebug("REQUEST:: > " + body);
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return null;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment