@@ -54,74 +54,83 @@ public static boolean shouldInit(
5454 }
5555
5656 public static IContinuousProfiler initializeProfiler (@ NotNull SentryOptions options ) {
57- IContinuousProfiler continuousProfiler = NoOpContinuousProfiler .getInstance ();
58-
59- if (options .isContinuousProfilingEnabled ()
60- && options .getContinuousProfiler () == NoOpContinuousProfiler .getInstance ()) {
61- try {
62- String profilingTracesDirPath = options .getProfilingTracesDirPath ();
63- if (profilingTracesDirPath == null ) {
64- File tempDir = new File (System .getProperty ("java.io.tmpdir" ), "sentry_profiling_traces" );
65- boolean createDirectorySuccess = tempDir .mkdirs () || tempDir .exists ();
66-
67- if (!createDirectorySuccess ) {
68- throw new IllegalArgumentException (
69- "Creating a fallback directory for profiling failed in "
70- + tempDir .getAbsolutePath ());
71- }
72- profilingTracesDirPath = tempDir .getAbsolutePath ();
73- options .setProfilingTracesDirPath (profilingTracesDirPath );
74- }
75-
76- continuousProfiler =
77- ProfilingServiceLoader .loadContinuousProfiler (
78- options .getLogger (),
79- profilingTracesDirPath ,
80- options .getProfilingTracesHz (),
81- options .getExecutorService ());
82-
83- if (!(continuousProfiler instanceof NoOpContinuousProfiler )) {
84- options .setContinuousProfiler (continuousProfiler );
85- options .getLogger ().log (SentryLevel .INFO , "Successfully loaded profiler" );
86- } else {
87- options
88- .getLogger ()
89- .log (
90- SentryLevel .WARNING ,
91- "Could not load profiler, profiling will be disabled. If you are using Spring or Spring Boot with the OTEL Agent profiler init will be retried." );
92- }
93-
94- return continuousProfiler ;
95-
96- } catch (Exception e ) {
57+ if (!shouldInitializeProfiler (options )) {
58+ return options .getContinuousProfiler ();
59+ }
60+
61+ try {
62+ String profilingTracesDirPath = getOrCreateProfilingTracesDir (options );
63+ IContinuousProfiler profiler =
64+ ProfilingServiceLoader .loadContinuousProfiler (
65+ options .getLogger (),
66+ profilingTracesDirPath ,
67+ options .getProfilingTracesHz (),
68+ options .getExecutorService ());
69+
70+ if (profiler instanceof NoOpContinuousProfiler ) {
9771 options
9872 .getLogger ()
99- .log (SentryLevel .ERROR , "Failed to create default profiling traces directory" , e );
73+ .log (
74+ SentryLevel .WARNING ,
75+ "Could not load profiler, profiling will be disabled. If you are using Spring or Spring Boot with the OTEL Agent profiler init will be retried." );
76+ } else {
77+ options .setContinuousProfiler (profiler );
78+ options .getLogger ().log (SentryLevel .INFO , "Successfully loaded profiler" );
10079 }
80+ } catch (Exception e ) {
81+ options
82+ .getLogger ()
83+ .log (SentryLevel .ERROR , "Failed to create default profiling traces directory" , e );
10184 }
102- return continuousProfiler ;
85+
86+ return options .getContinuousProfiler ();
10387 }
10488
10589 public static IProfileConverter initializeProfileConverter (@ NotNull SentryOptions options ) {
106- IProfileConverter converter = NoOpProfileConverter .getInstance ();
107-
108- if (options .isContinuousProfilingEnabled ()
109- && options .getProfilerConverter () instanceof NoOpProfileConverter ) {
90+ if (!shouldInitializeProfileConverter (options )) {
91+ return options .getProfilerConverter ();
92+ }
11093
111- converter = ProfilingServiceLoader .loadProfileConverter ();
94+ IProfileConverter converter = ProfilingServiceLoader .loadProfileConverter ();
11295
96+ if (converter instanceof NoOpProfileConverter ) {
97+ options
98+ .getLogger ()
99+ .log (
100+ SentryLevel .WARNING ,
101+ "Could not load profile converter. If you are using Spring or Spring Boot with the OTEL Agent, profile converter init will be retried." );
102+ } else {
113103 options .setProfilerConverter (converter );
104+ options .getLogger ().log (SentryLevel .INFO , "Successfully loaded profile converter" );
105+ }
114106
115- if (!(converter instanceof NoOpProfileConverter )) {
116- options .getLogger ().log (SentryLevel .INFO , "Successfully loaded profile converter" );
117- } else {
118- options
119- .getLogger ()
120- .log (
121- SentryLevel .WARNING ,
122- "Could not load profile converter. If you are using Spring or Spring Boot with the OTEL Agent, profile converter init will be retried." );
123- }
107+ return options .getProfilerConverter ();
108+ }
109+
110+ private static boolean shouldInitializeProfiler (@ NotNull SentryOptions options ) {
111+ return options .isContinuousProfilingEnabled ()
112+ && options .getContinuousProfiler () instanceof NoOpContinuousProfiler ;
113+ }
114+
115+ private static boolean shouldInitializeProfileConverter (@ NotNull SentryOptions options ) {
116+ return options .isContinuousProfilingEnabled ()
117+ && options .getProfilerConverter () instanceof NoOpProfileConverter ;
118+ }
119+
120+ private static String getOrCreateProfilingTracesDir (@ NotNull SentryOptions options ) {
121+ String profilingTracesDirPath = options .getProfilingTracesDirPath ();
122+ if (profilingTracesDirPath != null ) {
123+ return profilingTracesDirPath ;
124124 }
125- return converter ;
125+
126+ File tempDir = new File (System .getProperty ("java.io.tmpdir" ), "sentry_profiling_traces" );
127+ if (!tempDir .mkdirs () && !tempDir .exists ()) {
128+ throw new IllegalArgumentException (
129+ "Creating a fallback directory for profiling failed in " + tempDir .getAbsolutePath ());
130+ }
131+
132+ profilingTracesDirPath = tempDir .getAbsolutePath ();
133+ options .setProfilingTracesDirPath (profilingTracesDirPath );
134+ return profilingTracesDirPath ;
126135 }
127136}
0 commit comments