-
-
Notifications
You must be signed in to change notification settings - Fork 427
/
SentryAutoConfiguration.java
344 lines (307 loc) · 14.5 KB
/
SentryAutoConfiguration.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
package io.sentry.spring.boot;
import com.jakewharton.nopen.annotation.Open;
import io.sentry.EventProcessor;
import io.sentry.HubAdapter;
import io.sentry.IHub;
import io.sentry.ITransportFactory;
import io.sentry.Integration;
import io.sentry.Sentry;
import io.sentry.SentryOptions;
import io.sentry.opentelemetry.OpenTelemetryLinkErrorEventProcessor;
import io.sentry.protocol.SdkVersion;
import io.sentry.spring.ContextTagsEventProcessor;
import io.sentry.spring.SentryExceptionResolver;
import io.sentry.spring.SentryRequestResolver;
import io.sentry.spring.SentrySpringFilter;
import io.sentry.spring.SentryUserFilter;
import io.sentry.spring.SentryUserProvider;
import io.sentry.spring.SentryWebConfiguration;
import io.sentry.spring.SpringSecuritySentryUserProvider;
import io.sentry.spring.tracing.SentryAdviceConfiguration;
import io.sentry.spring.tracing.SentrySpanPointcutConfiguration;
import io.sentry.spring.tracing.SentryTracingFilter;
import io.sentry.spring.tracing.SentryTransactionPointcutConfiguration;
import io.sentry.spring.tracing.SpringMvcTransactionNameProvider;
import io.sentry.spring.tracing.TransactionNameProvider;
import io.sentry.transport.ITransportGate;
import io.sentry.transport.apache.ApacheHttpClientTransportFactory;
import java.util.List;
import java.util.Optional;
import javax.servlet.http.HttpServletRequest;
import org.aspectj.lang.ProceedingJoinPoint;
import org.jetbrains.annotations.NotNull;
import org.slf4j.MDC;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.AnyNestedCondition;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.autoconfigure.web.client.RestTemplateAutoConfiguration;
import org.springframework.boot.autoconfigure.web.reactive.function.client.WebClientAutoConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.info.GitProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.reactive.function.client.WebClient;
import org.springframework.web.servlet.HandlerExceptionResolver;
@Configuration(proxyBeanMethods = false)
@ConditionalOnProperty(name = "sentry.dsn")
@Open
public class SentryAutoConfiguration {
/** Registers general purpose Sentry related beans. */
@Configuration(proxyBeanMethods = false)
@EnableConfigurationProperties(SentryProperties.class)
@Open
static class HubConfiguration {
@Bean
@ConditionalOnMissingBean(name = "sentryOptionsConfiguration")
@Order(Ordered.HIGHEST_PRECEDENCE)
public @NotNull Sentry.OptionsConfiguration<SentryOptions> sentryOptionsConfiguration(
final @NotNull ObjectProvider<SentryOptions.BeforeSendCallback> beforeSendCallback,
final @NotNull ObjectProvider<SentryOptions.BeforeSendTransactionCallback>
beforeSendTransactionCallback,
final @NotNull ObjectProvider<SentryOptions.BeforeBreadcrumbCallback>
beforeBreadcrumbCallback,
final @NotNull ObjectProvider<SentryOptions.TracesSamplerCallback> tracesSamplerCallback,
final @NotNull List<EventProcessor> eventProcessors,
final @NotNull List<Integration> integrations,
final @NotNull ObjectProvider<ITransportGate> transportGate,
final @NotNull ObjectProvider<ITransportFactory> transportFactory,
final @NotNull InAppIncludesResolver inAppPackagesResolver) {
return options -> {
beforeSendCallback.ifAvailable(options::setBeforeSend);
beforeSendTransactionCallback.ifAvailable(options::setBeforeSendTransaction);
beforeBreadcrumbCallback.ifAvailable(options::setBeforeBreadcrumb);
tracesSamplerCallback.ifAvailable(options::setTracesSampler);
eventProcessors.forEach(options::addEventProcessor);
integrations.forEach(options::addIntegration);
transportGate.ifAvailable(options::setTransportGate);
transportFactory.ifAvailable(options::setTransportFactory);
inAppPackagesResolver.resolveInAppIncludes().forEach(options::addInAppInclude);
};
}
@Bean
public @NotNull InAppIncludesResolver inAppPackagesResolver() {
return new InAppIncludesResolver();
}
@Bean
public @NotNull IHub sentryHub(
final @NotNull List<Sentry.OptionsConfiguration<SentryOptions>> optionsConfigurations,
final @NotNull SentryProperties options,
final @NotNull ObjectProvider<GitProperties> gitProperties) {
optionsConfigurations.forEach(
optionsConfiguration -> optionsConfiguration.configure(options));
gitProperties.ifAvailable(
git -> {
if (options.getRelease() == null && options.isUseGitCommitIdAsRelease()) {
options.setRelease(git.getCommitId());
}
});
options.setSentryClientName(BuildConfig.SENTRY_SPRING_BOOT_SDK_NAME);
options.setSdkVersion(createSdkVersion(options));
if (options.getTracesSampleRate() == null) {
options.setTracesSampleRate(0.0);
}
// Spring Boot sets ignored exceptions in runtime using reflection - where the generic
// information is lost
// its technically possible to set non-throwable class to `ignoredExceptionsForType` set
// here we make sure that only classes that extend throwable are set on this field
options.getIgnoredExceptionsForType().removeIf(it -> !Throwable.class.isAssignableFrom(it));
Sentry.init(options);
return HubAdapter.getInstance();
}
@Configuration(proxyBeanMethods = false)
@ConditionalOnClass(MDC.class)
@Open
static class ContextTagsEventProcessorConfiguration {
@Bean
public @NotNull ContextTagsEventProcessor contextTagsEventProcessor(
final @NotNull SentryOptions sentryOptions) {
return new ContextTagsEventProcessor(sentryOptions);
}
}
@Configuration(proxyBeanMethods = false)
@ConditionalOnProperty(name = "sentry.auto-init", havingValue = "false")
@ConditionalOnClass(OpenTelemetryLinkErrorEventProcessor.class)
@Open
static class OpenTelemetryLinkErrorEventProcessorConfiguration {
@Bean
@ConditionalOnMissingBean
public @NotNull OpenTelemetryLinkErrorEventProcessor openTelemetryLinkErrorEventProcessor() {
return new OpenTelemetryLinkErrorEventProcessor();
}
}
/** Registers beans specific to Spring MVC. */
@Configuration(proxyBeanMethods = false)
@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET)
@Import(SentryWebConfiguration.class)
@Open
static class SentryWebMvcConfiguration {
private static final int SENTRY_SPRING_FILTER_PRECEDENCE = Ordered.HIGHEST_PRECEDENCE;
@Configuration(proxyBeanMethods = false)
@ConditionalOnClass(SecurityContextHolder.class)
@Open
static class SentrySecurityConfiguration {
/**
* Configures {@link SpringSecuritySentryUserProvider} only if Spring Security is on the
* classpath. Its order is set to be higher than {@link
* SentryWebConfiguration#httpServletRequestSentryUserProvider(SentryOptions)}
*
* @param sentryOptions the Sentry options
* @return {@link SpringSecuritySentryUserProvider}
*/
@Bean
@Order(1)
public @NotNull SpringSecuritySentryUserProvider springSecuritySentryUserProvider(
final @NotNull SentryOptions sentryOptions) {
return new SpringSecuritySentryUserProvider(sentryOptions);
}
}
/**
* Configures {@link SentryUserFilter}. By default it runs as the last filter in order to make
* sure that all potential authentication information is propagated to {@link
* HttpServletRequest#getUserPrincipal()}. If Spring Security is auto-configured, its order is
* set to run after Spring Security.
*
* @param hub the Sentry hub
* @param sentryProperties the Sentry properties
* @param sentryUserProvider the user provider
* @return {@link SentryUserFilter} registration bean
*/
@Bean
@ConditionalOnBean(SentryUserProvider.class)
public @NotNull FilterRegistrationBean<SentryUserFilter> sentryUserFilter(
final @NotNull IHub hub,
final @NotNull SentryProperties sentryProperties,
final @NotNull List<SentryUserProvider> sentryUserProvider) {
final FilterRegistrationBean<SentryUserFilter> filter = new FilterRegistrationBean<>();
filter.setFilter(new SentryUserFilter(hub, sentryUserProvider));
filter.setOrder(resolveUserFilterOrder(sentryProperties));
return filter;
}
private @NotNull Integer resolveUserFilterOrder(
final @NotNull SentryProperties sentryProperties) {
return Optional.ofNullable(sentryProperties.getUserFilterOrder())
.orElse(Ordered.LOWEST_PRECEDENCE);
}
@Bean
public @NotNull SentryRequestResolver sentryRequestResolver(final @NotNull IHub hub) {
return new SentryRequestResolver(hub);
}
@Bean
@ConditionalOnMissingBean(TransactionNameProvider.class)
public @NotNull TransactionNameProvider transactionNameProvider() {
return new SpringMvcTransactionNameProvider();
}
@Bean
@ConditionalOnMissingBean(name = "sentrySpringFilter")
public @NotNull FilterRegistrationBean<SentrySpringFilter> sentrySpringFilter(
final @NotNull IHub hub,
final @NotNull SentryRequestResolver requestResolver,
final @NotNull TransactionNameProvider transactionNameProvider) {
FilterRegistrationBean<SentrySpringFilter> filter =
new FilterRegistrationBean<>(
new SentrySpringFilter(hub, requestResolver, transactionNameProvider));
filter.setOrder(SENTRY_SPRING_FILTER_PRECEDENCE);
return filter;
}
@Bean
@Conditional(SentryTracingCondition.class)
@ConditionalOnMissingBean(name = "sentryTracingFilter")
public FilterRegistrationBean<SentryTracingFilter> sentryTracingFilter(
final @NotNull IHub hub, final @NotNull TransactionNameProvider transactionNameProvider) {
FilterRegistrationBean<SentryTracingFilter> filter =
new FilterRegistrationBean<>(new SentryTracingFilter(hub, transactionNameProvider));
filter.setOrder(SENTRY_SPRING_FILTER_PRECEDENCE + 1); // must run after SentrySpringFilter
return filter;
}
@Bean
@ConditionalOnMissingBean
@ConditionalOnClass(HandlerExceptionResolver.class)
public @NotNull SentryExceptionResolver sentryExceptionResolver(
final @NotNull IHub sentryHub,
final @NotNull TransactionNameProvider transactionNameProvider,
final @NotNull SentryProperties options) {
return new SentryExceptionResolver(
sentryHub, transactionNameProvider, options.getExceptionResolverOrder());
}
}
@Configuration(proxyBeanMethods = false)
@Conditional(SentryTracingCondition.class)
@ConditionalOnClass(ProceedingJoinPoint.class)
@Import(SentryAdviceConfiguration.class)
@Open
static class SentryPerformanceAspectsConfiguration {
@Configuration(proxyBeanMethods = false)
@ConditionalOnMissingBean(name = "sentryTransactionPointcut")
@Import(SentryTransactionPointcutConfiguration.class)
@Open
static class SentryTransactionPointcutAutoConfiguration {}
@Configuration(proxyBeanMethods = false)
@ConditionalOnMissingBean(name = "sentrySpanPointcut")
@Import(SentrySpanPointcutConfiguration.class)
@Open
static class SentrySpanPointcutAutoConfiguration {}
}
@Configuration(proxyBeanMethods = false)
@AutoConfigureBefore(RestTemplateAutoConfiguration.class)
@ConditionalOnClass(RestTemplate.class)
@Open
static class SentryPerformanceRestTemplateConfiguration {
@Bean
public SentrySpanRestTemplateCustomizer sentrySpanRestTemplateCustomizer(IHub hub) {
return new SentrySpanRestTemplateCustomizer(hub);
}
}
@Configuration(proxyBeanMethods = false)
@AutoConfigureBefore(WebClientAutoConfiguration.class)
@ConditionalOnClass(WebClient.class)
@Open
static class SentryPerformanceWebClientConfiguration {
@Bean
public SentrySpanWebClientCustomizer sentrySpanWebClientCustomizer(IHub hub) {
return new SentrySpanWebClientCustomizer(hub);
}
}
@Configuration(proxyBeanMethods = false)
@ConditionalOnMissingBean(ITransportFactory.class)
@ConditionalOnClass(ApacheHttpClientTransportFactory.class)
@Open
static class ApacheHttpClientTransportFactoryAutoconfiguration {
@Bean
public @NotNull ApacheHttpClientTransportFactory apacheHttpClientTransportFactory() {
return new ApacheHttpClientTransportFactory();
}
}
private static @NotNull SdkVersion createSdkVersion(
final @NotNull SentryOptions sentryOptions) {
SdkVersion sdkVersion = sentryOptions.getSdkVersion();
final String name = BuildConfig.SENTRY_SPRING_BOOT_SDK_NAME;
final String version = BuildConfig.VERSION_NAME;
sdkVersion = SdkVersion.updateSdkVersion(sdkVersion, name, version);
sdkVersion.addPackage("maven:io.sentry:sentry-spring-boot-starter", version);
return sdkVersion;
}
}
static final class SentryTracingCondition extends AnyNestedCondition {
public SentryTracingCondition() {
super(ConfigurationPhase.REGISTER_BEAN);
}
@ConditionalOnProperty(name = "sentry.traces-sample-rate")
@SuppressWarnings("UnusedNestedClass")
private static class SentryTracesSampleRateCondition {}
@ConditionalOnBean(SentryOptions.TracesSamplerCallback.class)
@SuppressWarnings("UnusedNestedClass")
private static class SentryTracesSamplerBeanCondition {}
}
}