Skip to content

Commit 743df7b

Browse files
author
Mateusz Rzeszutek
authored
Convert Spring Webflux client tests to Java; add library instrumentat… (#7566)
…ion tests
1 parent 1f0ae6a commit 743df7b

File tree

13 files changed

+383
-204
lines changed

13 files changed

+383
-204
lines changed

instrumentation/spring/spring-webflux-5.0/javaagent/build.gradle.kts

+1-4
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,7 @@ dependencies {
5151
testInstrumentation(project(":instrumentation:reactor:reactor-3.1:javaagent"))
5252
testInstrumentation(project(":instrumentation:reactor:reactor-netty:reactor-netty-1.0:javaagent"))
5353

54-
// Compile with both old and new netty packages since our test references both for old and
55-
// latest dep tests.
56-
testCompileOnly("io.projectreactor.ipc:reactor-netty:0.7.0.RELEASE")
57-
testCompileOnly("io.projectreactor.netty:reactor-netty-http:1.0.7")
54+
testImplementation(project(":instrumentation:spring:spring-webflux-5.0:testing"))
5855

5956
testLibrary("org.springframework.boot:spring-boot-starter-webflux:2.0.0.RELEASE")
6057
testLibrary("org.springframework.boot:spring-boot-starter-test:2.0.0.RELEASE")

instrumentation/spring/spring-webflux-5.0/javaagent/src/test/groovy/client/SpringWebFluxSingleConnection.groovy

-74
This file was deleted.

instrumentation/spring/spring-webflux-5.0/javaagent/src/test/groovy/client/SpringWebfluxHttpClientTest.groovy

-114
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package io.opentelemetry.javaagent.instrumentation.spring.webflux.client;
7+
8+
import io.opentelemetry.instrumentation.spring.webflux.client.AbstractSpringWebfluxClientInstrumentationTest;
9+
import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension;
10+
import io.opentelemetry.instrumentation.testing.junit.http.HttpClientInstrumentationExtension;
11+
import org.junit.jupiter.api.extension.RegisterExtension;
12+
import org.springframework.web.reactive.function.client.WebClient;
13+
14+
class SpringWebfluxClientInstrumentationTest
15+
extends AbstractSpringWebfluxClientInstrumentationTest {
16+
17+
@RegisterExtension
18+
static final InstrumentationExtension testing = HttpClientInstrumentationExtension.forAgent();
19+
20+
@Override
21+
protected WebClient.Builder instrument(WebClient.Builder builder) {
22+
return builder;
23+
}
24+
}

instrumentation/spring/spring-webflux-5.0/library/build.gradle.kts

+17-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,22 @@ plugins {
33
}
44

55
dependencies {
6-
compileOnly("org.springframework:spring-webflux:5.0.0.RELEASE")
6+
library("org.springframework:spring-webflux:5.0.0.RELEASE")
7+
78
compileOnly("io.projectreactor.ipc:reactor-netty:0.7.0.RELEASE")
9+
10+
testImplementation(project(":instrumentation:spring:spring-webflux-5.0:testing"))
11+
12+
testLibrary("org.springframework.boot:spring-boot-starter-webflux:2.0.0.RELEASE")
13+
testLibrary("org.springframework.boot:spring-boot-starter-test:2.0.0.RELEASE")
14+
testLibrary("org.springframework.boot:spring-boot-starter-reactor-netty:2.0.0.RELEASE")
15+
}
16+
17+
val latestDepTest = findProperty("testLatestDeps") as Boolean
18+
19+
// spring 6 (which spring-kafka 3.+ uses) requires java 17
20+
if (latestDepTest) {
21+
otelJava {
22+
minJavaVersionSupported.set(JavaVersion.VERSION_17)
23+
}
824
}

instrumentation/spring/spring-webflux-5.0/library/src/main/java/io/opentelemetry/instrumentation/spring/webflux/client/TraceWebClientSubscriber.java

+11-10
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,21 @@ final class TraceWebClientSubscriber implements CoreSubscriber<ClientResponse> {
2323
private final ClientRequest request;
2424
private final CoreSubscriber<? super ClientResponse> actual;
2525
private final reactor.util.context.Context reactorContext;
26-
private final io.opentelemetry.context.Context otelContext;
26+
private final io.opentelemetry.context.Context otelClientContext;
27+
private final io.opentelemetry.context.Context otelParentContext;
2728

2829
TraceWebClientSubscriber(
2930
Instrumenter<ClientRequest, ClientResponse> instrumenter,
3031
ClientRequest request,
3132
CoreSubscriber<? super ClientResponse> actual,
32-
Context otelContext) {
33+
Context otelClientContext,
34+
Context otelParentContext) {
3335
this.instrumenter = instrumenter;
3436
this.request = request;
3537
this.actual = actual;
36-
this.otelContext = otelContext;
3738
this.reactorContext = actual.currentContext();
39+
this.otelClientContext = otelClientContext;
40+
this.otelParentContext = otelParentContext;
3841
}
3942

4043
@Override
@@ -44,25 +47,23 @@ public void onSubscribe(Subscription subscription) {
4447

4548
@Override
4649
public void onNext(ClientResponse response) {
47-
try (Scope ignored = otelContext.makeCurrent()) {
50+
instrumenter.end(otelClientContext, request, response, null);
51+
try (Scope ignored = otelParentContext.makeCurrent()) {
4852
this.actual.onNext(response);
49-
} finally {
50-
instrumenter.end(otelContext, request, response, null);
5153
}
5254
}
5355

5456
@Override
5557
public void onError(Throwable t) {
56-
try (Scope ignored = otelContext.makeCurrent()) {
58+
instrumenter.end(otelClientContext, request, null, t);
59+
try (Scope ignored = otelParentContext.makeCurrent()) {
5760
this.actual.onError(t);
58-
} finally {
59-
instrumenter.end(otelContext, request, null, t);
6061
}
6162
}
6263

6364
@Override
6465
public void onComplete() {
65-
try (Scope ignored = otelContext.makeCurrent()) {
66+
try (Scope ignored = otelParentContext.makeCurrent()) {
6667
this.actual.onComplete();
6768
}
6869
}

instrumentation/spring/spring-webflux-5.0/library/src/main/java/io/opentelemetry/instrumentation/spring/webflux/client/WebClientTracingFilter.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,9 @@ public void subscribe(CoreSubscriber<? super ClientResponse> subscriber) {
6565
.doOnCancel(
6666
// no response and no error means that the request has been cancelled
6767
() -> instrumenter.end(context, request, null, null))
68-
.subscribe(new TraceWebClientSubscriber(instrumenter, request, subscriber, context));
68+
.subscribe(
69+
new TraceWebClientSubscriber(
70+
instrumenter, request, subscriber, context, parentContext));
6971
}
7072
}
7173
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package io.opentelemetry.instrumentation.spring.webflux.client;
7+
8+
import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension;
9+
import io.opentelemetry.instrumentation.testing.junit.http.HttpClientInstrumentationExtension;
10+
import org.junit.jupiter.api.extension.RegisterExtension;
11+
import org.springframework.web.reactive.function.client.WebClient;
12+
13+
class SpringWebfluxClientInstrumentationTest
14+
extends AbstractSpringWebfluxClientInstrumentationTest {
15+
16+
@RegisterExtension
17+
static final InstrumentationExtension testing = HttpClientInstrumentationExtension.forLibrary();
18+
19+
@Override
20+
protected WebClient.Builder instrument(WebClient.Builder builder) {
21+
SpringWebfluxTelemetry instrumentation =
22+
SpringWebfluxTelemetry.create(testing.getOpenTelemetry());
23+
return builder.filters(instrumentation::addClientTracingFilter);
24+
}
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
plugins {
2+
id("otel.java-conventions")
3+
}
4+
5+
dependencies {
6+
implementation(project(":testing-common"))
7+
8+
compileOnly("org.springframework:spring-webflux:5.0.0.RELEASE")
9+
10+
// Compile with both old and new netty packages since our test references both for old and
11+
// latest dep tests.
12+
compileOnly("io.projectreactor.ipc:reactor-netty:0.7.0.RELEASE")
13+
compileOnly("io.projectreactor.netty:reactor-netty-http:1.0.7")
14+
}

0 commit comments

Comments
 (0)