From 891ae0d8f761628b7fd8c27f2aabbabc6cb244f9 Mon Sep 17 00:00:00 2001 From: Lorenzo Cian Date: Fri, 4 Apr 2025 13:40:38 +0200 Subject: [PATCH 1/3] fix(java): fix `RestTemplate` autoinstrumentation guide --- .../automatic-instrumentation.mdx | 45 ++++++++++++++----- 1 file changed, 33 insertions(+), 12 deletions(-) diff --git a/docs/platforms/java/guides/spring/tracing/instrumentation/automatic-instrumentation.mdx b/docs/platforms/java/guides/spring/tracing/instrumentation/automatic-instrumentation.mdx index 82efdfa2ff622f..f365fd7a04cabd 100644 --- a/docs/platforms/java/guides/spring/tracing/instrumentation/automatic-instrumentation.mdx +++ b/docs/platforms/java/guides/spring/tracing/instrumentation/automatic-instrumentation.mdx @@ -19,6 +19,7 @@ import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; + @RestController class HelloController { @@ -51,22 +52,27 @@ 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; import org.springframework.context.annotation.Configuration; import org.springframework.web.client.RestTemplate; import org.springframework.web.util.UriTemplateHandler; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.ApplicationContext; @Configuration class AppConfig { + @Autowired + private ApplicationContext applicationContext; + @Bean - RestTemplate restTemplate(IHub hub) { + RestTemplate restTemplate() { RestTemplate restTemplate = new RestTemplate(); SentrySpanClientHttpRequestInterceptor sentryRestTemplateInterceptor = - new SentrySpanClientHttpRequestInterceptor(hub); + new SentrySpanClientHttpRequestInterceptor(applicationContext.getBean(IScopes.class)); restTemplate.setInterceptors(Collections.singletonList(sentryRestTemplateInterceptor)); return restTemplate; } @@ -74,22 +80,27 @@ class AppConfig { ``` ```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; import org.springframework.context.annotation.Configuration; import org.springframework.web.client.RestTemplate; import org.springframework.web.util.UriTemplateHandler; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.ApplicationContext; @Configuration class AppConfig { + @Autowired + private ApplicationContext applicationContext; + @Bean - RestTemplate restTemplate(IHub hub) { + RestTemplate restTemplate() { RestTemplate restTemplate = new RestTemplate(); SentrySpanClientHttpRequestInterceptor sentryRestTemplateInterceptor = - new SentrySpanClientHttpRequestInterceptor(hub); + new SentrySpanClientHttpRequestInterceptor(applicationContext.getBean(IScopes.class)); restTemplate.setInterceptors(Collections.singletonList(sentryRestTemplateInterceptor)); return restTemplate; } @@ -97,19 +108,24 @@ class AppConfig { ``` ```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 import org.springframework.web.client.RestTemplate import org.springframework.web.util.UriTemplateHandler +import org.springframework.beans.factory.annotation.Autowired +import org.springframework.context.ApplicationContext @Configuration class AppConfig { + @Autowired + private ApplicationContext applicationContext; + @Bean - fun restTemplate(hub: IHub): RestTemplate { - val sentryRestTemplateInterceptor = SentrySpanClientHttpRequestInterceptor(hub) + fun restTemplate(): RestTemplate { + val sentryRestTemplateInterceptor = SentrySpanClientHttpRequestInterceptor(applicationContext.getBean(IScopes.class)) return RestTemplate().apply { interceptors = listOf(sentryRestTemplateInterceptor) @@ -119,19 +135,24 @@ 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 import org.springframework.web.client.RestTemplate import org.springframework.web.util.UriTemplateHandler +import org.springframework.beans.factory.annotation.Autowired +import org.springframework.context.ApplicationContext @Configuration class AppConfig { + @Autowired + private ApplicationContext applicationContext; + @Bean - fun restTemplate(hub: IHub): RestTemplate { - val sentryRestTemplateInterceptor = SentrySpanClientHttpRequestInterceptor(hub) + fun restTemplate(): RestTemplate { + val sentryRestTemplateInterceptor = SentrySpanClientHttpRequestInterceptor(applicationContext.getBean(IScopes.class)) return RestTemplate().apply { interceptors = listOf(sentryRestTemplateInterceptor) From fe3062507c4ffa9174d72462a06076a36a08760e Mon Sep 17 00:00:00 2001 From: Lorenzo Cian Date: Thu, 17 Apr 2025 16:13:11 +0200 Subject: [PATCH 2/3] Update automatic-instrumentation.mdx --- .../automatic-instrumentation.mdx | 21 +++++++------------ 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/docs/platforms/java/guides/spring/tracing/instrumentation/automatic-instrumentation.mdx b/docs/platforms/java/guides/spring/tracing/instrumentation/automatic-instrumentation.mdx index f365fd7a04cabd..8d7b84880eb4db 100644 --- a/docs/platforms/java/guides/spring/tracing/instrumentation/automatic-instrumentation.mdx +++ b/docs/platforms/java/guides/spring/tracing/instrumentation/automatic-instrumentation.mdx @@ -59,20 +59,15 @@ import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.client.RestTemplate; import org.springframework.web.util.UriTemplateHandler; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.context.ApplicationContext; @Configuration class AppConfig { - @Autowired - private ApplicationContext applicationContext; - @Bean - RestTemplate restTemplate() { + RestTemplate restTemplate(IScopes scopes) { RestTemplate restTemplate = new RestTemplate(); SentrySpanClientHttpRequestInterceptor sentryRestTemplateInterceptor = - new SentrySpanClientHttpRequestInterceptor(applicationContext.getBean(IScopes.class)); + new SentrySpanClientHttpRequestInterceptor(scopes); restTemplate.setInterceptors(Collections.singletonList(sentryRestTemplateInterceptor)); return restTemplate; } @@ -97,10 +92,10 @@ class AppConfig { private ApplicationContext applicationContext; @Bean - RestTemplate restTemplate() { + RestTemplate restTemplate(IScopes scopes) { RestTemplate restTemplate = new RestTemplate(); SentrySpanClientHttpRequestInterceptor sentryRestTemplateInterceptor = - new SentrySpanClientHttpRequestInterceptor(applicationContext.getBean(IScopes.class)); + new SentrySpanClientHttpRequestInterceptor(scopes); restTemplate.setInterceptors(Collections.singletonList(sentryRestTemplateInterceptor)); return restTemplate; } @@ -124,8 +119,8 @@ class AppConfig { private ApplicationContext applicationContext; @Bean - fun restTemplate(): RestTemplate { - val sentryRestTemplateInterceptor = SentrySpanClientHttpRequestInterceptor(applicationContext.getBean(IScopes.class)) + fun restTemplate(scopes: IScopes): RestTemplate { + val sentryRestTemplateInterceptor = SentrySpanClientHttpRequestInterceptor(scopes) return RestTemplate().apply { interceptors = listOf(sentryRestTemplateInterceptor) @@ -151,8 +146,8 @@ class AppConfig { private ApplicationContext applicationContext; @Bean - fun restTemplate(): RestTemplate { - val sentryRestTemplateInterceptor = SentrySpanClientHttpRequestInterceptor(applicationContext.getBean(IScopes.class)) + fun restTemplate(scopes: IScopes): RestTemplate { + val sentryRestTemplateInterceptor = SentrySpanClientHttpRequestInterceptor(scopes) return RestTemplate().apply { interceptors = listOf(sentryRestTemplateInterceptor) From 1753fa37dc0af62467b0a0165165b488a4dd698a Mon Sep 17 00:00:00 2001 From: Lorenzo Cian Date: Thu, 17 Apr 2025 16:14:10 +0200 Subject: [PATCH 3/3] Update automatic-instrumentation.mdx --- .../automatic-instrumentation.mdx | 16 ---------------- 1 file changed, 16 deletions(-) diff --git a/docs/platforms/java/guides/spring/tracing/instrumentation/automatic-instrumentation.mdx b/docs/platforms/java/guides/spring/tracing/instrumentation/automatic-instrumentation.mdx index 8d7b84880eb4db..c4cbc06b0bd532 100644 --- a/docs/platforms/java/guides/spring/tracing/instrumentation/automatic-instrumentation.mdx +++ b/docs/platforms/java/guides/spring/tracing/instrumentation/automatic-instrumentation.mdx @@ -19,7 +19,6 @@ import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; - @RestController class HelloController { @@ -82,15 +81,10 @@ import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.client.RestTemplate; import org.springframework.web.util.UriTemplateHandler; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.context.ApplicationContext; @Configuration class AppConfig { - @Autowired - private ApplicationContext applicationContext; - @Bean RestTemplate restTemplate(IScopes scopes) { RestTemplate restTemplate = new RestTemplate(); @@ -109,15 +103,10 @@ import org.springframework.context.annotation.Bean import org.springframework.context.annotation.Configuration import org.springframework.web.client.RestTemplate import org.springframework.web.util.UriTemplateHandler -import org.springframework.beans.factory.annotation.Autowired -import org.springframework.context.ApplicationContext @Configuration class AppConfig { - @Autowired - private ApplicationContext applicationContext; - @Bean fun restTemplate(scopes: IScopes): RestTemplate { val sentryRestTemplateInterceptor = SentrySpanClientHttpRequestInterceptor(scopes) @@ -136,15 +125,10 @@ import org.springframework.context.annotation.Bean import org.springframework.context.annotation.Configuration import org.springframework.web.client.RestTemplate import org.springframework.web.util.UriTemplateHandler -import org.springframework.beans.factory.annotation.Autowired -import org.springframework.context.ApplicationContext @Configuration class AppConfig { - @Autowired - private ApplicationContext applicationContext; - @Bean fun restTemplate(scopes: IScopes): RestTemplate { val sentryRestTemplateInterceptor = SentrySpanClientHttpRequestInterceptor(scopes)