Skip to content

Commit 891ae0d

Browse files
authored
fix(java): fix RestTemplate autoinstrumentation guide
1 parent fe72a68 commit 891ae0d

File tree

1 file changed

+33
-12
lines changed

1 file changed

+33
-12
lines changed

docs/platforms/java/guides/spring/tracing/instrumentation/automatic-instrumentation.mdx

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import org.springframework.web.bind.annotation.RestController;
1919
import org.springframework.web.bind.annotation.GetMapping;
2020
import org.springframework.web.bind.annotation.PathVariable;
2121

22+
2223
@RestController
2324
class HelloController {
2425

@@ -51,65 +52,80 @@ Each sampled request executed by this controller method will be turned into a tr
5152
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:
5253

5354
```java {tabTitle:Java (Spring 5)}
54-
import io.sentry.IHub;
55+
import io.sentry.IScopes;
5556
import io.sentry.spring.tracing.SentrySpanClientHttpRequestInterceptor;
5657
import java.util.Collections;
5758
import org.springframework.context.annotation.Bean;
5859
import org.springframework.context.annotation.Configuration;
5960
import org.springframework.web.client.RestTemplate;
6061
import org.springframework.web.util.UriTemplateHandler;
62+
import org.springframework.beans.factory.annotation.Autowired;
63+
import org.springframework.context.ApplicationContext;
6164

6265
@Configuration
6366
class AppConfig {
6467

68+
@Autowired
69+
private ApplicationContext applicationContext;
70+
6571
@Bean
66-
RestTemplate restTemplate(IHub hub) {
72+
RestTemplate restTemplate() {
6773
RestTemplate restTemplate = new RestTemplate();
6874
SentrySpanClientHttpRequestInterceptor sentryRestTemplateInterceptor =
69-
new SentrySpanClientHttpRequestInterceptor(hub);
75+
new SentrySpanClientHttpRequestInterceptor(applicationContext.getBean(IScopes.class));
7076
restTemplate.setInterceptors(Collections.singletonList(sentryRestTemplateInterceptor));
7177
return restTemplate;
7278
}
7379
}
7480
```
7581

7682
```java {tabTitle:Java (Spring 6)}
77-
import io.sentry.IHub;
83+
import io.sentry.IScopes;
7884
import io.sentry.spring.jakarta.tracing.SentrySpanClientHttpRequestInterceptor;
7985
import java.util.Collections;
8086
import org.springframework.context.annotation.Bean;
8187
import org.springframework.context.annotation.Configuration;
8288
import org.springframework.web.client.RestTemplate;
8389
import org.springframework.web.util.UriTemplateHandler;
90+
import org.springframework.beans.factory.annotation.Autowired;
91+
import org.springframework.context.ApplicationContext;
8492

8593
@Configuration
8694
class AppConfig {
8795

96+
@Autowired
97+
private ApplicationContext applicationContext;
98+
8899
@Bean
89-
RestTemplate restTemplate(IHub hub) {
100+
RestTemplate restTemplate() {
90101
RestTemplate restTemplate = new RestTemplate();
91102
SentrySpanClientHttpRequestInterceptor sentryRestTemplateInterceptor =
92-
new SentrySpanClientHttpRequestInterceptor(hub);
103+
new SentrySpanClientHttpRequestInterceptor(applicationContext.getBean(IScopes.class));
93104
restTemplate.setInterceptors(Collections.singletonList(sentryRestTemplateInterceptor));
94105
return restTemplate;
95106
}
96107
}
97108
```
98109

99110
```kotlin {tabTitle:Kotlin (Spring 5)}
100-
import io.sentry.IHub
111+
import io.sentry.IScopes
101112
import io.sentry.spring.tracing.SentrySpanClientHttpRequestInterceptor
102113
import org.springframework.context.annotation.Bean
103114
import org.springframework.context.annotation.Configuration
104115
import org.springframework.web.client.RestTemplate
105116
import org.springframework.web.util.UriTemplateHandler
117+
import org.springframework.beans.factory.annotation.Autowired
118+
import org.springframework.context.ApplicationContext
106119

107120
@Configuration
108121
class AppConfig {
109122

123+
@Autowired
124+
private ApplicationContext applicationContext;
125+
110126
@Bean
111-
fun restTemplate(hub: IHub): RestTemplate {
112-
val sentryRestTemplateInterceptor = SentrySpanClientHttpRequestInterceptor(hub)
127+
fun restTemplate(): RestTemplate {
128+
val sentryRestTemplateInterceptor = SentrySpanClientHttpRequestInterceptor(applicationContext.getBean(IScopes.class))
113129

114130
return RestTemplate().apply {
115131
interceptors = listOf(sentryRestTemplateInterceptor)
@@ -119,19 +135,24 @@ class AppConfig {
119135
```
120136

121137
```kotlin {tabTitle:Kotlin (Spring 6)}
122-
import io.sentry.IHub
138+
import io.sentry.IScopes
123139
import io.sentry.spring.jakarta.tracing.SentrySpanClientHttpRequestInterceptor
124140
import org.springframework.context.annotation.Bean
125141
import org.springframework.context.annotation.Configuration
126142
import org.springframework.web.client.RestTemplate
127143
import org.springframework.web.util.UriTemplateHandler
144+
import org.springframework.beans.factory.annotation.Autowired
145+
import org.springframework.context.ApplicationContext
128146

129147
@Configuration
130148
class AppConfig {
131149

150+
@Autowired
151+
private ApplicationContext applicationContext;
152+
132153
@Bean
133-
fun restTemplate(hub: IHub): RestTemplate {
134-
val sentryRestTemplateInterceptor = SentrySpanClientHttpRequestInterceptor(hub)
154+
fun restTemplate(): RestTemplate {
155+
val sentryRestTemplateInterceptor = SentrySpanClientHttpRequestInterceptor(applicationContext.getBean(IScopes.class))
135156

136157
return RestTemplate().apply {
137158
interceptors = listOf(sentryRestTemplateInterceptor)

0 commit comments

Comments
 (0)