Commit f731f6aa authored by Stephane Nicoll's avatar Stephane Nicoll

Restore customization of HTTP trace request headers

This commit restore the use of the `postProcessRequestHeaders` method
when overridden in a custom HttpExchangeTracer implementation.

Closes gh-13924
parent 737b4a27
...@@ -127,8 +127,11 @@ public class HttpExchangeTracer { ...@@ -127,8 +127,11 @@ public class HttpExchangeTracer {
@Override @Override
public Map<String, List<String>> getHeaders() { public Map<String, List<String>> getHeaders() {
return getHeadersIfIncluded(Include.REQUEST_HEADERS, Map<String, List<String>> headers = getHeadersIfIncluded(
this.delegate::getHeaders, this::includedHeader); Include.REQUEST_HEADERS, this.delegate::getHeaders,
this::includedHeader);
postProcessRequestHeaders(headers);
return headers;
} }
private boolean includedHeader(String name) { private boolean includedHeader(String name) {
......
...@@ -29,6 +29,8 @@ import org.junit.Test; ...@@ -29,6 +29,8 @@ import org.junit.Test;
import org.springframework.boot.actuate.trace.http.HttpTrace.Request; import org.springframework.boot.actuate.trace.http.HttpTrace.Request;
import org.springframework.http.HttpHeaders; import org.springframework.http.HttpHeaders;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.BDDMockito.given; import static org.mockito.BDDMockito.given;
...@@ -89,6 +91,19 @@ public class HttpExchangeTracerTests { ...@@ -89,6 +91,19 @@ public class HttpExchangeTracerTests {
assertThat(request.getHeaders()).containsOnlyKeys(HttpHeaders.ACCEPT); assertThat(request.getHeaders()).containsOnlyKeys(HttpHeaders.ACCEPT);
} }
@Test
public void requestHeadersCanBeCustomized() {
MultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
headers.add("to-remove", "test");
headers.add("test", "value");
HttpTrace trace = new RequestHeadersFilterHttpExchangeTracer()
.receivedRequest(createRequest(headers));
Request request = trace.getRequest();
assertThat(request.getHeaders()).containsOnlyKeys("test", "to-add");
assertThat(request.getHeaders().get("test")).containsExactly("value");
assertThat(request.getHeaders().get("to-add")).containsExactly("42");
}
@Test @Test
public void authorizationHeaderIsNotIncludedByDefault() { public void authorizationHeaderIsNotIncludedByDefault() {
HttpTrace trace = new HttpExchangeTracer(EnumSet.of(Include.REQUEST_HEADERS)) HttpTrace trace = new HttpExchangeTracer(EnumSet.of(Include.REQUEST_HEADERS))
...@@ -332,4 +347,19 @@ public class HttpExchangeTracerTests { ...@@ -332,4 +347,19 @@ public class HttpExchangeTracerTests {
return output.toString(); return output.toString();
} }
private static class RequestHeadersFilterHttpExchangeTracer
extends HttpExchangeTracer {
RequestHeadersFilterHttpExchangeTracer() {
super(EnumSet.of(Include.REQUEST_HEADERS));
}
@Override
protected void postProcessRequestHeaders(Map<String, List<String>> headers) {
headers.remove("to-remove");
headers.putIfAbsent("to-add", Collections.singletonList("42"));
}
}
} }
...@@ -1915,7 +1915,8 @@ endpoint and obtain basic information about the last 100 request-response exchan ...@@ -1915,7 +1915,8 @@ endpoint and obtain basic information about the last 100 request-response exchan
[[production-ready-http-tracing-custom]] [[production-ready-http-tracing-custom]]
=== Custom HTTP tracing === Custom HTTP tracing
To customize the items that are included in each trace, use the To customize the items that are included in each trace, use the
`management.trace.http.include` configuration property. `management.trace.http.include` configuration property. For advanced customization,
consider registering your own `HttpExchangeTracer` implementation.
By default, an `InMemoryHttpTraceRepository` that stores traces for the last 100 By default, an `InMemoryHttpTraceRepository` that stores traces for the last 100
request-response exchanges is used. If you need to expand the capacity, you can define request-response exchanges is used. If you need to expand the capacity, you can define
......
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