@@ -56,9 +56,8 @@ public final class JavaContinuousProfiler
5656
5757 private @ NotNull String filename = "" ;
5858
59- private @ Nullable AsyncProfiler profiler ;
59+ private @ NotNull AsyncProfiler profiler ;
6060 private volatile boolean shouldSample = true ;
61- private boolean shouldStop = false ;
6261 private boolean isSampled = false ;
6362 private int rootSpanCounter = 0 ;
6463
@@ -69,27 +68,20 @@ public JavaContinuousProfiler(
6968 final @ NotNull ILogger logger ,
7069 final @ Nullable String profilingTracesDirPath ,
7170 final int profilingTracesHz ,
72- final @ NotNull ISentryExecutorService executorService ) {
71+ final @ NotNull ISentryExecutorService executorService )
72+ throws Exception {
7373 this .logger = logger ;
7474 this .profilingTracesDirPath = profilingTracesDirPath ;
7575 this .profilingTracesHz = profilingTracesHz ;
7676 this .executorService = executorService ;
7777 initializeProfiler ();
7878 }
7979
80- private void initializeProfiler () {
81- try {
82- this .profiler = AsyncProfiler .getInstance ();
83- // Check version to verify profiler is working
84- String version = profiler .execute ("version" );
85- logger .log (SentryLevel .DEBUG , "AsyncProfiler initialized successfully. Version: " + version );
86- } catch (Exception e ) {
87- logger .log (
88- SentryLevel .WARNING ,
89- "Failed to initialize AsyncProfiler. Profiling will be disabled." ,
90- e );
91- this .profiler = null ;
92- }
80+ private void initializeProfiler () throws Exception {
81+ this .profiler = AsyncProfiler .getInstance ();
82+ // Check version to verify profiler is working
83+ String version = profiler .execute ("version" );
84+ logger .log (SentryLevel .DEBUG , "AsyncProfiler initialized successfully. Version: " + version );
9385 }
9486
9587 private boolean init () {
@@ -98,11 +90,6 @@ private boolean init() {
9890 }
9991 isInitialized = true ;
10092
101- if (profiler == null ) {
102- logger .log (SentryLevel .ERROR , "Disabling profiling because AsyncProfiler is not available." );
103- return false ;
104- }
105-
10693 if (profilingTracesDirPath == null ) {
10794 logger .log (
10895 SentryLevel .WARNING ,
@@ -168,10 +155,11 @@ public void startProfiler(
168155 }
169156
170157 if (!isRunning ()) {
171- shouldStop = false ;
172158 logger .log (SentryLevel .DEBUG , "Started Profiler." );
173159 start ();
174160 }
161+ } catch (Exception e ) {
162+ logger .log (SentryLevel .ERROR , "Error starting profiler: " , e );
175163 }
176164 }
177165
@@ -210,11 +198,6 @@ private void start() {
210198 startProfileChunkTimestamp = new SentryNanotimeDate ();
211199 }
212200
213- if (profiler == null ) {
214- logger .log (SentryLevel .ERROR , "Cannot start profiling: AsyncProfiler is not available" );
215- return ;
216- }
217-
218201 filename = profilingTracesDirPath + File .separator + SentryUUID .generateSentryId () + ".jfr" ;
219202
220203 File jfrFile = new File (filename );
@@ -250,7 +233,8 @@ private void start() {
250233 SentryLevel .ERROR ,
251234 "Failed to schedule profiling chunk finish. Did you call Sentry.close()?" ,
252235 e );
253- shouldStop = true ;
236+ // If we can't schedule the auto-stop, stop immediately without restart
237+ stop (false );
254238 }
255239 }
256240
@@ -269,10 +253,12 @@ public void stopProfiler(final @NotNull ProfileLifecycle profileLifecycle) {
269253 if (rootSpanCounter < 0 ) {
270254 rootSpanCounter = 0 ;
271255 }
272- shouldStop = true ;
256+ // Stop immediately without restart
257+ stop (false );
273258 break ;
274259 case MANUAL :
275- shouldStop = true ;
260+ // Stop immediately without restart
261+ stop (false );
276262 break ;
277263 }
278264 }
@@ -293,11 +279,6 @@ private void stop(final boolean restartProfiler) {
293279
294280 File jfrFile = new File (filename );
295281
296- if (profiler == null ) {
297- logger .log (SentryLevel .WARNING , "Profiler is null when trying to stop" );
298- return ;
299- }
300-
301282 try {
302283 profiler .execute ("stop,jfr" );
303284 } catch (Exception e ) {
@@ -339,14 +320,16 @@ private void stop(final boolean restartProfiler) {
339320 sendChunks (scopes , scopes .getOptions ());
340321 }
341322
342- if (restartProfiler && ! shouldStop ) {
323+ if (restartProfiler ) {
343324 logger .log (SentryLevel .DEBUG , "Profile chunk finished. Starting a new one." );
344325 start ();
345326 } else {
346327 // When the profiler is stopped manually, we have to reset its id
347328 profilerId = SentryId .EMPTY_ID ;
348329 logger .log (SentryLevel .DEBUG , "Profile chunk finished." );
349330 }
331+ } catch (Exception e ) {
332+ logger .log (SentryLevel .ERROR , "Error stopping profiler: " , e );
350333 }
351334 }
352335
@@ -359,9 +342,8 @@ public void reevaluateSampling() {
359342 public void close (final boolean isTerminating ) {
360343 try (final @ NotNull ISentryLifecycleToken ignored = lock .acquire ()) {
361344 rootSpanCounter = 0 ;
362- shouldStop = true ;
345+ stop ( false ) ;
363346 if (isTerminating ) {
364- stop (false );
365347 isClosed .set (true );
366348 }
367349 }
@@ -412,7 +394,7 @@ private void safelyRemoveFile(File file) {
412394 @ Override
413395 public boolean isRunning () {
414396 try (final @ NotNull ISentryLifecycleToken ignored = lock .acquire ()) {
415- return isRunning && profiler != null && !filename .isEmpty ();
397+ return isRunning && !filename .isEmpty ();
416398 }
417399 }
418400
0 commit comments