Skip to content

Commit f731f6a

Browse files
committed
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
1 parent 737b4a2 commit f731f6a

File tree

3 files changed

+37
-3
lines changed

3 files changed

+37
-3
lines changed

spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/trace/http/HttpExchangeTracer.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,11 @@ public URI getUri() {
127127

128128
@Override
129129
public Map<String, List<String>> getHeaders() {
130-
return getHeadersIfIncluded(Include.REQUEST_HEADERS,
131-
this.delegate::getHeaders, this::includedHeader);
130+
Map<String, List<String>> headers = getHeadersIfIncluded(
131+
Include.REQUEST_HEADERS, this.delegate::getHeaders,
132+
this::includedHeader);
133+
postProcessRequestHeaders(headers);
134+
return headers;
132135
}
133136

134137
private boolean includedHeader(String name) {

spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/trace/http/HttpExchangeTracerTests.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929

3030
import org.springframework.boot.actuate.trace.http.HttpTrace.Request;
3131
import org.springframework.http.HttpHeaders;
32+
import org.springframework.util.LinkedMultiValueMap;
33+
import org.springframework.util.MultiValueMap;
3234

3335
import static org.assertj.core.api.Assertions.assertThat;
3436
import static org.mockito.BDDMockito.given;
@@ -89,6 +91,19 @@ public void requestHeadersCanBeIncluded() {
8991
assertThat(request.getHeaders()).containsOnlyKeys(HttpHeaders.ACCEPT);
9092
}
9193

94+
@Test
95+
public void requestHeadersCanBeCustomized() {
96+
MultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
97+
headers.add("to-remove", "test");
98+
headers.add("test", "value");
99+
HttpTrace trace = new RequestHeadersFilterHttpExchangeTracer()
100+
.receivedRequest(createRequest(headers));
101+
Request request = trace.getRequest();
102+
assertThat(request.getHeaders()).containsOnlyKeys("test", "to-add");
103+
assertThat(request.getHeaders().get("test")).containsExactly("value");
104+
assertThat(request.getHeaders().get("to-add")).containsExactly("42");
105+
}
106+
92107
@Test
93108
public void authorizationHeaderIsNotIncludedByDefault() {
94109
HttpTrace trace = new HttpExchangeTracer(EnumSet.of(Include.REQUEST_HEADERS))
@@ -332,4 +347,19 @@ private String mixedCase(String input) {
332347
return output.toString();
333348
}
334349

350+
private static class RequestHeadersFilterHttpExchangeTracer
351+
extends HttpExchangeTracer {
352+
353+
RequestHeadersFilterHttpExchangeTracer() {
354+
super(EnumSet.of(Include.REQUEST_HEADERS));
355+
}
356+
357+
@Override
358+
protected void postProcessRequestHeaders(Map<String, List<String>> headers) {
359+
headers.remove("to-remove");
360+
headers.putIfAbsent("to-add", Collections.singletonList("42"));
361+
}
362+
363+
}
364+
335365
}

spring-boot-project/spring-boot-docs/src/main/asciidoc/production-ready-features.adoc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1915,7 +1915,8 @@ endpoint and obtain basic information about the last 100 request-response exchan
19151915
[[production-ready-http-tracing-custom]]
19161916
=== Custom HTTP tracing
19171917
To customize the items that are included in each trace, use the
1918-
`management.trace.http.include` configuration property.
1918+
`management.trace.http.include` configuration property. For advanced customization,
1919+
consider registering your own `HttpExchangeTracer` implementation.
19191920

19201921
By default, an `InMemoryHttpTraceRepository` that stores traces for the last 100
19211922
request-response exchanges is used. If you need to expand the capacity, you can define

0 commit comments

Comments
 (0)