Skip to content

Commit 3f83146

Browse files
committed
port relevant AndroidContinuousProfilerTest tests to JavaContinuousProfilerTest
1 parent 9bcd4ea commit 3f83146

File tree

2 files changed

+449
-15
lines changed

2 files changed

+449
-15
lines changed

sentry-async-profiler/src/main/java/io/sentry/asyncprofiler/profiling/JavaContinuousProfiler.java

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ public final class JavaContinuousProfiler
5656
private @NotNull SentryId chunkId = SentryId.EMPTY_ID;
5757
private final @NotNull AtomicBoolean isClosed = new AtomicBoolean(false);
5858
private @NotNull SentryDate startProfileChunkTimestamp = new SentryNanotimeDate();
59-
private final @NotNull String profilingIntervalMicros;
6059

6160
private @NotNull String filename = "";
6261

@@ -79,29 +78,28 @@ public JavaContinuousProfiler(
7978
this.profilingTracesHz = profilingTracesHz;
8079
this.executorService = executorService;
8180
this.profiler = AsyncProfiler.getInstance();
82-
this.profilingIntervalMicros =
83-
String.format("%dus", (int) SECONDS.toMicros(1) / profilingTracesHz);
8481
}
8582

86-
private void init() {
83+
private boolean init() {
8784
// We initialize it only once
8885
if (isInitialized) {
89-
return;
86+
return true;
9087
}
9188
isInitialized = true;
9289
if (profilingTracesDirPath == null) {
9390
logger.log(
9491
SentryLevel.WARNING,
9592
"Disabling profiling because no profiling traces dir path is defined in options.");
96-
return;
93+
return false;
9794
}
9895
if (profilingTracesHz <= 0) {
9996
logger.log(
10097
SentryLevel.WARNING,
10198
"Disabling profiling because trace rate is set to %d",
10299
profilingTracesHz);
103-
return;
100+
return false;
104101
}
102+
return true;
105103
}
106104

107105
@SuppressWarnings("ReferenceEquality")
@@ -150,7 +148,8 @@ public void startProfiler(
150148
private void initScopes() {
151149
if ((scopes == null || scopes == NoOpScopes.getInstance())
152150
&& Sentry.getCurrentScopes() != NoOpScopes.getInstance()) {
153-
this.scopes = Sentry.forkedRootScopes("profiler");
151+
// TODO: should we fork the scopes here?
152+
this.scopes = Sentry.getCurrentScopes();
154153
final @Nullable RateLimiter rateLimiter = scopes.getRateLimiter();
155154
if (rateLimiter != null) {
156155
rateLimiter.addRateLimitObserver(this);
@@ -163,7 +162,9 @@ private void start() {
163162
initScopes();
164163

165164
// Let's initialize trace folder and profiling interval
166-
init();
165+
if (!init()) {
166+
return;
167+
}
167168

168169
if (scopes != null) {
169170
final @Nullable RateLimiter rateLimiter = scopes.getRateLimiter();
@@ -188,18 +189,18 @@ private void start() {
188189
} else {
189190
startProfileChunkTimestamp = new SentryNanotimeDate();
190191
}
191-
filename = SentryUUID.generateSentryId() + ".jfr";
192-
final String startData;
192+
filename = profilingTracesDirPath + File.separator + SentryUUID.generateSentryId() + ".jfr";
193+
String startData = null;
193194
try {
194-
// final String command =
195-
// String.format("start,jfr,event=cpu,wall=%s,file=%s",profilingIntervalMicros, filename);
195+
final String profilingIntervalMicros =
196+
String.format("%dus", (int) SECONDS.toMicros(1) / profilingTracesHz);
196197
final String command =
197198
String.format(
198199
"start,jfr,event=wall,interval=%s,file=%s", profilingIntervalMicros, filename);
199200
System.out.println(command);
200201
startData = profiler.execute(command);
201-
} catch (IOException e) {
202-
throw new RuntimeException(e);
202+
} catch (Exception e) {
203+
logger.log(SentryLevel.ERROR, "Failed to start profiling: ", e);
203204
}
204205
// check if profiling started
205206
if (startData == null) {

0 commit comments

Comments
 (0)