Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
simplify JavaContinuous profiler by catching AsyncProfiler instantiat…
…ion exceptions in provider
  • Loading branch information
lbloder committed Sep 19, 2025
commit cde0eb1ba6526e81266ff0c1c016540251cc1492
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public final class JavaContinuousProfiler

private @NotNull String filename = "";

private @Nullable AsyncProfiler profiler;
private @NotNull AsyncProfiler profiler;
private volatile boolean shouldSample = true;
private boolean isSampled = false;
private int rootSpanCounter = 0;
Expand All @@ -68,27 +68,20 @@ public JavaContinuousProfiler(
final @NotNull ILogger logger,
final @Nullable String profilingTracesDirPath,
final int profilingTracesHz,
final @NotNull ISentryExecutorService executorService) {
final @NotNull ISentryExecutorService executorService)
throws Exception {
this.logger = logger;
this.profilingTracesDirPath = profilingTracesDirPath;
this.profilingTracesHz = profilingTracesHz;
this.executorService = executorService;
initializeProfiler();
}

private void initializeProfiler() {
try {
private void initializeProfiler() throws Exception {
this.profiler = AsyncProfiler.getInstance();
// Check version to verify profiler is working
String version = profiler.execute("version");
logger.log(SentryLevel.DEBUG, "AsyncProfiler initialized successfully. Version: " + version);
} catch (Exception e) {
logger.log(
SentryLevel.WARNING,
"Failed to initialize AsyncProfiler. Profiling will be disabled.",
e);
this.profiler = null;
}
}

private boolean init() {
Expand All @@ -97,11 +90,6 @@ private boolean init() {
}
isInitialized = true;

if (profiler == null) {
logger.log(SentryLevel.ERROR, "Disabling profiling because AsyncProfiler is not available.");
return false;
}

if (profilingTracesDirPath == null) {
logger.log(
SentryLevel.WARNING,
Expand Down Expand Up @@ -206,11 +194,6 @@ private void start() {
startProfileChunkTimestamp = new SentryNanotimeDate();
}

if (profiler == null) {
logger.log(SentryLevel.ERROR, "Cannot start profiling: AsyncProfiler is not available");
return;
}

filename = profilingTracesDirPath + File.separator + SentryUUID.generateSentryId() + ".jfr";

File jfrFile = new File(filename);
Expand Down Expand Up @@ -294,11 +277,6 @@ private void stop(final boolean restartProfiler) {

File jfrFile = new File(filename);

if (profiler == null) {
logger.log(SentryLevel.WARNING, "Profiler is null when trying to stop");
return;
}

try {
profiler.execute("stop,jfr");
} catch (Exception e) {
Expand Down Expand Up @@ -406,7 +384,7 @@ private void sendChunks(final @NotNull IScopes scopes, final @NotNull SentryOpti
@Override
public boolean isRunning() {
try (final @NotNull ISentryLifecycleToken ignored = lock.acquire()) {
return isRunning && profiler != null && !filename.isEmpty();
return isRunning && !filename.isEmpty();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import io.sentry.IContinuousProfiler;
import io.sentry.ILogger;
import io.sentry.ISentryExecutorService;
import io.sentry.NoOpContinuousProfiler;
import io.sentry.SentryLevel;
import io.sentry.asyncprofiler.profiling.JavaContinuousProfiler;
import io.sentry.profiling.JavaContinuousProfilerProvider;
import io.sentry.profiling.JavaProfileConverterProvider;
Expand All @@ -22,7 +24,15 @@ public final class AsyncProfilerContinuousProfilerProvider
String profilingTracesDirPath,
int profilingTracesHz,
ISentryExecutorService executorService) {
try {
return new JavaContinuousProfiler(
logger, profilingTracesDirPath, profilingTracesHz, executorService);
} catch (Exception e) {
logger.log(
SentryLevel.WARNING,
"Failed to initialize AsyncProfiler. Profiling will be disabled.",
e);
return NoOpContinuousProfiler.getInstance();
}
}
}