Skip to content

Commit 3ecd34d

Browse files
Merge branch '2.2.x'
2 parents 965d22f + f31df6b commit 3ecd34d

File tree

2 files changed

+30
-0
lines changed
  • spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/instrument/web
  • tests/spring-cloud-sleuth-instrumentation-webflux-tests/src/test/java/org/springframework/cloud/sleuth/instrument/web

2 files changed

+30
-0
lines changed

spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/instrument/web/TraceWebFilter.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,14 @@ public ServerHttpRequest unwrap() {
351351

352352
@Override
353353
public boolean parseClientIpAndPort(Span span) {
354+
boolean clientIpAndPortParsed = super.parseClientIpAndPort(span);
355+
if (clientIpAndPortParsed) {
356+
return true;
357+
}
358+
return resolveFromInetAddress(span);
359+
}
360+
361+
private boolean resolveFromInetAddress(Span span) {
354362
InetSocketAddress addr = delegate.getRemoteAddress();
355363
if (addr == null) {
356364
return false;

tests/spring-cloud-sleuth-instrumentation-webflux-tests/src/test/java/org/springframework/cloud/sleuth/instrument/web/TraceWebFluxTests.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,11 @@ public void should_instrument_web_filter() throws Exception {
9292
// then
9393
thenNoSpanWasReported(spans, skippedPatternResponse, controller2);
9494

95+
// when (issue #1683)
96+
response = whenRequestWithXForwardedForIsSent(port, "/api/fn/20");
97+
// then
98+
thenSpanWasReportedWithRemoteIpTags(spans, response);
99+
95100
// cleanup
96101
context.close();
97102
}
@@ -109,6 +114,15 @@ private void thenSpanWasReportedWithTags(TestSpanHandler spans,
109114
then(spans.get(0).name()).isEqualTo("GET /api/c2/{id}");
110115
then(spans.get(0).tags()).containsEntry("mvc.controller.method", "successful")
111116
.containsEntry("mvc.controller.class", "Controller2");
117+
then(spans.get(0).remoteIp()).isEqualTo("127.0.0.1");
118+
}
119+
120+
private void thenSpanWasReportedWithRemoteIpTags(TestSpanHandler spans,
121+
ClientResponse response) {
122+
Awaitility.await()
123+
.untilAsserted(() -> then(response.statusCode().value()).isEqualTo(200));
124+
then(spans).hasSize(1);
125+
then(spans.get(0).remoteIp()).isEqualTo("203.0.113.195");
112126
}
113127

114128
private void thenFunctionalSpanWasReportedWithTags(TestSpanHandler spans,
@@ -137,6 +151,14 @@ private ClientResponse whenRequestIsSent(int port, String path) {
137151
return exchange.block();
138152
}
139153

154+
private ClientResponse whenRequestWithXForwardedForIsSent(int port, String path) {
155+
Mono<ClientResponse> exchange = WebClient.create().get()
156+
.uri("http://localhost:" + port + path)
157+
.header("X-Forwarded-For", "203.0.113.195, 70.41.3.18, 150.172.238.178")
158+
.exchange();
159+
return exchange.block();
160+
}
161+
140162
private ClientResponse whenRequestIsSentToSkippedPattern(int port) {
141163
Mono<ClientResponse> exchange = WebClient.create().get()
142164
.uri("http://localhost:" + port + "/skipped").exchange();

0 commit comments

Comments
 (0)