Skip to content

Commit

Permalink
Hubs/Scopes Merge 42d - Close previous scopes before binding a new gl…
Browse files Browse the repository at this point in the history
…obal client (#3404)

* replace hub with scopes

* Add Scopes

* Introduce `IScopes` interface.

* Replace `IHub` with `IScopes` in core

* Replace `IHub` with `IScopes` in android core

* Replace `IHub` with `IScopes` in android integrations

* Replace `IHub` with `IScopes` in apollo integrations

* Replace `IHub` with `IScopes` in okhttp integration

* Replace `IHub` with `IScopes` in graphql integration

* Replace `IHub` with `IScopes` in logging integrations

* Replace `IHub` with `IScopes` in more integrations

* Replace `IHub` with `IScopes` in OTel integration

* Replace `IHub` with `IScopes` in Spring 5 / Spring Boot 2 integrations

* Replace `IHub` with `IScopes` in Spring 6 / Spring Boot 3 integrations

* Replace `IHub` with `IScopes` in samples

* gitscopes -> github

* Replace ThreadLocal with ScopesStorage

* Move client and throwable to span map to scope

* Add global scope

* use global scope in Scopes

* Implement pushScope popScope and withScope for Scopes

* Add pushIsolationScope; add fork methods to ISCope

* Use separate scopes for current, isolation and global scope; rename mainScopes to rootScopes

* Allow controlling which scope configureScope uses

* Combine scopes

* Use new API for CRONS integrations

* Add lifecycle helper

* Change spring integrations to use new API

* Use new API in servlet integrations

* Use new API for kotlin coroutines and wrapers for Supplier/Callable

* Discussion TODOs

* Fix breadcrumb ordering

* Mark TODOS with [HSM]

* Add getGlobalScope and forkedRootScopes to IScopes

* Fix EventProcessor ordering on scopes

* Reuse code in Scopes

* No longer replace global scope

* Replace hub occurrences in comments, var names etc.

* Implement ScopesTest

* Implement CombinedScopeViewTest

* Fix combined contexts

* Use combined scopes for cross platform

* Changes according to reviews of previous PRs

* more

* even more

* isEnabled checks client instead of having a property on Scopes

* Use SentryOptions.empty

* Remove Hub

* Close previous scopes before binding a new global client
  • Loading branch information
adinauer committed May 7, 2024
1 parent 5e2029b commit e7007dd
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 2 deletions.
4 changes: 2 additions & 2 deletions sentry/src/main/java/io/sentry/Sentry.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ private Sentry() {}
*/
// TODO [HSM] use SentryOptions.empty and address
// https://github.com/getsentry/sentry-java/issues/2541
private static volatile @NotNull IScope globalScope = new Scope(SentryOptions.empty());
private static final @NotNull IScope globalScope = new Scope(SentryOptions.empty());

/** Default value for globalHubMode is false */
private static final boolean GLOBAL_HUB_DEFAULT_MODE = false;
Expand Down Expand Up @@ -277,12 +277,12 @@ private static synchronized void init(
final IScopes scopes = getCurrentScopes();
final IScope rootScope = new Scope(options);
final IScope rootIsolationScope = new Scope(options);
globalScope.bindClient(new SentryClient(options));
rootScopes = new Scopes(rootScope, rootIsolationScope, globalScope, "Sentry.init");

getScopesStorage().set(rootScopes);

scopes.close(true);
globalScope.bindClient(new SentryClient(options));

// If the executorService passed in the init is the same that was previously closed, we have to
// set a new one
Expand Down
32 changes: 32 additions & 0 deletions sentry/src/test/java/io/sentry/SentryTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,38 @@ class SentryTest {
verify(scopes).close(eq(true))
}

@Test
fun `global client is enabled after restart`() {
val scopes = mock<IScopes>()
whenever(scopes.close()).then { Sentry.getGlobalScope().client.close() }
whenever(scopes.close(anyOrNull())).then { Sentry.getGlobalScope().client.close() }

Sentry.init {
it.dsn = dsn
}
Sentry.setCurrentScopes(scopes)
Sentry.init {
it.dsn = dsn
}
verify(scopes).close(eq(true))
assertTrue(Sentry.getGlobalScope().client.isEnabled)
}

@Test
fun `global client is disabled after close`() {
val scopes = mock<IScopes>()
whenever(scopes.close()).then { Sentry.getGlobalScope().client.close() }
whenever(scopes.close(anyOrNull())).then { Sentry.getGlobalScope().client.close() }

Sentry.init {
it.dsn = dsn
}
Sentry.setCurrentScopes(scopes)
Sentry.close()
verify(scopes).close(eq(false))
assertFalse(Sentry.getGlobalScope().client.isEnabled)
}

@Test
fun `close calls scopes close with isRestarting false`() {
val scopes = mock<IScopes>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,16 @@ import org.mockito.kotlin.check
import org.mockito.kotlin.mock
import org.mockito.kotlin.verify
import org.mockito.kotlin.whenever
import kotlin.test.BeforeTest
import kotlin.test.Test

class MetricsIntegrationTest {

@BeforeTest
fun setup() {
Sentry.close()
}

@Test
fun `metrics are collected`() {
val options = SentryOptions().apply {
Expand Down

0 comments on commit e7007dd

Please sign in to comment.