Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* specific language governing permissions and limitations
* under the License.
*/
package co.elastic.apm.agent.springwebmvc;
package co.elastic.apm.agent.testutils;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.EnabledForJreRange;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,18 @@
<animal.sniffer.skip>true</animal.sniffer.skip>
</properties>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${version.spring-boot-3}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<dependencies>
<dependency>
<groupId>co.elastic.apm</groupId>
Expand All @@ -29,13 +41,23 @@
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${version.spring}</version>
<scope>provided</scope>
<exclusions>
<exclusion>
<!--
jcl needs to be excluded because of the integration tests.
jcl seems to provide apache commons logging classes, but compiled with java 17.
Wiremock does some reflective lookup on these classes, which causes the tests to fail with Java 11.
As a workaround, jcl has been excluded and commons-logging has been included as test dependency instead
-->
<groupId>org.springframework</groupId>
<artifactId>spring-jcl</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webflux</artifactId>
<version>${version.spring}</version>
<scope>provided</scope>
</dependency>

Expand All @@ -51,27 +73,40 @@
<dependency>
<groupId>io.projectreactor.netty</groupId>
<artifactId>reactor-netty-http</artifactId>
<version>1.0.7</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-resolver-dns-native-macos</artifactId>
<classifier>osx-aarch_64</classifier>
<scope>test</scope>
</dependency>
<!-- alternative client implementation: jetty -->
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-reactive-httpclient</artifactId>
<version>1.1.11</version>
<scope>test</scope>
</dependency>
<!-- alternative client implementation: apache async client -->
<dependency>
<groupId>org.apache.httpcomponents.client5</groupId>
<artifactId>httpclient5</artifactId>
<version>5.1.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents.core5</groupId>
<artifactId>httpcore5-reactive</artifactId>
<version>5.1.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.ivy</groupId>
<artifactId>ivy</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
<scope>test</scope>
</dependency>
</dependencies>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package co.elastic.apm.agent.springwebclient;

import co.elastic.apm.agent.httpclient.AbstractHttpClientInstrumentationTest;
import co.elastic.apm.agent.testutils.JUnit4TestClassWithDependencyRunner;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.springframework.http.client.reactive.ReactorClientHttpConnector;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.netty.http.client.HttpClient;

import java.util.Arrays;
import java.util.List;

public class WebClientInstrumentationIT {


@ParameterizedTest
@ValueSource(strings = {"5.3.26"})
public void testNetty(String springVersion) throws Exception {
List<String> dependencies = Arrays.asList(
"org.springframework:spring-web:" + springVersion,
"org.springframework:spring-webflux:" + springVersion,
"org.springframework:spring-core:" + springVersion,
"org.springframework:spring-beans:" + springVersion
);
JUnit4TestClassWithDependencyRunner runner = new JUnit4TestClassWithDependencyRunner(dependencies, WebClientInstrumentationIT.class.getName() + "$TestImpl", WebClientInstrumentationIT.class.getName());
runner.run();
}

/**
* We don't test with all variations of {@link WebClientInstrumentationTest}
* but just with netty for integration test.
*/
public static class TestImpl extends AbstractHttpClientInstrumentationTest {

private final WebClient webClient;


public TestImpl() {
HttpClient httpClient = HttpClient.create()
// followRedirect(boolean) only enables redirect for 30[1278], not 303
.followRedirect((req, res) -> res.status().code() == 303);

// crete netty reactor client
webClient = WebClient.builder()
.clientConnector(new ReactorClientHttpConnector(httpClient))
.build();
}

@Override
public boolean isRequireCheckErrorWhenCircularRedirect() {
// circular redirect does not trigger an error to capture with netty
return false;
}

@Override
public boolean isTestHttpCallWithUserInfoEnabled() {
// user info URI does not work with netty
return false;
}


@Override
protected void performGet(String path) throws Exception {
webClient.get().uri(path).exchangeToMono(response -> response.bodyToMono(String.class)).block();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/
package co.elastic.apm.agent.springwebclient;

import co.elastic.apm.agent.common.JvmRuntimeInfo;
import co.elastic.apm.agent.httpclient.AbstractHttpClientInstrumentationTest;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
Expand All @@ -30,34 +31,41 @@
@RunWith(Parameterized.class)
public class WebClientInstrumentationTest extends AbstractHttpClientInstrumentationTest {

private final WebClient webClient;
/**
* Can't directly reference WebClient because it is compiled with java 17.
*/
private final Object webClient;

private final RequestStrategy strategy;

private final boolean isNetty;

public WebClientInstrumentationTest(String clientIgnored, WebClient webClient, RequestStrategy strategy, boolean isNetty) {
public WebClientInstrumentationTest(String clientIgnored, Object webClient, RequestStrategy strategy, boolean isNetty) {
this.webClient = webClient;
this.strategy = strategy;
this.isNetty = isNetty;
}

@Parameterized.Parameters(name = "client = {0}, request strategy = {2}")
public static Object[][] testParams() {
return new Object[][]{
{"jetty", jettyClient(), RequestStrategy.EXCHANGE, false},
{"jetty", jettyClient(), RequestStrategy.EXCHANGE_TO_FLUX, false},
{"jetty", jettyClient(), RequestStrategy.EXCHANGE_TO_MONO, false},
{"jetty", jettyClient(), RequestStrategy.RETRIEVE, false},
{"netty", nettyClient(), RequestStrategy.EXCHANGE, true},
{"netty", nettyClient(), RequestStrategy.EXCHANGE_TO_FLUX, true},
{"netty", nettyClient(), RequestStrategy.EXCHANGE_TO_MONO, true},
{"netty", nettyClient(), RequestStrategy.RETRIEVE, true},
{"hc5", reactiveHttpClient5(), RequestStrategy.EXCHANGE, false},
{"hc5", reactiveHttpClient5(), RequestStrategy.EXCHANGE_TO_FLUX, false},
{"hc5", reactiveHttpClient5(), RequestStrategy.EXCHANGE_TO_MONO, false},
{"hc5", reactiveHttpClient5(), RequestStrategy.RETRIEVE, false}
};
if (JvmRuntimeInfo.ofCurrentVM().getMajorVersion() >= 17) {
return new Object[][]{
{"jetty", Clients.jettyClient(), RequestStrategy.EXCHANGE, false},
{"jetty", Clients.jettyClient(), RequestStrategy.EXCHANGE_TO_FLUX, false},
{"jetty", Clients.jettyClient(), RequestStrategy.EXCHANGE_TO_MONO, false},
{"jetty", Clients.jettyClient(), RequestStrategy.RETRIEVE, false},
{"netty", Clients.nettyClient(), RequestStrategy.EXCHANGE, true},
{"netty", Clients.nettyClient(), RequestStrategy.EXCHANGE_TO_FLUX, true},
{"netty", Clients.nettyClient(), RequestStrategy.EXCHANGE_TO_MONO, true},
{"netty", Clients.nettyClient(), RequestStrategy.RETRIEVE, true},
{"hc5", Clients.reactiveHttpClient5(), RequestStrategy.EXCHANGE, false},
{"hc5", Clients.reactiveHttpClient5(), RequestStrategy.EXCHANGE_TO_FLUX, false},
{"hc5", Clients.reactiveHttpClient5(), RequestStrategy.EXCHANGE_TO_MONO, false},
{"hc5", Clients.reactiveHttpClient5(), RequestStrategy.RETRIEVE, false}
};
} else {
return new Object[0][0];
}
}

@Override
Expand Down Expand Up @@ -86,55 +94,58 @@ protected enum RequestStrategy {
EXCHANGE {
@Override
@SuppressWarnings("deprecation")
void execute(WebClient client, String uri) {
client.get().uri(uri).exchange() // deprecated API
void execute(Object client, String uri) {
((WebClient) client).get().uri(uri).exchange() // deprecated API
.block();
}
},
EXCHANGE_TO_FLUX {
@Override
void execute(WebClient client, String uri) {
client.get().uri(uri).exchangeToFlux(response -> response.bodyToFlux(String.class)).blockLast();
void execute(Object client, String uri) {
((WebClient) client).get().uri(uri).exchangeToFlux(response -> response.bodyToFlux(String.class)).blockLast();
}
},
EXCHANGE_TO_MONO {
// TODO
@Override
void execute(WebClient client, String uri) {
client.get().uri(uri).exchangeToMono(response -> response.bodyToMono(String.class)).block();
void execute(Object client, String uri) {
((WebClient) client).get().uri(uri).exchangeToMono(response -> response.bodyToMono(String.class)).block();
}
},
RETRIEVE {
@Override
void execute(WebClient client, String uri) {
client.get().uri(uri).retrieve().bodyToMono(String.class).block();
void execute(Object client, String uri) {
((WebClient) client).get().uri(uri).retrieve().bodyToMono(String.class).block();
}
};

abstract void execute(WebClient client, String uri);
}

private static WebClient jettyClient() {
return WebClient.builder()
.clientConnector(new JettyClientHttpConnector())
.build();
}

private static WebClient nettyClient() {
HttpClient httpClient = HttpClient.create()
// followRedirect(boolean) only enables redirect for 30[1278], not 303
.followRedirect((req, res) -> res.status().code() == 303);

// crete netty reactor client
return WebClient.builder()
.clientConnector(new ReactorClientHttpConnector(httpClient))
.build();
abstract void execute(Object client, String uri);
}

public static WebClient reactiveHttpClient5() {
return WebClient.builder()
.clientConnector(new HttpComponentsClientHttpConnector())
.build();
public static class Clients {

private static Object jettyClient() {
return WebClient.builder()
.clientConnector(new JettyClientHttpConnector())
.build();
}

private static Object nettyClient() {
HttpClient httpClient = HttpClient.create()
// followRedirect(boolean) only enables redirect for 30[1278], not 303
.followRedirect((req, res) -> res.status().code() == 303);

// crete netty reactor client
return WebClient.builder()
.clientConnector(new ReactorClientHttpConnector(httpClient))
.build();
}

public static Object reactiveHttpClient5() {
return WebClient.builder()
.clientConnector(new HttpComponentsClientHttpConnector())
.build();
}
}

}
Loading