-
Notifications
You must be signed in to change notification settings - Fork 321
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added a test to verify the instrumentation support for the Spring HTT…
…P client RestClient. (#3442)
- Loading branch information
Showing
4 changed files
with
148 additions
and
0 deletions.
There are no files selected for viewing
59 changes: 59 additions & 0 deletions
59
apm-agent-plugins/apm-spring-resttemplate/apm-spring-restclient-test/pom.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<parent> | ||
<groupId>co.elastic.apm</groupId> | ||
<artifactId>apm-spring-resttemplate</artifactId> | ||
<version>1.44.1-SNAPSHOT</version> | ||
</parent> | ||
|
||
<artifactId>apm-spring-restclient-test</artifactId> | ||
<name>${project.groupId}:${project.artifactId}</name> | ||
|
||
<properties> | ||
<apm-agent-parent.base.dir>${project.basedir}/../../..</apm-agent-parent.base.dir> | ||
</properties> | ||
|
||
<dependencyManagement> | ||
<dependencies> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-dependencies</artifactId> | ||
<version>3.2.0</version> | ||
<type>pom</type> | ||
<scope>import</scope> | ||
</dependency> | ||
</dependencies> | ||
</dependencyManagement> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>org.springframework</groupId> | ||
<artifactId>spring-webmvc</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>${project.groupId}</groupId> | ||
<artifactId>apm-spring-resttemplate-plugin</artifactId> | ||
<version>${project.version}</version> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>${project.groupId}</groupId> | ||
<artifactId>apm-spring-resttemplate-plugin</artifactId> | ||
<version>${project.version}</version> | ||
<type>test-jar</type> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>${project.groupId}</groupId> | ||
<artifactId>apm-httpclient-core</artifactId> | ||
<version>${project.version}</version> | ||
<type>test-jar</type> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
</project> |
83 changes: 83 additions & 0 deletions
83
...st/src/test/java/co/elastic/apm/agent/restclient/SpringRestClientInstrumentationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/* | ||
* 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.restclient; | ||
|
||
import co.elastic.apm.agent.common.JvmRuntimeInfo; | ||
import co.elastic.apm.agent.httpclient.AbstractHttpClientInstrumentationTest; | ||
import org.junit.runner.RunWith; | ||
import org.junit.runners.Parameterized; | ||
import org.springframework.web.client.RestClient; | ||
import org.springframework.web.client.RestTemplate; | ||
|
||
import java.util.List; | ||
import java.util.function.Supplier; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.Stream; | ||
|
||
@RunWith(Parameterized.class) | ||
public class SpringRestClientInstrumentationTest extends AbstractHttpClientInstrumentationTest { | ||
|
||
// Cannot directly reference RestTemplate here because it is compiled with Java 17 | ||
private final Object restClient; | ||
private final Boolean isRedirectFollowingSupported; | ||
|
||
public SpringRestClientInstrumentationTest(Supplier<RestClient> supplier, | ||
Boolean isRedirectFollowingSupported) { | ||
this.restClient = supplier.get(); | ||
this.isRedirectFollowingSupported = isRedirectFollowingSupported; | ||
} | ||
|
||
@Parameterized.Parameters() | ||
public static Iterable<Object[]> data() { | ||
if (JvmRuntimeInfo.ofCurrentVM().getMajorVersion() >= 17) { | ||
return SpringRestClientInstrumentationTest.Java17Code.getRestClient(); | ||
} else { | ||
return List.of(); | ||
} | ||
} | ||
|
||
@Override | ||
protected void performGet(String path) { | ||
Java17Code.performGet(restClient, path); | ||
} | ||
|
||
@Override | ||
protected boolean isRedirectFollowingSupported() { | ||
return isRedirectFollowingSupported; | ||
} | ||
|
||
/** | ||
* The code is compiled with java 17 but potentially run with java 11. | ||
* JUnit will inspect the test class, therefore it must not contain any references to java 17 code. | ||
*/ | ||
private static class Java17Code { | ||
public static void performGet(Object restClient, String path) { | ||
((RestClient) restClient).get().uri(path).retrieve().body(String.class); | ||
} | ||
|
||
public static Iterable<Object[]> getRestClient() { | ||
Supplier<RestClient> defaultRestClient = () -> RestClient.create(); | ||
Supplier<RestClient> restTemplateBasedRestClient = () -> RestClient.create(new RestTemplate()); | ||
return Stream.of( | ||
new Object[][]{{defaultRestClient, false}, | ||
{restTemplateBasedRestClient, true}}) | ||
.collect(Collectors.toList()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters