Skip to content

Commit 7da193d

Browse files
authored
Merge 83b1f1a into bb0ff41
2 parents bb0ff41 + 83b1f1a commit 7da193d

25 files changed

+1416
-64
lines changed

sentry-android-core/api/sentry-android-core.api

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ public final class io/sentry/android/core/ActivityLifecycleIntegration : android
4141
}
4242

4343
public class io/sentry/android/core/AndroidContinuousProfiler : io/sentry/IContinuousProfiler, io/sentry/transport/RateLimiter$IRateLimitObserver {
44-
public fun <init> (Lio/sentry/android/core/BuildInfoProvider;Lio/sentry/android/core/internal/util/SentryFrameMetricsCollector;Lio/sentry/ILogger;Ljava/lang/String;ILio/sentry/util/LazyEvaluator$Evaluator;)V
4544
public fun close (Z)V
45+
public static fun createLegacy (Lio/sentry/android/core/BuildInfoProvider;Lio/sentry/android/core/internal/util/SentryFrameMetricsCollector;Lio/sentry/ILogger;Ljava/lang/String;ILio/sentry/util/LazyEvaluator$Evaluator;)Lio/sentry/android/core/AndroidContinuousProfiler;
4646
public fun getChunkId ()Lio/sentry/protocol/SentryId;
4747
public fun getProfilerId ()Lio/sentry/protocol/SentryId;
4848
public fun getRootSpanCounter ()I
@@ -338,6 +338,25 @@ public final class io/sentry/android/core/NetworkBreadcrumbsIntegration : io/sen
338338
public fun register (Lio/sentry/IScopes;Lio/sentry/SentryOptions;)V
339339
}
340340

341+
public class io/sentry/android/core/PerfettoContinuousProfiler : io/sentry/IContinuousProfiler, io/sentry/transport/RateLimiter$IRateLimitObserver {
342+
public fun <init> (Landroid/content/Context;Lio/sentry/android/core/BuildInfoProvider;Lio/sentry/android/core/internal/util/SentryFrameMetricsCollector;Lio/sentry/ILogger;Lio/sentry/util/LazyEvaluator$Evaluator;)V
343+
public fun close (Z)V
344+
public fun getActiveTraceCount ()I
345+
public fun getChunkId ()Lio/sentry/protocol/SentryId;
346+
public fun getProfilerId ()Lio/sentry/protocol/SentryId;
347+
public fun isRunning ()Z
348+
public fun onRateLimitChanged (Lio/sentry/transport/RateLimiter;)V
349+
public fun reevaluateSampling ()V
350+
public fun startProfiler (Lio/sentry/ProfileLifecycle;Lio/sentry/TracesSampler;)V
351+
public fun stopProfiler (Lio/sentry/ProfileLifecycle;)V
352+
}
353+
354+
public class io/sentry/android/core/PerfettoProfiler {
355+
public fun <init> (Landroid/content/Context;Lio/sentry/android/core/internal/util/SentryFrameMetricsCollector;Lio/sentry/ILogger;)V
356+
public fun endAndCollect ()Lio/sentry/android/core/AndroidProfiler$ProfileEndData;
357+
public fun start (J)Lio/sentry/android/core/AndroidProfiler$ProfileStartData;
358+
}
359+
341360
public final class io/sentry/android/core/ScreenshotEventProcessor : io/sentry/EventProcessor {
342361
public fun <init> (Lio/sentry/android/core/SentryAndroidOptions;Lio/sentry/android/core/BuildInfoProvider;Z)V
343362
public fun getOrder ()Ljava/lang/Long;

sentry-android-core/src/main/java/io/sentry/android/core/AndroidContinuousProfiler.java

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,29 @@ public class AndroidContinuousProfiler
6868
private final AutoClosableReentrantLock lock = new AutoClosableReentrantLock();
6969
private final AutoClosableReentrantLock payloadLock = new AutoClosableReentrantLock();
7070

71-
public AndroidContinuousProfiler(
71+
public static AndroidContinuousProfiler createLegacy(
7272
final @NotNull BuildInfoProvider buildInfoProvider,
7373
final @NotNull SentryFrameMetricsCollector frameMetricsCollector,
7474
final @NotNull ILogger logger,
7575
final @Nullable String profilingTracesDirPath,
7676
final int profilingTracesHz,
7777
final @NotNull LazyEvaluator.Evaluator<ISentryExecutorService> executorServiceSupplier) {
78+
return new AndroidContinuousProfiler(
79+
buildInfoProvider,
80+
frameMetricsCollector,
81+
executorServiceSupplier,
82+
logger,
83+
profilingTracesHz,
84+
profilingTracesDirPath);
85+
}
86+
87+
private AndroidContinuousProfiler(
88+
final @NotNull BuildInfoProvider buildInfoProvider,
89+
final @NotNull SentryFrameMetricsCollector frameMetricsCollector,
90+
final @NotNull LazyEvaluator.Evaluator<ISentryExecutorService> executorServiceSupplier,
91+
final @NotNull ILogger logger,
92+
final int profilingTracesHz,
93+
final @Nullable String profilingTracesDirPath) {
7894
this.logger = logger;
7995
this.frameMetricsCollector = frameMetricsCollector;
8096
this.buildInfoProvider = buildInfoProvider;
@@ -89,6 +105,7 @@ private void init() {
89105
return;
90106
}
91107
isInitialized = true;
108+
92109
if (profilingTracesDirPath == null) {
93110
logger.log(
94111
SentryLevel.WARNING,
@@ -117,6 +134,7 @@ public void startProfiler(
117134
final @NotNull ProfileLifecycle profileLifecycle,
118135
final @NotNull TracesSampler tracesSampler) {
119136
try (final @NotNull ISentryLifecycleToken ignored = lock.acquire()) {
137+
shouldStop = false;
120138
if (shouldSample) {
121139
isSampled = tracesSampler.sampleSessionProfile(SentryRandom.current().nextDouble());
122140
shouldSample = false;
@@ -152,21 +170,24 @@ public void startProfiler(
152170
}
153171
}
154172

155-
private void initScopes() {
173+
private void tryResolveScopes() {
156174
if ((scopes == null || scopes == NoOpScopes.getInstance())
157175
&& Sentry.getCurrentScopes() != NoOpScopes.getInstance()) {
158-
this.scopes = Sentry.getCurrentScopes();
159-
this.performanceCollector =
160-
Sentry.getCurrentScopes().getOptions().getCompositePerformanceCollector();
161-
final @Nullable RateLimiter rateLimiter = scopes.getRateLimiter();
162-
if (rateLimiter != null) {
163-
rateLimiter.addRateLimitObserver(this);
164-
}
176+
onScopesAvailable(Sentry.getCurrentScopes());
177+
}
178+
}
179+
180+
private void onScopesAvailable(final @NotNull IScopes resolvedScopes) {
181+
this.scopes = resolvedScopes;
182+
this.performanceCollector = resolvedScopes.getOptions().getCompositePerformanceCollector();
183+
final @Nullable RateLimiter rateLimiter = resolvedScopes.getRateLimiter();
184+
if (rateLimiter != null) {
185+
rateLimiter.addRateLimitObserver(this);
165186
}
166187
}
167188

168189
private void start() {
169-
initScopes();
190+
tryResolveScopes();
170191

171192
// Debug.startMethodTracingSampling() is only available since Lollipop, but Android Profiler
172193
// causes crashes on api 21 -> https://github.com/getsentry/sentry-java/issues/3392
@@ -259,7 +280,7 @@ public void stopProfiler(final @NotNull ProfileLifecycle profileLifecycle) {
259280
}
260281

261282
private void stop(final boolean restartProfiler) {
262-
initScopes();
283+
tryResolveScopes();
263284
try (final @NotNull ISentryLifecycleToken ignored = lock.acquire()) {
264285
if (stopFuture != null) {
265286
stopFuture.cancel(true);
@@ -297,14 +318,15 @@ private void stop(final boolean restartProfiler) {
297318
// start profiling), meaning there's no scopes to send the chunks. In that case, we store
298319
// the data in a list and send it when the next chunk is finished.
299320
try (final @NotNull ISentryLifecycleToken ignored2 = payloadLock.acquire()) {
300-
payloadBuilders.add(
321+
final ProfileChunk.Builder builder =
301322
new ProfileChunk.Builder(
302323
profilerId,
303324
chunkId,
304325
endData.measurementsMap,
305326
endData.traceFile,
306327
startProfileChunkTimestamp,
307-
ProfileChunk.PLATFORM_ANDROID));
328+
ProfileChunk.PLATFORM_ANDROID);
329+
payloadBuilders.add(builder);
308330
}
309331
}
310332

sentry-android-core/src/main/java/io/sentry/android/core/AndroidOptionsInitializer.java

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import static io.sentry.android.core.NdkIntegration.SENTRY_NDK_CLASS_NAME;
44

5+
import android.annotation.SuppressLint;
56
import android.app.Application;
67
import android.content.Context;
78
import android.content.pm.PackageInfo;
@@ -293,6 +294,7 @@ static void initializeIntegrationsAndProcessors(
293294
}
294295

295296
/** Setup the correct profiler (transaction or continuous) based on the options. */
297+
@SuppressLint("NewApi")
296298
private static void setupProfiler(
297299
final @NotNull SentryAndroidOptions options,
298300
final @NotNull Context context,
@@ -335,16 +337,25 @@ private static void setupProfiler(
335337
performanceCollector.start(chunkId.toString());
336338
}
337339
} else {
340+
final @NotNull SentryFrameMetricsCollector frameMetricsCollector =
341+
Objects.requireNonNull(
342+
options.getFrameMetricsCollector(), "options.getFrameMetricsCollector is required");
338343
options.setContinuousProfiler(
339-
new AndroidContinuousProfiler(
340-
buildInfoProvider,
341-
Objects.requireNonNull(
342-
options.getFrameMetricsCollector(),
343-
"options.getFrameMetricsCollector is required"),
344-
options.getLogger(),
345-
options.getProfilingTracesDirPath(),
346-
options.getProfilingTracesHz(),
347-
() -> options.getExecutorService()));
344+
options.isUseProfilingManager()
345+
? new PerfettoContinuousProfiler(
346+
buildInfoProvider,
347+
options.getLogger(),
348+
() -> options.getExecutorService(),
349+
() ->
350+
new PerfettoProfiler(
351+
context.getApplicationContext(), frameMetricsCollector, options.getLogger()))
352+
: AndroidContinuousProfiler.createLegacy(
353+
buildInfoProvider,
354+
frameMetricsCollector,
355+
options.getLogger(),
356+
options.getProfilingTracesDirPath(),
357+
options.getProfilingTracesHz(),
358+
() -> options.getExecutorService()));
348359
}
349360
}
350361
}

sentry-android-core/src/main/java/io/sentry/android/core/ManifestMetadataReader.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,8 @@ final class ManifestMetadataReader {
108108

109109
static final String ENABLE_APP_START_PROFILING = "io.sentry.profiling.enable-app-start";
110110

111+
static final String USE_PROFILING_MANAGER = "io.sentry.profiling.use-profiling-manager";
112+
111113
static final String ENABLE_SCOPE_PERSISTENCE = "io.sentry.enable-scope-persistence";
112114

113115
static final String REPLAYS_SESSION_SAMPLE_RATE = "io.sentry.session-replay.session-sample-rate";
@@ -497,6 +499,9 @@ static void applyMetadata(
497499
readBool(
498500
metadata, logger, ENABLE_APP_START_PROFILING, options.isEnableAppStartProfiling()));
499501

502+
options.setUseProfilingManager(
503+
readBool(metadata, logger, USE_PROFILING_MANAGER, options.isUseProfilingManager()));
504+
500505
options.setEnableScopePersistence(
501506
readBool(
502507
metadata, logger, ENABLE_SCOPE_PERSISTENCE, options.isEnableScopePersistence()));

0 commit comments

Comments
 (0)