Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -155,6 +155,7 @@ static void initializeIntegrationsAndProcessors(
final boolean isReplayAvailable) {
initializeIntegrationsAndProcessors(
options,
SentryRuntime.builder(),
context,
new BuildInfoProvider(new AndroidLogger()),
loadClass,
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand All @@ -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);
}
}));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ static void init(

AndroidOptionsInitializer.initializeIntegrationsAndProcessors(
options,
runtime,
context,
buildInfoProvider,
loadClass,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -126,6 +127,7 @@ class AndroidContinuousProfilerTest {

AndroidOptionsInitializer.initializeIntegrationsAndProcessors(
fixture.options,
SentryRuntime.builder(),
context,
buildInfoProvider,
loadClass,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -67,6 +68,7 @@ private const val SAAS_DSN = "https://[email protected]/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<ILogger>()

Expand Down Expand Up @@ -117,7 +119,9 @@ class AndroidOptionsInitializerTest {
sentryOptions.configureOptions()
AndroidOptionsInitializer.initializeIntegrationsAndProcessors(
sentryOptions,
runtime,
if (useRealContext) context else mockContext,
BuildInfoProvider(AndroidLogger()),
loadClass,
activityFramesTracker,
false,
Expand Down Expand Up @@ -166,6 +170,7 @@ class AndroidOptionsInitializerTest {

AndroidOptionsInitializer.initializeIntegrationsAndProcessors(
sentryOptions,
runtime,
context,
buildInfo,
loadClass,
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -131,6 +132,7 @@ class AndroidProfilerTest {

AndroidOptionsInitializer.initializeIntegrationsAndProcessors(
fixture.options,
SentryRuntime.builder(),
context,
buildInfoProvider,
loadClass,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -146,6 +147,7 @@ class AndroidTransactionProfilerTest {

AndroidOptionsInitializer.initializeIntegrationsAndProcessors(
fixture.options,
SentryRuntime.builder(),
context,
buildInfoProvider,
loadClass,
Expand Down
4 changes: 4 additions & 0 deletions sentry/api/sentry.api
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
public final class io/sentry/AsyncHttpTransportFactory : io/sentry/ITransportFactory {
public fun <init> ()V
public fun <init> (Lio/sentry/cache/IEnvelopeCache;)V
public fun create (Lio/sentry/SentryOptions;Lio/sentry/RequestDetails;)Lio/sentry/transport/ITransport;
}

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -7815,7 +7817,9 @@ public final class io/sentry/time/Timestamp {
}

public final class io/sentry/transport/AsyncHttpTransport : io/sentry/transport/ITransport {
public fun <init> (Lio/sentry/SentryOptions;Lio/sentry/cache/IEnvelopeCache;Lio/sentry/transport/RateLimiter;Lio/sentry/transport/ITransportGate;Lio/sentry/RequestDetails;)V
public fun <init> (Lio/sentry/SentryOptions;Lio/sentry/transport/RateLimiter;Lio/sentry/transport/ITransportGate;Lio/sentry/RequestDetails;)V
public fun <init> (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 <init> (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
Expand Down
13 changes: 13 additions & 0 deletions sentry/src/main/java/io/sentry/AsyncHttpTransportFactory.java
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -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);
Expand Down
5 changes: 3 additions & 2 deletions sentry/src/main/java/io/sentry/MovePreviousSession.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
Expand Down
14 changes: 11 additions & 3 deletions sentry/src/main/java/io/sentry/PreviousSessionFinalizer.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -48,7 +58,6 @@ public void run() {
return;
}

final IEnvelopeCache cache = options.getEnvelopeDiskCache();
if (cache instanceof EnvelopeCache) {
if (!((EnvelopeCache) cache).waitPreviousSessionFlush()) {
options
Expand All @@ -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.");
Expand Down
37 changes: 17 additions & 20 deletions sentry/src/main/java/io/sentry/Sentry.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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(
Expand All @@ -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.
Expand All @@ -431,7 +428,7 @@ private static void init(

notifyOptionsObservers(options);

finalizePreviousSession(options, ScopesAdapter.getInstance());
finalizePreviousSession(options, runtime, ScopesAdapter.getInstance());

handleAppStartProfilingConfig(options, options.getExecutorService());

Expand Down Expand Up @@ -574,23 +571,32 @@ 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);
}
}

@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);
}
Expand Down Expand Up @@ -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) {
Expand Down
Loading
Loading