Skip to content

fix(java): fix RestTemplate auto-instrumentation guide #13236

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

Merged
merged 3 commits into from
Apr 17, 2025
Merged
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 @@ -51,7 +51,7 @@ Each sampled request executed by this controller method will be turned into a tr
Sentry Spring integration provides `SentrySpanClientHttpRequestInterceptor` that creates a span for each outgoing HTTP request executed with a `RestTemplate`. To use instrumented `RestTemplate` make sure to set interceptor on the `RestTemplate` bean:

```java {tabTitle:Java (Spring 5)}
import io.sentry.IHub;
import io.sentry.IScopes;
import io.sentry.spring.tracing.SentrySpanClientHttpRequestInterceptor;
import java.util.Collections;
import org.springframework.context.annotation.Bean;
Expand All @@ -63,18 +63,18 @@ import org.springframework.web.util.UriTemplateHandler;
class AppConfig {

@Bean
RestTemplate restTemplate(IHub hub) {
RestTemplate restTemplate(IScopes scopes) {
RestTemplate restTemplate = new RestTemplate();
SentrySpanClientHttpRequestInterceptor sentryRestTemplateInterceptor =
new SentrySpanClientHttpRequestInterceptor(hub);
new SentrySpanClientHttpRequestInterceptor(scopes);
restTemplate.setInterceptors(Collections.singletonList(sentryRestTemplateInterceptor));
return restTemplate;
}
}
```

```java {tabTitle:Java (Spring 6)}
import io.sentry.IHub;
import io.sentry.IScopes;
import io.sentry.spring.jakarta.tracing.SentrySpanClientHttpRequestInterceptor;
import java.util.Collections;
import org.springframework.context.annotation.Bean;
Expand All @@ -86,18 +86,18 @@ import org.springframework.web.util.UriTemplateHandler;
class AppConfig {

@Bean
RestTemplate restTemplate(IHub hub) {
RestTemplate restTemplate(IScopes scopes) {
RestTemplate restTemplate = new RestTemplate();
SentrySpanClientHttpRequestInterceptor sentryRestTemplateInterceptor =
new SentrySpanClientHttpRequestInterceptor(hub);
new SentrySpanClientHttpRequestInterceptor(scopes);
restTemplate.setInterceptors(Collections.singletonList(sentryRestTemplateInterceptor));
return restTemplate;
}
}
```

```kotlin {tabTitle:Kotlin (Spring 5)}
import io.sentry.IHub
import io.sentry.IScopes
import io.sentry.spring.tracing.SentrySpanClientHttpRequestInterceptor
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
Expand All @@ -108,8 +108,8 @@ import org.springframework.web.util.UriTemplateHandler
class AppConfig {

@Bean
fun restTemplate(hub: IHub): RestTemplate {
val sentryRestTemplateInterceptor = SentrySpanClientHttpRequestInterceptor(hub)
fun restTemplate(scopes: IScopes): RestTemplate {
val sentryRestTemplateInterceptor = SentrySpanClientHttpRequestInterceptor(scopes)

return RestTemplate().apply {
interceptors = listOf(sentryRestTemplateInterceptor)
Expand All @@ -119,7 +119,7 @@ class AppConfig {
```

```kotlin {tabTitle:Kotlin (Spring 6)}
import io.sentry.IHub
import io.sentry.IScopes
import io.sentry.spring.jakarta.tracing.SentrySpanClientHttpRequestInterceptor
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
Expand All @@ -130,8 +130,8 @@ import org.springframework.web.util.UriTemplateHandler
class AppConfig {

@Bean
fun restTemplate(hub: IHub): RestTemplate {
val sentryRestTemplateInterceptor = SentrySpanClientHttpRequestInterceptor(hub)
fun restTemplate(scopes: IScopes): RestTemplate {
val sentryRestTemplateInterceptor = SentrySpanClientHttpRequestInterceptor(scopes)

return RestTemplate().apply {
interceptors = listOf(sentryRestTemplateInterceptor)
Expand Down