From 3f5b40fc5b959b32a7543c649bea089db96d3f69 Mon Sep 17 00:00:00 2001 From: Nelson Osacky Date: Thu, 3 Dec 2026 15:45:08 +0100 Subject: [PATCH] ref(core): Inject the envelope disk cache from InternalServices The SentryClient, its transport, and the previous-session tasks now get the envelope disk cache through their constructors instead of reading it off SentryOptions. InternalServices creates the default cache, and Android puts its AndroidEnvelopeCache on the InternalServices 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 | 8 +++- .../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/InternalServices.java | 4 ++ .../sentry/transport/AsyncHttpTransport.java | 34 +++++++++++++++-- .../java/io/sentry/MovePreviousSessionTest.kt | 13 ++++--- .../io/sentry/PreviousSessionFinalizerTest.kt | 10 ++--- 17 files changed, 126 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..b262cbff538 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.InternalServices; 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, + InternalServices.builder(), context, new BuildInfoProvider(new AndroidLogger()), loadClass, @@ -164,15 +165,15 @@ static void initializeIntegrationsAndProcessors( static void initializeIntegrationsAndProcessors( final @NotNull SentryAndroidOptions options, + final @NotNull InternalServices.Builder services, 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 && !services.hasEnvelopeDiskCache()) { + services.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 2480888b535..b18938326d0 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.InternalServices; import io.sentry.protocol.App; import io.sentry.protocol.Device; @@ -320,6 +321,9 @@ 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 InternalServices services = InternalServices.getCurrent(); + final @Nullable IEnvelopeCache cache = + services != null ? services.getEnvelopeDiskCache() : null; scopes.configureScope(scope -> scope.withSession(session -> { if (session == null) { @@ -334,8 +338,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 fe9fc7fea83..5d2ff446cc8 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, + services, 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..7fb62c78874 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.InternalServices 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, + InternalServices.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..f30a8cf9646 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.InternalServices 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 services = InternalServices.builder() lateinit var mockContext: Context val logger = mock() @@ -117,7 +119,9 @@ class AndroidOptionsInitializerTest { sentryOptions.configureOptions() AndroidOptionsInitializer.initializeIntegrationsAndProcessors( sentryOptions, + services, if (useRealContext) context else mockContext, + BuildInfoProvider(AndroidLogger()), loadClass, activityFramesTracker, false, @@ -166,6 +170,7 @@ class AndroidOptionsInitializerTest { AndroidOptionsInitializer.initializeIntegrationsAndProcessors( sentryOptions, + services, context, buildInfo, loadClass, @@ -798,10 +803,10 @@ class AndroidOptionsInitializerTest { } @Test - fun `AndroidEnvelopeCache is set to options`() { + fun `AndroidEnvelopeCache is set on the internal services`() { fixture.initSut() - assertTrue { fixture.sentryOptions.envelopeDiskCache is AndroidEnvelopeCache } + assertTrue { fixture.services.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..305cc2ae329 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.InternalServices 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, + InternalServices.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..0a2c0741c3d 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.InternalServices 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, + InternalServices.builder(), context, buildInfoProvider, loadClass, diff --git a/sentry/api/sentry.api b/sentry/api/sentry.api index dc5c3c2a1c9..c13b997d1f2 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; } @@ -5465,6 +5466,7 @@ public final class io/sentry/internal/InternalServices$Builder { public fun buildForTest ()Lio/sentry/internal/InternalServices; public fun envelopeDiskCache (Lio/sentry/cache/IEnvelopeCache;)Lio/sentry/internal/InternalServices$Builder; public fun executorService (Lio/sentry/ISentryExecutorService;)Lio/sentry/internal/InternalServices$Builder; + public fun hasEnvelopeDiskCache ()Z public fun hasExecutorService ()Z public fun lazySerializer (Lio/sentry/util/LazyEvaluator$Evaluator;)Lio/sentry/internal/InternalServices$Builder; public fun serializer (Lio/sentry/ISerializer;)Lio/sentry/internal/InternalServices$Builder; @@ -7801,7 +7803,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 c43f339b122..df3fa237ded 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 InternalServices services = LegacyOptionsBridge.buildServices(servicesBuilder, options); InternalServices.setCurrent(services); + globalScope.bindClient(new SentryClient(options, services.getEnvelopeDiskCache())); + if (options.getTimerExecutorService().isClosed()) { options.setTimerExecutorService( new SentryExecutorService( @@ -406,7 +403,7 @@ private static void init( e); } - movePreviousSession(options); + movePreviousSession(options, services); // 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, services, 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 InternalServices services) { // 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, services.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 InternalServices services, + 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, services.getEnvelopeDiskCache(), services.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/InternalServices.java b/sentry/src/main/java/io/sentry/internal/InternalServices.java index 8d1d7480b48..7c3b4392a84 100644 --- a/sentry/src/main/java/io/sentry/internal/InternalServices.java +++ b/sentry/src/main/java/io/sentry/internal/InternalServices.java @@ -107,6 +107,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 {