Skip to content

Commit

Permalink
Added a test to verify the instrumentation support for the Spring HTT…
Browse files Browse the repository at this point in the history
…P client RestClient. (#3442)
  • Loading branch information
videnkz authored Nov 28, 2023
1 parent a4daa46 commit 8ef5bf9
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 0 deletions.
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>
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());
}
}
}
1 change: 1 addition & 0 deletions apm-agent-plugins/apm-spring-resttemplate/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
<modules>
<module>apm-spring-resttemplate-plugin</module>
<module>apm-spring-resttemplate-test</module>
<module>apm-spring-restclient-test</module>
</modules>

</project>
5 changes: 5 additions & 0 deletions docs/supported-technologies.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,11 @@ The spans are named after the schema `<method> <host>`, for example `GET elastic
|
|
| 1.36.0

|Spring RestClient
|6.1.0+
|
|1.45.0
|===


Expand Down

0 comments on commit 8ef5bf9

Please sign in to comment.