|
| 1 | +package io.sentry.spring.jakarta; |
| 2 | + |
| 3 | +import com.jakewharton.nopen.annotation.Open; |
| 4 | +import io.sentry.IContinuousProfiler; |
| 5 | +import io.sentry.IProfileConverter; |
| 6 | +import io.sentry.NoOpContinuousProfiler; |
| 7 | +import io.sentry.NoOpProfileConverter; |
| 8 | +import io.sentry.Sentry; |
| 9 | +import io.sentry.SentryLevel; |
| 10 | +import io.sentry.SentryOptions; |
| 11 | +import io.sentry.profiling.ProfilingServiceLoader; |
| 12 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; |
| 13 | +import org.springframework.context.annotation.Bean; |
| 14 | +import org.springframework.context.annotation.Configuration; |
| 15 | + |
| 16 | +/** |
| 17 | + * Handles late initialization of the profiler if the application is run with the OTEL Agent in |
| 18 | + * auto-init mode. In that case the agent cannot initialize the profiler yet and falls back to No-Op |
| 19 | + * implementations. This Configuration sets the profiler and converter on the options if that was |
| 20 | + * the case. |
| 21 | + */ |
| 22 | +@Configuration(proxyBeanMethods = false) |
| 23 | +@Open |
| 24 | +public class SentryProfilerConfiguration { |
| 25 | + |
| 26 | + @Bean |
| 27 | + @ConditionalOnMissingBean(name = "sentryOpenTelemetryProfilerConfiguration") |
| 28 | + public IContinuousProfiler sentryOpenTelemetryProfilerConfiguration() { |
| 29 | + SentryOptions options = Sentry.getGlobalScope().getOptions(); |
| 30 | + IContinuousProfiler profiler = NoOpContinuousProfiler.getInstance(); |
| 31 | + |
| 32 | + if (Sentry.isEnabled() |
| 33 | + && options.isContinuousProfilingEnabled() |
| 34 | + && options.getContinuousProfiler() instanceof NoOpContinuousProfiler) { |
| 35 | + |
| 36 | + options |
| 37 | + .getLogger() |
| 38 | + .log( |
| 39 | + SentryLevel.DEBUG, |
| 40 | + "Continuous profiler is NoOp, attempting to reload with Spring Boot classloader"); |
| 41 | + |
| 42 | + String path = options.getProfilingTracesDirPath(); |
| 43 | + |
| 44 | + profiler = |
| 45 | + ProfilingServiceLoader.loadContinuousProfiler( |
| 46 | + options.getLogger(), |
| 47 | + path != null ? path : "", |
| 48 | + options.getProfilingTracesHz(), |
| 49 | + options.getExecutorService()); |
| 50 | + |
| 51 | + options.setContinuousProfiler(profiler); |
| 52 | + |
| 53 | + if (!(profiler instanceof NoOpContinuousProfiler)) { |
| 54 | + options |
| 55 | + .getLogger() |
| 56 | + .log( |
| 57 | + SentryLevel.INFO, |
| 58 | + "Successfully loaded continuous profiler via Spring Boot classloader"); |
| 59 | + } |
| 60 | + } |
| 61 | + return profiler; |
| 62 | + } |
| 63 | + |
| 64 | + @Bean |
| 65 | + @ConditionalOnMissingBean(name = "sentryOpenTelemetryProfilerConverterConfiguration") |
| 66 | + public IProfileConverter sentryOpenTelemetryProfilerConverterConfiguration() { |
| 67 | + SentryOptions options = Sentry.getGlobalScope().getOptions(); |
| 68 | + IProfileConverter converter = NoOpProfileConverter.getInstance(); |
| 69 | + |
| 70 | + if (Sentry.isEnabled() |
| 71 | + && options.isContinuousProfilingEnabled() |
| 72 | + && options.getProfilerConverter() instanceof NoOpProfileConverter) { |
| 73 | + |
| 74 | + options |
| 75 | + .getLogger() |
| 76 | + .log( |
| 77 | + SentryLevel.DEBUG, |
| 78 | + "Profile converter is NoOp, attempting to reload with Spring Boot classloader"); |
| 79 | + |
| 80 | + converter = ProfilingServiceLoader.loadProfileConverter(); |
| 81 | + |
| 82 | + options.setProfilerConverter(converter); |
| 83 | + |
| 84 | + if (!(converter instanceof NoOpProfileConverter)) { |
| 85 | + options |
| 86 | + .getLogger() |
| 87 | + .log( |
| 88 | + SentryLevel.INFO, |
| 89 | + "Successfully loaded profile converter via Spring Boot classloader"); |
| 90 | + } |
| 91 | + } |
| 92 | + return converter; |
| 93 | + } |
| 94 | +} |
0 commit comments