Skip to content

Commit

Permalink
Merge 1c2c297 into b071538
Browse files Browse the repository at this point in the history
  • Loading branch information
markushi authored Jun 25, 2024
2 parents b071538 + 1c2c297 commit e1c34e7
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

### Fixes

- Fix duplicate session start for React Native ([#3504](https://github.com/getsentry/sentry-java/pull/3504))
- Move onFinishCallback before span or transaction is finished ([#3459](https://github.com/getsentry/sentry-java/pull/3459))
- Add timestamp when a profile starts ([#3442](https://github.com/getsentry/sentry-java/pull/3442))
- Move fragment auto span finish to onFragmentStarted ([#3424](https://github.com/getsentry/sentry-java/pull/3424))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import io.sentry.Sentry;
import io.sentry.SentryLevel;
import io.sentry.SentryOptions;
import io.sentry.Session;
import io.sentry.android.core.internal.util.BreadcrumbFactory;
import io.sentry.android.core.performance.AppStartMetrics;
import io.sentry.android.core.performance.TimeSpan;
Expand All @@ -19,7 +20,9 @@
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

/** Sentry initialization class */
public final class SentryAndroid {
Expand Down Expand Up @@ -146,8 +149,21 @@ public static synchronized void init(

final @NotNull IHub hub = Sentry.getCurrentHub();
if (hub.getOptions().isEnableAutoSessionTracking() && ContextUtils.isForegroundImportance()) {
hub.addBreadcrumb(BreadcrumbFactory.forSession("session.start"));
hub.startSession();
// The LifecycleWatcher of AppLifecycleIntegration may already started a session
// so only start a session if it's not already started
// This e.g. happens on React Native, or e.g. on deferred SDK init
final AtomicBoolean sessionStarted = new AtomicBoolean(false);
hub.configureScope(
scope -> {
final @Nullable Session currentSession = scope.getSession();
if (currentSession != null && currentSession.getStarted() != null) {
sessionStarted.set(true);
}
});
if (!sessionStarted.get()) {
hub.addBreadcrumb(BreadcrumbFactory.forSession("session.start"));
hub.startSession();
}
}
} catch (IllegalAccessException e) {
logger.log(SentryLevel.FATAL, "Fatal error during SentryAndroid.init(...)", e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import androidx.test.ext.junit.runners.AndroidJUnit4
import io.sentry.Breadcrumb
import io.sentry.Hint
import io.sentry.ILogger
import io.sentry.ISentryClient
import io.sentry.Sentry
import io.sentry.SentryEnvelope
import io.sentry.SentryLevel
Expand Down Expand Up @@ -50,6 +51,7 @@ import org.mockito.kotlin.eq
import org.mockito.kotlin.mock
import org.mockito.kotlin.never
import org.mockito.kotlin.spy
import org.mockito.kotlin.times
import org.mockito.kotlin.verify
import org.mockito.kotlin.whenever
import org.robolectric.annotation.Config
Expand Down Expand Up @@ -314,8 +316,24 @@ class SentryAndroidTest {
}
}

@Test
fun `init does not start a session if one is already running`() {
val client = mock<ISentryClient>()

initSentryWithForegroundImportance(true, { options ->
options.addIntegration { hub, _ ->
hub.bindClient(client)
// usually done by LifecycleWatcher
hub.startSession()
}
}) {}

verify(client, times(1)).captureSession(any(), any())
}

private fun initSentryWithForegroundImportance(
inForeground: Boolean,
optionsConfig: (SentryAndroidOptions) -> Unit = {},
callback: (session: Session?) -> Unit
) {
val context = ContextUtilsTestHelper.createMockContext()
Expand All @@ -327,6 +345,7 @@ class SentryAndroidTest {
options.release = "prod"
options.dsn = "https://[email protected]/123"
options.isEnableAutoSessionTracking = true
optionsConfig(options)
}

var session: Session? = null
Expand Down

0 comments on commit e1c34e7

Please sign in to comment.