11package io .sentry .spring .autoconfigure ;
22
3+ import io .sentry .Sentry ;
4+ import io .sentry .SentryClient ;
5+ import io .sentry .SentryOptions ;
6+ import io .sentry .config .Lookup ;
7+ import io .sentry .config .provider .ConfigurationProvider ;
8+ import io .sentry .connection .EventSendCallback ;
9+ import io .sentry .event .helper .EventBuilderHelper ;
10+ import io .sentry .event .helper .ShouldSendEventCallback ;
311import io .sentry .spring .SentryExceptionResolver ;
412import io .sentry .spring .SentryServletContextInitializer ;
5-
13+ import org . springframework . beans . factory . annotation . Autowired ;
614import org .springframework .boot .autoconfigure .condition .ConditionalOnClass ;
715import org .springframework .boot .autoconfigure .condition .ConditionalOnMissingBean ;
816import org .springframework .boot .autoconfigure .condition .ConditionalOnProperty ;
917import org .springframework .boot .autoconfigure .condition .ConditionalOnWebApplication ;
18+ import org .springframework .boot .context .properties .EnableConfigurationProperties ;
1019import org .springframework .boot .web .servlet .ServletContextInitializer ;
1120import org .springframework .context .annotation .Bean ;
1221import org .springframework .context .annotation .Configuration ;
22+ import org .springframework .util .StringUtils ;
1323import org .springframework .web .servlet .HandlerExceptionResolver ;
1424
25+ import java .util .Collections ;
26+ import java .util .List ;
27+ import java .util .Map ;
28+
1529/**
1630 * Spring Auto Configuration for Sentry.
1731 */
1832@ Configuration
19- @ ConditionalOnClass ({ HandlerExceptionResolver .class , SentryExceptionResolver .class })
33+ @ ConditionalOnClass ({HandlerExceptionResolver .class , SentryExceptionResolver .class })
34+ @ EnableConfigurationProperties (SentryProperties .class )
2035@ ConditionalOnWebApplication
2136@ ConditionalOnProperty (name = "sentry.enabled" , havingValue = "true" , matchIfMissing = true )
2237public class SentryAutoConfiguration {
2338
2439 /**
2540 * Resolves a {@link HandlerExceptionResolver}.
26- * @return a new instance of {@link SentryAutoConfiguration}.
41+ *
42+ * @return a new instance of {@link HandlerExceptionResolver}.
2743 */
2844 @ Bean
2945 @ ConditionalOnMissingBean (SentryExceptionResolver .class )
@@ -33,6 +49,7 @@ public HandlerExceptionResolver sentryExceptionResolver() {
3349
3450 /**
3551 * Initializes a {@link ServletContextInitializer}.
52+ *
3653 * @return a new instance of {@link SentryServletContextInitializer}.
3754 */
3855 @ Bean
@@ -41,4 +58,84 @@ public ServletContextInitializer sentryServletContextInitializer() {
4158 return new SentryServletContextInitializer ();
4259 }
4360
61+ /**
62+ * Initializes a {@link SentryClient}.
63+ *
64+ * @return a new instance of {@link SentryClient}.
65+ */
66+ @ Bean
67+ @ ConditionalOnMissingBean (SentryClient .class )
68+ @ ConditionalOnProperty (name = "sentry.init-default-client" , havingValue = "true" , matchIfMissing = true )
69+ public SentryClient sentryClient (SentryProperties properties ,
70+ @ Autowired (required = false ) List <EventBuilderHelper > eventBuilderHelpers ,
71+ @ Autowired (required = false ) List <EventSendCallback > eventSendCallbacks ,
72+ @ Autowired (required = false ) List <ShouldSendEventCallback > shouldSendEventCallbacks ) {
73+ String dsn = properties .getDsn () != null ? properties .getDsn ().toString () : null ;
74+
75+ SentryOptions sentryOptions = SentryOptions .from (createLookup (properties ), dsn , null );
76+
77+ SentryClient sentryClient = Sentry .init (sentryOptions );
78+
79+ if (!StringUtils .isEmpty (properties .getRelease ())) {
80+ sentryClient .setRelease (properties .getRelease ());
81+ }
82+
83+ if (!StringUtils .isEmpty (properties .getDist ())) {
84+ sentryClient .setDist (properties .getDist ());
85+ }
86+
87+ if (!StringUtils .isEmpty (properties .getEnvironment ())) {
88+ sentryClient .setEnvironment (properties .getEnvironment ());
89+ }
90+
91+ if (!StringUtils .isEmpty (properties .getServerName ())) {
92+ sentryClient .setServerName (properties .getServerName ());
93+ }
94+
95+ if (properties .getTags () != null && !properties .getTags ().isEmpty ()) {
96+ for (Map .Entry <String , String > tag : properties .getTags ().entrySet ()) {
97+ sentryClient .addTag (tag .getKey (), tag .getValue ());
98+ }
99+ }
100+
101+ if (properties .getMdcTags () != null && !properties .getMdcTags ().isEmpty ()) {
102+ for (String mdcTag : properties .getMdcTags ()) {
103+ sentryClient .addMdcTag (mdcTag );
104+ }
105+ }
106+
107+ if (properties .getExtra () != null && !properties .getExtra ().isEmpty ()) {
108+ for (Map .Entry <String , Object > extra : properties .getExtra ().entrySet ()) {
109+ sentryClient .addExtra (extra .getKey (), extra .getValue ());
110+ }
111+ }
112+
113+ if (eventBuilderHelpers != null && !eventBuilderHelpers .isEmpty ()) {
114+ for (EventBuilderHelper eventBuilderHelper : eventBuilderHelpers ) {
115+ sentryClient .addBuilderHelper (eventBuilderHelper );
116+ }
117+ }
118+
119+ if (eventSendCallbacks != null && !eventSendCallbacks .isEmpty ()) {
120+ for (EventSendCallback eventSendCallback : eventSendCallbacks ) {
121+ sentryClient .addEventSendCallback (eventSendCallback );
122+ }
123+ }
124+
125+ if (shouldSendEventCallbacks != null && !shouldSendEventCallbacks .isEmpty ()) {
126+ for (ShouldSendEventCallback shouldSendEventCallback : shouldSendEventCallbacks ) {
127+ sentryClient .addShouldSendEventCallback (shouldSendEventCallback );
128+ }
129+ }
130+
131+ return sentryClient ;
132+ }
133+
134+ private Lookup createLookup (SentryProperties properties ) {
135+ return Lookup .getDefaultWithAdditionalProviders (
136+ Collections .<ConfigurationProvider >singletonList (new SpringBootConfigurationProvider (properties )),
137+ Collections .<ConfigurationProvider >emptyList ()
138+ );
139+ }
140+
44141}
0 commit comments