Updates for new HttpTraceRepository
This commit is contained in:
@@ -23,7 +23,7 @@ import org.apache.http.impl.client.CloseableHttpClient;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.actuate.autoconfigure.endpoint.condition.ConditionalOnEnabledEndpoint;
|
||||
import org.springframework.boot.actuate.health.Health;
|
||||
import org.springframework.boot.actuate.trace.TraceRepository;
|
||||
import org.springframework.boot.actuate.web.trace.HttpTraceRepository;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
@@ -163,7 +163,7 @@ public class ZuulProxyAutoConfiguration extends ZuulServerAutoConfiguration {
|
||||
protected static class EndpointConfiguration {
|
||||
|
||||
@Autowired(required = false)
|
||||
private TraceRepository traces;
|
||||
private HttpTraceRepository traces;
|
||||
|
||||
@Bean
|
||||
@ConditionalOnEnabledEndpoint
|
||||
|
||||
@@ -20,26 +20,37 @@ package org.springframework.cloud.netflix.zuul.filters;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.net.URI;
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Enumeration;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
|
||||
import org.springframework.boot.actuate.trace.TraceRepository;
|
||||
import org.springframework.boot.actuate.web.trace.HttpExchangeTracer;
|
||||
import org.springframework.boot.actuate.web.trace.HttpTrace;
|
||||
import org.springframework.boot.actuate.web.trace.HttpTraceRepository;
|
||||
import org.springframework.boot.actuate.web.trace.Include;
|
||||
import org.springframework.boot.actuate.web.trace.TraceableRequest;
|
||||
import org.springframework.util.MultiValueMap;
|
||||
|
||||
import com.netflix.zuul.context.RequestContext;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
/**
|
||||
* @author Spencer Gibb
|
||||
*/
|
||||
public class TraceProxyRequestHelper extends ProxyRequestHelper {
|
||||
|
||||
private TraceRepository traces;
|
||||
private HttpTraceRepository traces;
|
||||
private final HttpExchangeTracer tracer = new HttpExchangeTracer(Include.defaultIncludes());
|
||||
|
||||
public void setTraces(TraceRepository traces) {
|
||||
public void setTraces(HttpTraceRepository traces) {
|
||||
this.traces = traces;
|
||||
}
|
||||
|
||||
@@ -60,19 +71,72 @@ public class TraceProxyRequestHelper extends ProxyRequestHelper {
|
||||
trace.put("request", input);
|
||||
info.put("headers", trace);
|
||||
debugHeaders(headers, input);
|
||||
RequestContext ctx = RequestContext.getCurrentContext();
|
||||
if (shouldDebugBody(ctx)) {
|
||||
HttpServletRequest request = context.getRequest();
|
||||
if (shouldDebugBody(context)) {
|
||||
// Prevent input stream from being read if it needs to go downstream
|
||||
if (requestEntity != null) {
|
||||
debugRequestEntity(info, ctx.getRequest().getInputStream());
|
||||
debugRequestEntity(info, request.getInputStream());
|
||||
}
|
||||
}
|
||||
this.traces.add(info);
|
||||
HttpTrace httpTrace = tracer.receivedRequest(new ServletTraceableRequest(request));
|
||||
this.traces.add(httpTrace);
|
||||
return info;
|
||||
}
|
||||
return info;
|
||||
}
|
||||
|
||||
private class ServletTraceableRequest implements TraceableRequest {
|
||||
private HttpServletRequest request;
|
||||
|
||||
public ServletTraceableRequest(HttpServletRequest request) {
|
||||
|
||||
this.request = request;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getMethod() {
|
||||
return request.getMethod();
|
||||
}
|
||||
|
||||
@Override
|
||||
public URI getUri() {
|
||||
StringBuffer urlBuffer = request.getRequestURL();
|
||||
if (StringUtils.hasText(request.getQueryString())) {
|
||||
urlBuffer.append("?");
|
||||
urlBuffer.append(request.getQueryString());
|
||||
}
|
||||
return URI.create(urlBuffer.toString());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, List<String>> getHeaders() {
|
||||
return extractHeaders();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getRemoteAddress() {
|
||||
return request.getRemoteAddr();
|
||||
}
|
||||
|
||||
private Map<String, List<String>> extractHeaders() {
|
||||
Map<String, List<String>> headers = new LinkedHashMap<>();
|
||||
Enumeration<String> names = request.getHeaderNames();
|
||||
while (names.hasMoreElements()) {
|
||||
String name = names.nextElement();
|
||||
headers.put(name, toList(request.getHeaders(name)));
|
||||
}
|
||||
return headers;
|
||||
}
|
||||
|
||||
private List<String> toList(Enumeration<String> enumeration) {
|
||||
List<String> list = new ArrayList<>();
|
||||
while (enumeration.hasMoreElements()) {
|
||||
list.add(enumeration.nextElement());
|
||||
}
|
||||
return list;
|
||||
}
|
||||
}
|
||||
|
||||
void debugHeaders(MultiValueMap<String, String> headers, Map<String, Object> map) {
|
||||
for (Entry<String, List<String>> entry : headers.entrySet()) {
|
||||
Collection<String> collection = entry.getValue();
|
||||
|
||||
@@ -18,7 +18,6 @@
|
||||
package org.springframework.cloud.netflix.zuul;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.inject.Inject;
|
||||
import javax.servlet.http.Part;
|
||||
@@ -27,8 +26,8 @@ import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.boot.actuate.trace.InMemoryTraceRepository;
|
||||
import org.springframework.boot.actuate.trace.TraceRepository;
|
||||
import org.springframework.boot.actuate.web.trace.HttpTraceRepository;
|
||||
import org.springframework.boot.actuate.web.trace.InMemoryHttpTraceRepository;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
|
||||
@@ -289,16 +288,8 @@ class FormZuulProxyApplication {
|
||||
}
|
||||
|
||||
@Bean
|
||||
public TraceRepository traceRepository() {
|
||||
return new InMemoryTraceRepository() {
|
||||
@Override
|
||||
public void add(Map<String, Object> map) {
|
||||
if (map.containsKey("body")) {
|
||||
map.get("body");
|
||||
}
|
||||
super.add(map);
|
||||
}
|
||||
};
|
||||
public HttpTraceRepository traceRepository() {
|
||||
return new InMemoryHttpTraceRepository();
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
@@ -19,7 +19,6 @@ package org.springframework.cloud.netflix.zuul;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
@@ -28,8 +27,8 @@ import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.actuate.trace.InMemoryTraceRepository;
|
||||
import org.springframework.boot.actuate.trace.TraceRepository;
|
||||
import org.springframework.boot.actuate.web.trace.HttpTraceRepository;
|
||||
import org.springframework.boot.actuate.web.trace.InMemoryHttpTraceRepository;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.boot.test.web.client.TestRestTemplate;
|
||||
@@ -205,16 +204,8 @@ class FormZuulServletProxyApplication {
|
||||
}
|
||||
|
||||
@Bean
|
||||
public TraceRepository traceRepository() {
|
||||
return new InMemoryTraceRepository() {
|
||||
@Override
|
||||
public void add(Map<String, Object> map) {
|
||||
if (map.containsKey("body")) {
|
||||
map.get("body");
|
||||
}
|
||||
super.add(map);
|
||||
}
|
||||
};
|
||||
public HttpTraceRepository traceRepository() {
|
||||
return new InMemoryHttpTraceRepository();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -21,14 +21,15 @@ import java.util.List;
|
||||
|
||||
import com.netflix.zuul.context.RequestContext;
|
||||
|
||||
import org.assertj.core.api.Assertions;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.mockito.Mock;
|
||||
|
||||
import org.springframework.boot.actuate.trace.InMemoryTraceRepository;
|
||||
import org.springframework.boot.actuate.trace.Trace;
|
||||
import org.springframework.boot.actuate.trace.TraceRepository;
|
||||
import org.springframework.boot.actuate.web.trace.HttpTrace;
|
||||
import org.springframework.boot.actuate.web.trace.HttpTraceRepository;
|
||||
import org.springframework.boot.actuate.web.trace.InMemoryHttpTraceRepository;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.mock.web.MockHttpServletRequest;
|
||||
@@ -53,7 +54,7 @@ import static org.springframework.cloud.netflix.zuul.filters.support.FilterConst
|
||||
public class ProxyRequestHelperTests {
|
||||
|
||||
@Mock
|
||||
private TraceRepository traceRepository;
|
||||
private HttpTraceRepository traceRepository;
|
||||
|
||||
@Before
|
||||
public void init() {
|
||||
@@ -81,16 +82,15 @@ public class ProxyRequestHelperTests {
|
||||
RequestContext.getCurrentContext().setRequest(request);
|
||||
|
||||
TraceProxyRequestHelper helper = new TraceProxyRequestHelper();
|
||||
this.traceRepository = new InMemoryTraceRepository();
|
||||
this.traceRepository = new InMemoryHttpTraceRepository();
|
||||
helper.setTraces(this.traceRepository);
|
||||
|
||||
MultiValueMap<String, String> headers = helper.buildZuulRequestHeaders(request);
|
||||
|
||||
helper.debug("POST", "http://example.com", headers,
|
||||
new LinkedMultiValueMap<String, String>(), request.getInputStream());
|
||||
Trace actual = this.traceRepository.findAll().get(0);
|
||||
assertThat((String) actual.getInfo().get("body"), equalTo("{}"));
|
||||
|
||||
new LinkedMultiValueMap<>(), request.getInputStream());
|
||||
HttpTrace actual = this.traceRepository.findAll().get(0);
|
||||
Assertions.assertThat(actual.getRequest().getHeaders()).containsKeys("singleName", "multiName");
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -24,7 +24,9 @@ import org.springframework.context.annotation.Import;
|
||||
import org.springframework.core.Ordered;
|
||||
import org.springframework.core.annotation.Order;
|
||||
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
||||
import org.springframework.security.config.annotation.web.builders.WebSecurity;
|
||||
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
|
||||
import org.springframework.security.web.firewall.StrictHttpFirewall;
|
||||
|
||||
/**
|
||||
* @author Spencer Gibb
|
||||
@@ -43,6 +45,12 @@ public class TestAutoConfiguration {
|
||||
super(true);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void configure(WebSecurity web) throws Exception {
|
||||
StrictHttpFirewall httpFirewall = new StrictHttpFirewall();
|
||||
httpFirewall.setAllowSemicolon(true);
|
||||
web.httpFirewall(httpFirewall);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void configure(HttpSecurity http) throws Exception {
|
||||
|
||||
Reference in New Issue
Block a user