-
Notifications
You must be signed in to change notification settings - Fork 325
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added a test to verify the instrumentation support for the Spring HTTP client RestClient. #3442
Merged
Merged
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
4744923
ADded tests that checks spring RestClient
videnkz a4e0882
updated supported technologies doc
videnkz 3132bba
removed SpringRestClientIntegration test
videnkz a4e7325
Merge remote-tracking branch 'upstream/main' into spring6-restclient
videnkz e578892
up version in supported technologies
videnkz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()); | ||
} | ||
} | ||
} |
95 changes: 95 additions & 0 deletions
95
...lient-test/src/test/java/co/elastic/apm/agent/restclient/SprintRestClientIntegration.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,95 @@ | ||
/* | ||
* 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.AbstractInstrumentationTest; | ||
import co.elastic.apm.agent.impl.context.Http; | ||
import co.elastic.apm.agent.impl.transaction.Span; | ||
import co.elastic.apm.agent.impl.transaction.Transaction; | ||
import com.github.tomakehurst.wiremock.core.WireMockConfiguration; | ||
import com.github.tomakehurst.wiremock.junit.WireMockRule; | ||
import org.junit.After; | ||
import org.junit.Before; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.springframework.web.client.RestClient; | ||
import org.springframework.web.client.RestTemplate; | ||
|
||
import static com.github.tomakehurst.wiremock.client.WireMock.any; | ||
import static com.github.tomakehurst.wiremock.client.WireMock.okJson; | ||
import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
public class SprintRestClientIntegration extends AbstractInstrumentationTest { | ||
|
||
@Rule | ||
public WireMockRule wireMockRule = new WireMockRule(WireMockConfiguration.wireMockConfig().dynamicPort(), false); | ||
|
||
private final RestClient restClient; | ||
|
||
private Transaction rootTransaction; | ||
|
||
private final boolean expectSpan; | ||
|
||
public SprintRestClientIntegration() { | ||
this(true); | ||
} | ||
|
||
protected SprintRestClientIntegration(boolean expectSpan){ | ||
this.restClient = RestClient.create(); | ||
this.expectSpan = expectSpan; | ||
} | ||
|
||
@Before | ||
public void before() { | ||
wireMockRule.stubFor(any(urlEqualTo("/")) | ||
.willReturn(okJson("{}") | ||
.withStatus(200))); | ||
|
||
rootTransaction = tracer.startRootTransaction(getClass().getClassLoader()); | ||
rootTransaction.withName("parent of http span") | ||
.withType("request") | ||
.activate(); | ||
} | ||
|
||
@After | ||
public void after() { | ||
rootTransaction.deactivate().end(); | ||
reporter.awaitTransactionCount(1); | ||
} | ||
|
||
@Test | ||
public void getRoot() { | ||
String url = String.format("http://127.0.0.1:%d/", wireMockRule.port()); | ||
String result = restClient.get().uri(url) | ||
.retrieve() | ||
.body(String.class); | ||
|
||
if (!expectSpan) { | ||
reporter.assertNoSpan(1000); | ||
} else { | ||
reporter.awaitSpanCount(1); | ||
|
||
Span span = reporter.getFirstSpan(); | ||
Http http = span.getContext().getHttp(); | ||
assertThat(http.getMethod()).isEqualTo("GET"); | ||
assertThat(http.getUrl().toString()).isEqualTo(url); | ||
} | ||
} | ||
} |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this test bring any benefit In addition to the
SpringRestClientInstrumentationTest
? To me it seems like they cover the same things.If it doesn't cover anything I'm missing here, let's keep it simple and remove
SpringRestClientIntegration
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
removed