From 7452696df0274f63dc67d259165be5511a06e4b5 Mon Sep 17 00:00:00 2001 From: Nelson Osacky Date: Tue, 1 Dec 2026 16:21:09 +0100 Subject: [PATCH] ref(core): Inject the envelope disk cache from SentryRuntime The SentryClient, its transport, and the previous-session tasks now get the envelope disk cache through their constructors instead of reading it off SentryOptions. The runtime creates the default cache, and Android puts its AndroidEnvelopeCache on the runtime builder. The public constructors that read the cache off the options keep working. Part of JAVA-817 Co-Authored-By: Claude Opus 5.5 --- .../core/AndroidOptionsInitializer.java | 9 +++-- .../android/core/InternalSentrySdk.java | 7 +++- .../io/sentry/android/core/SentryAndroid.java | 1 + .../core/AndroidContinuousProfilerTest.kt | 2 + .../core/AndroidOptionsInitializerTest.kt | 9 ++++- .../android/core/AndroidProfilerTest.kt | 2 + .../core/AndroidTransactionProfilerTest.kt | 2 + sentry/api/sentry.api | 4 ++ .../io/sentry/AsyncHttpTransportFactory.java | 13 +++++++ .../java/io/sentry/MovePreviousSession.java | 5 ++- .../io/sentry/PreviousSessionFinalizer.java | 14 +++++-- sentry/src/main/java/io/sentry/Sentry.java | 37 +++++++++---------- .../src/main/java/io/sentry/SentryClient.java | 7 +++- .../io/sentry/internal/SentryRuntime.java | 4 ++ .../sentry/transport/AsyncHttpTransport.java | 34 +++++++++++++++-- .../java/io/sentry/MovePreviousSessionTest.kt | 13 ++++--- .../io/sentry/PreviousSessionFinalizerTest.kt | 10 ++--- 17 files changed, 125 insertions(+), 48 deletions(-) diff --git a/sentry-android-core/src/main/java/io/sentry/android/core/AndroidOptionsInitializer.java b/sentry-android-core/src/main/java/io/sentry/android/core/AndroidOptionsInitializer.java index 52a54b339dc..3ec8f47ad93 100644 --- a/sentry-android-core/src/main/java/io/sentry/android/core/AndroidOptionsInitializer.java +++ b/sentry-android-core/src/main/java/io/sentry/android/core/AndroidOptionsInitializer.java @@ -48,13 +48,13 @@ import io.sentry.cache.PersistingScopeObserver; import io.sentry.compose.gestures.ComposeGestureTargetLocator; import io.sentry.compose.viewhierarchy.ComposeViewHierarchyExporter; +import io.sentry.internal.SentryRuntime; import io.sentry.internal.debugmeta.NoOpDebugMetaLoader; import io.sentry.internal.gestures.GestureTargetLocator; import io.sentry.internal.modules.NoOpModulesLoader; import io.sentry.internal.viewhierarchy.ViewHierarchyExporter; import io.sentry.protocol.SentryId; import io.sentry.transport.CurrentDateProvider; -import io.sentry.transport.NoOpEnvelopeCache; import io.sentry.transport.NoOpTransportGate; import io.sentry.util.LazyEvaluator; import io.sentry.util.Objects; @@ -155,6 +155,7 @@ static void initializeIntegrationsAndProcessors( final boolean isReplayAvailable) { initializeIntegrationsAndProcessors( options, + SentryRuntime.builder(), context, new BuildInfoProvider(new AndroidLogger()), loadClass, @@ -164,15 +165,15 @@ static void initializeIntegrationsAndProcessors( static void initializeIntegrationsAndProcessors( final @NotNull SentryAndroidOptions options, + final @NotNull SentryRuntime.Builder runtime, final @NotNull Context context, final @NotNull BuildInfoProvider buildInfoProvider, final @NotNull io.sentry.util.LoadClass loadClass, final @NotNull ActivityFramesTracker activityFramesTracker, final boolean isReplayAvailable) { - if (options.getCacheDirPath() != null - && options.getEnvelopeDiskCache() instanceof NoOpEnvelopeCache) { - options.setEnvelopeDiskCache(new AndroidEnvelopeCache(options)); + if (options.getCacheDirPath() != null && !runtime.hasEnvelopeDiskCache()) { + runtime.envelopeDiskCache(new AndroidEnvelopeCache(options)); } if (options.getConnectionStatusProvider() instanceof NoOpConnectionStatusProvider) { diff --git a/sentry-android-core/src/main/java/io/sentry/android/core/InternalSentrySdk.java b/sentry-android-core/src/main/java/io/sentry/android/core/InternalSentrySdk.java index bd8f4d791c4..be844a471d3 100644 --- a/sentry-android-core/src/main/java/io/sentry/android/core/InternalSentrySdk.java +++ b/sentry-android-core/src/main/java/io/sentry/android/core/InternalSentrySdk.java @@ -27,6 +27,7 @@ import io.sentry.android.core.performance.AppStartMetrics; import io.sentry.android.core.performance.TimeSpan; import io.sentry.cache.EnvelopeCache; +import io.sentry.cache.IEnvelopeCache; import io.sentry.internal.SentryRuntime; import io.sentry.protocol.App; import io.sentry.protocol.Device; @@ -316,6 +317,8 @@ public static void updateSessionForDroppedEventNonTerminating(final boolean cras private static void updateSessionNonTerminating(final boolean unhandled) { final @NotNull IScopes scopes = ScopesAdapter.getInstance(); final @NotNull SentryOptions options = scopes.getOptions(); + final @Nullable SentryRuntime runtime = SentryRuntime.getCurrent(); + final @Nullable IEnvelopeCache cache = runtime != null ? runtime.getEnvelopeDiskCache() : null; scopes.configureScope(scope -> scope.withSession(session -> { if (session == null) { @@ -330,8 +333,8 @@ private static void updateSessionNonTerminating(final boolean unhandled) { unhandled ? session.recordNonTerminatingUnhandledError() : session.update(null, null, true, null); - if (recorded && options.getEnvelopeDiskCache() instanceof EnvelopeCache) { - ((EnvelopeCache) options.getEnvelopeDiskCache()).persistCurrentSession(session); + if (recorded && cache instanceof EnvelopeCache) { + ((EnvelopeCache) cache).persistCurrentSession(session); } })); } diff --git a/sentry-android-core/src/main/java/io/sentry/android/core/SentryAndroid.java b/sentry-android-core/src/main/java/io/sentry/android/core/SentryAndroid.java index c8d9039a452..89da056d8c1 100644 --- a/sentry-android-core/src/main/java/io/sentry/android/core/SentryAndroid.java +++ b/sentry-android-core/src/main/java/io/sentry/android/core/SentryAndroid.java @@ -203,6 +203,7 @@ static void init( AndroidOptionsInitializer.initializeIntegrationsAndProcessors( options, + runtime, context, buildInfoProvider, loadClass, diff --git a/sentry-android-core/src/test/java/io/sentry/android/core/AndroidContinuousProfilerTest.kt b/sentry-android-core/src/test/java/io/sentry/android/core/AndroidContinuousProfilerTest.kt index 5a27a303342..94639b27fb0 100644 --- a/sentry-android-core/src/test/java/io/sentry/android/core/AndroidContinuousProfilerTest.kt +++ b/sentry-android-core/src/test/java/io/sentry/android/core/AndroidContinuousProfilerTest.kt @@ -16,6 +16,7 @@ import io.sentry.SentryTracer import io.sentry.TracesSampler import io.sentry.TransactionContext import io.sentry.android.core.internal.util.SentryFrameMetricsCollector +import io.sentry.internal.SentryRuntime import io.sentry.profilemeasurements.ProfileMeasurement import io.sentry.test.DeferredExecutorService import io.sentry.test.getProperty @@ -126,6 +127,7 @@ class AndroidContinuousProfilerTest { AndroidOptionsInitializer.initializeIntegrationsAndProcessors( fixture.options, + SentryRuntime.builder(), context, buildInfoProvider, loadClass, diff --git a/sentry-android-core/src/test/java/io/sentry/android/core/AndroidOptionsInitializerTest.kt b/sentry-android-core/src/test/java/io/sentry/android/core/AndroidOptionsInitializerTest.kt index 226ea753977..9a51fae3b21 100644 --- a/sentry-android-core/src/test/java/io/sentry/android/core/AndroidOptionsInitializerTest.kt +++ b/sentry-android-core/src/test/java/io/sentry/android/core/AndroidOptionsInitializerTest.kt @@ -34,6 +34,7 @@ import io.sentry.cache.IEnvelopeCache import io.sentry.cache.PersistingOptionsObserver import io.sentry.cache.PersistingScopeObserver import io.sentry.compose.gestures.ComposeGestureTargetLocator +import io.sentry.internal.SentryRuntime import io.sentry.internal.debugmeta.IDebugMetaLoader import io.sentry.internal.modules.IModulesLoader import io.sentry.protocol.SentryId @@ -67,6 +68,7 @@ private const val SAAS_DSN = "https://key@o1.ingest.us.sentry.io/1" class AndroidOptionsInitializerTest { class Fixture(val context: Context, private val file: File) { val sentryOptions = SentryAndroidOptions() + val runtime = SentryRuntime.builder() lateinit var mockContext: Context val logger = mock() @@ -117,7 +119,9 @@ class AndroidOptionsInitializerTest { sentryOptions.configureOptions() AndroidOptionsInitializer.initializeIntegrationsAndProcessors( sentryOptions, + runtime, if (useRealContext) context else mockContext, + BuildInfoProvider(AndroidLogger()), loadClass, activityFramesTracker, false, @@ -166,6 +170,7 @@ class AndroidOptionsInitializerTest { AndroidOptionsInitializer.initializeIntegrationsAndProcessors( sentryOptions, + runtime, context, buildInfo, loadClass, @@ -798,10 +803,10 @@ class AndroidOptionsInitializerTest { } @Test - fun `AndroidEnvelopeCache is set to options`() { + fun `AndroidEnvelopeCache is set on the runtime`() { fixture.initSut() - assertTrue { fixture.sentryOptions.envelopeDiskCache is AndroidEnvelopeCache } + assertTrue { fixture.runtime.buildForTest().envelopeDiskCache is AndroidEnvelopeCache } } @Test diff --git a/sentry-android-core/src/test/java/io/sentry/android/core/AndroidProfilerTest.kt b/sentry-android-core/src/test/java/io/sentry/android/core/AndroidProfilerTest.kt index 8f493343d1e..4f01cec0834 100644 --- a/sentry-android-core/src/test/java/io/sentry/android/core/AndroidProfilerTest.kt +++ b/sentry-android-core/src/test/java/io/sentry/android/core/AndroidProfilerTest.kt @@ -9,6 +9,7 @@ import io.sentry.PerformanceCollectionData import io.sentry.SentryExecutorService import io.sentry.SentryLevel import io.sentry.android.core.internal.util.SentryFrameMetricsCollector +import io.sentry.internal.SentryRuntime import io.sentry.profilemeasurements.ProfileMeasurement import io.sentry.test.getCtor import io.sentry.test.getProperty @@ -131,6 +132,7 @@ class AndroidProfilerTest { AndroidOptionsInitializer.initializeIntegrationsAndProcessors( fixture.options, + SentryRuntime.builder(), context, buildInfoProvider, loadClass, diff --git a/sentry-android-core/src/test/java/io/sentry/android/core/AndroidTransactionProfilerTest.kt b/sentry-android-core/src/test/java/io/sentry/android/core/AndroidTransactionProfilerTest.kt index b37a6bbdee5..2d12a9fff89 100644 --- a/sentry-android-core/src/test/java/io/sentry/android/core/AndroidTransactionProfilerTest.kt +++ b/sentry-android-core/src/test/java/io/sentry/android/core/AndroidTransactionProfilerTest.kt @@ -13,6 +13,7 @@ import io.sentry.SentryLevel import io.sentry.SentryTracer import io.sentry.TransactionContext import io.sentry.android.core.internal.util.SentryFrameMetricsCollector +import io.sentry.internal.SentryRuntime import io.sentry.profilemeasurements.ProfileMeasurement import io.sentry.test.getCtor import io.sentry.test.getProperty @@ -146,6 +147,7 @@ class AndroidTransactionProfilerTest { AndroidOptionsInitializer.initializeIntegrationsAndProcessors( fixture.options, + SentryRuntime.builder(), context, buildInfoProvider, loadClass, diff --git a/sentry/api/sentry.api b/sentry/api/sentry.api index 7dde4d7bf74..02136ab0503 100644 --- a/sentry/api/sentry.api +++ b/sentry/api/sentry.api @@ -1,5 +1,6 @@ public final class io/sentry/AsyncHttpTransportFactory : io/sentry/ITransportFactory { public fun ()V + public fun (Lio/sentry/cache/IEnvelopeCache;)V public fun create (Lio/sentry/SentryOptions;Lio/sentry/RequestDetails;)Lio/sentry/transport/ITransport; } @@ -5484,6 +5485,7 @@ public final class io/sentry/internal/SentryRuntime$Builder { public fun buildForTest ()Lio/sentry/internal/SentryRuntime; public fun envelopeDiskCache (Lio/sentry/cache/IEnvelopeCache;)Lio/sentry/internal/SentryRuntime$Builder; public fun executorService (Lio/sentry/ISentryExecutorService;)Lio/sentry/internal/SentryRuntime$Builder; + public fun hasEnvelopeDiskCache ()Z public fun hasExecutorService ()Z public fun lazySerializer (Lio/sentry/util/LazyEvaluator$Evaluator;)Lio/sentry/internal/SentryRuntime$Builder; public fun serializer (Lio/sentry/ISerializer;)Lio/sentry/internal/SentryRuntime$Builder; @@ -7815,7 +7817,9 @@ public final class io/sentry/time/Timestamp { } public final class io/sentry/transport/AsyncHttpTransport : io/sentry/transport/ITransport { + public fun (Lio/sentry/SentryOptions;Lio/sentry/cache/IEnvelopeCache;Lio/sentry/transport/RateLimiter;Lio/sentry/transport/ITransportGate;Lio/sentry/RequestDetails;)V public fun (Lio/sentry/SentryOptions;Lio/sentry/transport/RateLimiter;Lio/sentry/transport/ITransportGate;Lio/sentry/RequestDetails;)V + public fun (Lio/sentry/transport/QueuedThreadPoolExecutor;Lio/sentry/SentryOptions;Lio/sentry/cache/IEnvelopeCache;Lio/sentry/transport/RateLimiter;Lio/sentry/transport/ITransportGate;Lio/sentry/transport/HttpConnection;)V public fun (Lio/sentry/transport/QueuedThreadPoolExecutor;Lio/sentry/SentryOptions;Lio/sentry/transport/RateLimiter;Lio/sentry/transport/ITransportGate;Lio/sentry/transport/HttpConnection;)V public fun close ()V public fun close (Z)V diff --git a/sentry/src/main/java/io/sentry/AsyncHttpTransportFactory.java b/sentry/src/main/java/io/sentry/AsyncHttpTransportFactory.java index 02a47e79315..b9a50fbf958 100644 --- a/sentry/src/main/java/io/sentry/AsyncHttpTransportFactory.java +++ b/sentry/src/main/java/io/sentry/AsyncHttpTransportFactory.java @@ -1,16 +1,28 @@ package io.sentry; +import io.sentry.cache.IEnvelopeCache; import io.sentry.transport.AsyncHttpTransport; import io.sentry.transport.ITransport; import io.sentry.transport.RateLimiter; import io.sentry.util.Objects; import org.jetbrains.annotations.ApiStatus; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; /** Creates {@link AsyncHttpTransport}. */ @ApiStatus.Internal public final class AsyncHttpTransportFactory implements ITransportFactory { + private final @Nullable IEnvelopeCache envelopeCache; + + public AsyncHttpTransportFactory() { + this.envelopeCache = null; + } + + public AsyncHttpTransportFactory(final @NotNull IEnvelopeCache envelopeCache) { + this.envelopeCache = envelopeCache; + } + @Override public @NotNull ITransport create( final @NotNull SentryOptions options, final @NotNull RequestDetails requestDetails) { @@ -19,6 +31,7 @@ public final class AsyncHttpTransportFactory implements ITransportFactory { return new AsyncHttpTransport( options, + envelopeCache != null ? envelopeCache : options.getEnvelopeDiskCache(), RateLimiter.create(options.getMonotonicTicker(), options), options.getTransportGate(), requestDetails); diff --git a/sentry/src/main/java/io/sentry/MovePreviousSession.java b/sentry/src/main/java/io/sentry/MovePreviousSession.java index 6b7de16d81a..2aa03a10c4a 100644 --- a/sentry/src/main/java/io/sentry/MovePreviousSession.java +++ b/sentry/src/main/java/io/sentry/MovePreviousSession.java @@ -10,9 +10,11 @@ final class MovePreviousSession implements Runnable { private final @NotNull SentryOptions options; + private final @NotNull IEnvelopeCache cache; - MovePreviousSession(final @NotNull SentryOptions options) { + MovePreviousSession(final @NotNull SentryOptions options, final @NotNull IEnvelopeCache cache) { this.options = options; + this.cache = cache; } @Override @@ -23,7 +25,6 @@ public void run() { return; } - final IEnvelopeCache cache = options.getEnvelopeDiskCache(); if (cache instanceof EnvelopeCache) { final File currentSessionFile = EnvelopeCache.getCurrentSessionFile(cacheDirPath); final File previousSessionFile = EnvelopeCache.getPreviousSessionFile(cacheDirPath); diff --git a/sentry/src/main/java/io/sentry/PreviousSessionFinalizer.java b/sentry/src/main/java/io/sentry/PreviousSessionFinalizer.java index 4e3b2e9b3d2..bae816c0571 100644 --- a/sentry/src/main/java/io/sentry/PreviousSessionFinalizer.java +++ b/sentry/src/main/java/io/sentry/PreviousSessionFinalizer.java @@ -35,9 +35,19 @@ final class PreviousSessionFinalizer implements Runnable { private final @NotNull IScopes scopes; - PreviousSessionFinalizer(final @NotNull SentryOptions options, final @NotNull IScopes scopes) { + private final @NotNull IEnvelopeCache cache; + + private final @NotNull ISerializer serializer; + + PreviousSessionFinalizer( + final @NotNull SentryOptions options, + final @NotNull IScopes scopes, + final @NotNull IEnvelopeCache cache, + final @NotNull ISerializer serializer) { this.options = options; this.scopes = scopes; + this.cache = cache; + this.serializer = serializer; } @Override @@ -48,7 +58,6 @@ public void run() { return; } - final IEnvelopeCache cache = options.getEnvelopeDiskCache(); if (cache instanceof EnvelopeCache) { if (!((EnvelopeCache) cache).waitPreviousSessionFlush()) { options @@ -61,7 +70,6 @@ public void run() { } final File previousSessionFile = EnvelopeCache.getPreviousSessionFile(cacheDirPath); - final ISerializer serializer = options.getSerializer(); if (previousSessionFile.exists()) { options.getLogger().log(WARNING, "Current session is not ended, we'd need to end it."); diff --git a/sentry/src/main/java/io/sentry/Sentry.java b/sentry/src/main/java/io/sentry/Sentry.java index aa8fdff4eb3..6553e456740 100644 --- a/sentry/src/main/java/io/sentry/Sentry.java +++ b/sentry/src/main/java/io/sentry/Sentry.java @@ -2,8 +2,6 @@ import io.sentry.backpressure.BackpressureMonitor; import io.sentry.backpressure.NoOpBackpressureMonitor; -import io.sentry.cache.EnvelopeCache; -import io.sentry.cache.IEnvelopeCache; import io.sentry.cache.PersistingScopeObserver; import io.sentry.config.PropertiesProviderFactory; import io.sentry.internal.InternalIntegration; @@ -22,7 +20,6 @@ import io.sentry.protocol.Feedback; import io.sentry.protocol.SentryId; import io.sentry.protocol.User; -import io.sentry.transport.NoOpEnvelopeCache; import io.sentry.util.AutoClosableReentrantLock; import io.sentry.util.DebugMetaPropertiesApplier; import io.sentry.util.FileUtils; @@ -381,13 +378,13 @@ private static void init( getScopesStorage().set(rootScopes); initConfigurations(options); - globalScope.bindClient(new SentryClient(options)); - // Also replaces an executor that a previous init closed, when the options are reused. final @NotNull SentryRuntime runtime = LegacyOptionsBridge.buildRuntime(runtimeBuilder, options); SentryRuntime.setCurrent(runtime); + globalScope.bindClient(new SentryClient(options, runtime.getEnvelopeDiskCache())); + if (options.getTimerExecutorService().isClosed()) { options.setTimerExecutorService( new SentryExecutorService( @@ -406,7 +403,7 @@ private static void init( e); } - movePreviousSession(options); + movePreviousSession(options, runtime); // when integrations are registered on Scopes ctor and async integrations are fired, // it might and actually happened that integrations called captureSomething // and Scopes was still NoOp. @@ -431,7 +428,7 @@ private static void init( notifyOptionsObservers(options); - finalizePreviousSession(options, ScopesAdapter.getInstance()); + finalizePreviousSession(options, runtime, ScopesAdapter.getInstance()); handleAppStartProfilingConfig(options, options.getExecutorService()); @@ -574,10 +571,13 @@ private static void handleAppStartProfilingConfig( } @SuppressWarnings("FutureReturnValueIgnored") - private static void movePreviousSession(final @NotNull SentryOptions options) { + private static void movePreviousSession( + final @NotNull SentryOptions options, final @NotNull SentryRuntime runtime) { // enqueue a task to move previous unfinished session to its own file try { - options.getExecutorService().submit(new MovePreviousSession(options)); + options + .getExecutorService() + .submit(new MovePreviousSession(options, runtime.getEnvelopeDiskCache())); } catch (Throwable e) { options.getLogger().log(SentryLevel.DEBUG, "Failed to move previous session.", e); } @@ -585,12 +585,18 @@ private static void movePreviousSession(final @NotNull SentryOptions options) { @SuppressWarnings("FutureReturnValueIgnored") private static void finalizePreviousSession( - final @NotNull SentryOptions options, final @NotNull IScopes scopes) { + final @NotNull SentryOptions options, + final @NotNull SentryRuntime runtime, + final @NotNull IScopes scopes) { // enqueue a task to finalize previous session. Since the executor // is single-threaded, this task will be enqueued sequentially after all integrations that have // to modify the previous session have done their work, even if they do that async. try { - options.getExecutorService().submit(new PreviousSessionFinalizer(options, scopes)); + options + .getExecutorService() + .submit( + new PreviousSessionFinalizer( + options, scopes, runtime.getEnvelopeDiskCache(), runtime.getSerializer())); } catch (Throwable e) { options.getLogger().log(SentryLevel.DEBUG, "Failed to finalize previous session.", e); } @@ -673,15 +679,6 @@ private static void initConfigurations(final @NotNull SentryOptions options) { logger.log(SentryLevel.INFO, "No outbox dir path is defined in options."); } - final String cacheDirPath = options.getCacheDirPath(); - if (cacheDirPath != null) { - final IEnvelopeCache envelopeCache = options.getEnvelopeDiskCache(); - // only overwrite the cache impl if it's not already set - if (envelopeCache instanceof NoOpEnvelopeCache) { - options.setEnvelopeDiskCache(EnvelopeCache.create(options)); - } - } - final String profilingTracesDirPath = options.getProfilingTracesDirPath(); if ((options.isProfilingEnabled() || options.isContinuousProfilingEnabled()) && profilingTracesDirPath != null) { diff --git a/sentry/src/main/java/io/sentry/SentryClient.java b/sentry/src/main/java/io/sentry/SentryClient.java index 9e8b49b169b..a80fc1e3ed7 100644 --- a/sentry/src/main/java/io/sentry/SentryClient.java +++ b/sentry/src/main/java/io/sentry/SentryClient.java @@ -1,5 +1,6 @@ package io.sentry; +import io.sentry.cache.IEnvelopeCache; import io.sentry.clientreport.DiscardReason; import io.sentry.exception.SentryEnvelopeException; import io.sentry.hints.AbnormalExit; @@ -48,12 +49,16 @@ public boolean isEnabled() { } public SentryClient(final @NotNull SentryOptions options) { + this(options, options.getEnvelopeDiskCache()); + } + + SentryClient(final @NotNull SentryOptions options, final @NotNull IEnvelopeCache envelopeCache) { this.options = Objects.requireNonNull(options, "SentryOptions is required."); this.enabled = true; ITransportFactory transportFactory = options.getTransportFactory(); if (transportFactory instanceof NoOpTransportFactory) { - transportFactory = new AsyncHttpTransportFactory(); + transportFactory = new AsyncHttpTransportFactory(envelopeCache); options.setTransportFactory(transportFactory); } diff --git a/sentry/src/main/java/io/sentry/internal/SentryRuntime.java b/sentry/src/main/java/io/sentry/internal/SentryRuntime.java index 9a9dc673012..d1f18920213 100644 --- a/sentry/src/main/java/io/sentry/internal/SentryRuntime.java +++ b/sentry/src/main/java/io/sentry/internal/SentryRuntime.java @@ -101,6 +101,10 @@ private Builder() {} return this; } + public boolean hasEnvelopeDiskCache() { + return envelopeDiskCache != null; + } + public boolean hasExecutorService() { return executorService != null; } diff --git a/sentry/src/main/java/io/sentry/transport/AsyncHttpTransport.java b/sentry/src/main/java/io/sentry/transport/AsyncHttpTransport.java index adf9f749d5c..fdff7f04e3d 100644 --- a/sentry/src/main/java/io/sentry/transport/AsyncHttpTransport.java +++ b/sentry/src/main/java/io/sentry/transport/AsyncHttpTransport.java @@ -25,6 +25,7 @@ import java.util.concurrent.RejectedExecutionHandler; import java.util.concurrent.ThreadFactory; import java.util.concurrent.TimeUnit; +import org.jetbrains.annotations.ApiStatus; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -47,13 +48,24 @@ public AsyncHttpTransport( final @NotNull RateLimiter rateLimiter, final @NotNull ITransportGate transportGate, final @NotNull RequestDetails requestDetails) { + this(options, options.getEnvelopeDiskCache(), rateLimiter, transportGate, requestDetails); + } + + @ApiStatus.Internal + public AsyncHttpTransport( + final @NotNull SentryOptions options, + final @NotNull IEnvelopeCache envelopeCache, + final @NotNull RateLimiter rateLimiter, + final @NotNull ITransportGate transportGate, + final @NotNull RequestDetails requestDetails) { this( initExecutor( options.getMaxQueueSize(), - options.getEnvelopeDiskCache(), + envelopeCache, options.getLogger(), options.getDateProvider()), options, + envelopeCache, rateLimiter, transportGate, new HttpConnection(options, requestDetails, rateLimiter)); @@ -65,9 +77,25 @@ public AsyncHttpTransport( final @NotNull RateLimiter rateLimiter, final @NotNull ITransportGate transportGate, final @NotNull HttpConnection httpConnection) { + this( + executor, + options, + options.getEnvelopeDiskCache(), + rateLimiter, + transportGate, + httpConnection); + } + + @ApiStatus.Internal + public AsyncHttpTransport( + final @NotNull QueuedThreadPoolExecutor executor, + final @NotNull SentryOptions options, + final @NotNull IEnvelopeCache envelopeCache, + final @NotNull RateLimiter rateLimiter, + final @NotNull ITransportGate transportGate, + final @NotNull HttpConnection httpConnection) { this.executor = Objects.requireNonNull(executor, "executor is required"); - this.envelopeCache = - Objects.requireNonNull(options.getEnvelopeDiskCache(), "envelopeCache is required"); + this.envelopeCache = Objects.requireNonNull(envelopeCache, "envelopeCache is required"); this.options = Objects.requireNonNull(options, "options is required"); this.rateLimiter = Objects.requireNonNull(rateLimiter, "rateLimiter is required"); this.transportGate = Objects.requireNonNull(transportGate, "transportGate is required"); diff --git a/sentry/src/test/java/io/sentry/MovePreviousSessionTest.kt b/sentry/src/test/java/io/sentry/MovePreviousSessionTest.kt index f14cbd6d016..8f89b57fdab 100644 --- a/sentry/src/test/java/io/sentry/MovePreviousSessionTest.kt +++ b/sentry/src/test/java/io/sentry/MovePreviousSessionTest.kt @@ -25,6 +25,7 @@ class MovePreviousSessionTest { setLogger(SystemOutLogger()) } val cache = mock() + lateinit var usedCache: IEnvelopeCache fun getSUT( cacheDirPath: String? = tempDir.toAbsolutePath().toFile().absolutePath, @@ -33,8 +34,8 @@ class MovePreviousSessionTest { ): MovePreviousSession { options.cacheDirPath = cacheDirPath options.isEnableAutoSessionTracking = isEnableSessionTracking - options.setEnvelopeDiskCache(envelopeCache ?: EnvelopeCache.create(options)) - return MovePreviousSession(options) + usedCache = envelopeCache ?: EnvelopeCache.create(options) + return MovePreviousSession(options, usedCache) } fun cleanup() { @@ -76,7 +77,7 @@ class MovePreviousSessionTest { sut.run() - (fixture.options.envelopeDiskCache as EnvelopeCache).waitPreviousSessionFlush() + (fixture.usedCache as EnvelopeCache).waitPreviousSessionFlush() assertFalse(currentSessionFile.exists()) assertTrue(previousSessionFile.exists()) @@ -109,7 +110,7 @@ class MovePreviousSessionTest { sut.run() // Wait for flush to complete - (fixture.options.envelopeDiskCache as EnvelopeCache).waitPreviousSessionFlush() + (fixture.usedCache as EnvelopeCache).waitPreviousSessionFlush() // Current session file should have been moved to previous assertFalse(currentSessionFile.exists()) @@ -131,7 +132,7 @@ class MovePreviousSessionTest { sut.run() - (fixture.options.envelopeDiskCache as EnvelopeCache).waitPreviousSessionFlush() + (fixture.usedCache as EnvelopeCache).waitPreviousSessionFlush() assertFalse(currentSessionFile.exists()) assertFalse(previousSessionFile.exists()) @@ -156,7 +157,7 @@ class MovePreviousSessionTest { sut.run() - (fixture.options.envelopeDiskCache as EnvelopeCache).waitPreviousSessionFlush() + (fixture.usedCache as EnvelopeCache).waitPreviousSessionFlush() // Current session file should have been moved to previous assertFalse(currentSessionFile.exists()) diff --git a/sentry/src/test/java/io/sentry/PreviousSessionFinalizerTest.kt b/sentry/src/test/java/io/sentry/PreviousSessionFinalizerTest.kt index f9a3097ec7d..dbc98b46267 100644 --- a/sentry/src/test/java/io/sentry/PreviousSessionFinalizerTest.kt +++ b/sentry/src/test/java/io/sentry/PreviousSessionFinalizerTest.kt @@ -53,10 +53,10 @@ class PreviousSessionFinalizerTest { this.flushTimeoutMillis = flushTimeoutMillis this.sessionFlushTimeoutMillis = flushTimeoutMillis isEnableAutoSessionTracking = sessionTrackingEnabled - setEnvelopeDiskCache(EnvelopeCache.create(this)) - if (!shouldAwait) { - (envelopeDiskCache as? EnvelopeCache)?.flushPreviousSession() - } + } + val cache = EnvelopeCache.create(options) + if (!shouldAwait) { + (cache as? EnvelopeCache)?.flushPreviousSession() } options.cacheDirPath?.let { sessionFile = EnvelopeCache.getPreviousSessionFile(it) @@ -73,7 +73,7 @@ class PreviousSessionFinalizerTest { nativeCrashMarker.writeText(nativeCrashTimestamp.toString()) } } - return PreviousSessionFinalizer(options, scopes) + return PreviousSessionFinalizer(options, scopes, cache, options.serializer) } fun sessionFromEnvelope(envelope: SentryEnvelope): Session {