Skip to content

Commit 76ec6b0

Browse files
authored
Hubs/Scopes Merge 9 - Replace IHub with IScopes in more integrations (#3305)
* 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
1 parent c0be8ea commit 76ec6b0

File tree

20 files changed

+129
-112
lines changed

20 files changed

+129
-112
lines changed

sentry-jdbc/api/sentry-jdbc.api

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public final class io/sentry/jdbc/DatabaseUtils$DatabaseDetails {
1616

1717
public class io/sentry/jdbc/SentryJdbcEventListener : com/p6spy/engine/event/SimpleJdbcEventListener {
1818
public fun <init> ()V
19-
public fun <init> (Lio/sentry/IHub;)V
19+
public fun <init> (Lio/sentry/IScopes;)V
2020
public fun onAfterAnyExecute (Lcom/p6spy/engine/common/StatementInformation;JLjava/sql/SQLException;)V
2121
public fun onBeforeAnyExecute (Lcom/p6spy/engine/common/StatementInformation;)V
2222
}

sentry-jdbc/src/main/java/io/sentry/jdbc/SentryJdbcEventListener.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
import com.jakewharton.nopen.annotation.Open;
77
import com.p6spy.engine.common.StatementInformation;
88
import com.p6spy.engine.event.SimpleJdbcEventListener;
9-
import io.sentry.HubAdapter;
10-
import io.sentry.IHub;
9+
import io.sentry.IScopes;
1110
import io.sentry.ISpan;
11+
import io.sentry.ScopesAdapter;
1212
import io.sentry.SentryIntegrationPackageStorage;
1313
import io.sentry.Span;
1414
import io.sentry.SpanStatus;
@@ -21,24 +21,24 @@
2121
@Open
2222
public class SentryJdbcEventListener extends SimpleJdbcEventListener {
2323
private static final String TRACE_ORIGIN = "auto.db.jdbc";
24-
private final @NotNull IHub hub;
24+
private final @NotNull IScopes scopes;
2525
private static final @NotNull ThreadLocal<ISpan> CURRENT_SPAN = new ThreadLocal<>();
2626

2727
private volatile @Nullable DatabaseUtils.DatabaseDetails cachedDatabaseDetails = null;
2828
private final @NotNull Object databaseDetailsLock = new Object();
2929

30-
public SentryJdbcEventListener(final @NotNull IHub hub) {
31-
this.hub = Objects.requireNonNull(hub, "hub is required");
30+
public SentryJdbcEventListener(final @NotNull IScopes scopes) {
31+
this.scopes = Objects.requireNonNull(scopes, "scopes are required");
3232
addPackageAndIntegrationInfo();
3333
}
3434

3535
public SentryJdbcEventListener() {
36-
this(HubAdapter.getInstance());
36+
this(ScopesAdapter.getInstance());
3737
}
3838

3939
@Override
4040
public void onBeforeAnyExecute(final @NotNull StatementInformation statementInformation) {
41-
final ISpan parent = hub.getSpan();
41+
final ISpan parent = scopes.getSpan();
4242
if (parent != null && !parent.isNoOp()) {
4343
final ISpan span = parent.startChild("db.query", statementInformation.getSql());
4444
CURRENT_SPAN.set(span);

sentry-jdbc/src/test/kotlin/io/sentry/jdbc/SentryJdbcEventListenerTest.kt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package io.sentry.jdbc
22

33
import com.p6spy.engine.common.StatementInformation
44
import com.p6spy.engine.spy.P6DataSource
5-
import io.sentry.IHub
5+
import io.sentry.IScopes
66
import io.sentry.SentryOptions
77
import io.sentry.SentryTracer
88
import io.sentry.SpanDataConvention.DB_NAME_KEY
@@ -26,7 +26,7 @@ import kotlin.test.assertTrue
2626
class SentryJdbcEventListenerTest {
2727

2828
class Fixture {
29-
val hub = mock<IHub>().apply {
29+
val scopes = mock<IScopes>().apply {
3030
whenever(options).thenReturn(
3131
SentryOptions().apply {
3232
sdkVersion = SdkVersion("test", "1.2.3")
@@ -37,9 +37,9 @@ class SentryJdbcEventListenerTest {
3737
val actualDataSource = JDBCDataSource()
3838

3939
fun getSut(withRunningTransaction: Boolean = true, existingRow: Int? = null): DataSource {
40-
tx = SentryTracer(TransactionContext("name", "op"), hub)
40+
tx = SentryTracer(TransactionContext("name", "op"), scopes)
4141
if (withRunningTransaction) {
42-
whenever(hub.span).thenReturn(tx)
42+
whenever(scopes.span).thenReturn(tx)
4343
}
4444
actualDataSource.setURL("jdbc:hsqldb:mem:testdb")
4545

@@ -54,7 +54,7 @@ class SentryJdbcEventListenerTest {
5454
}
5555
}
5656

57-
val sentryQueryExecutionListener = SentryJdbcEventListener(hub)
57+
val sentryQueryExecutionListener = SentryJdbcEventListener(scopes)
5858
val p6spyDataSource = P6DataSource(actualDataSource)
5959
p6spyDataSource.setJdbcEventListenerFactory { sentryQueryExecutionListener }
6060
return p6spyDataSource
@@ -131,9 +131,9 @@ class SentryJdbcEventListenerTest {
131131
@Test
132132
fun `sets SDKVersion Info`() {
133133
val sut = fixture.getSut()
134-
assertNotNull(fixture.hub.options.sdkVersion)
135-
assert(fixture.hub.options.sdkVersion!!.integrationSet.contains("JDBC"))
136-
val packageInfo = fixture.hub.options.sdkVersion!!.packageSet.firstOrNull { pkg -> pkg.name == "maven:io.sentry:sentry-jdbc" }
134+
assertNotNull(fixture.scopes.options.sdkVersion)
135+
assert(fixture.scopes.options.sdkVersion!!.integrationSet.contains("JDBC"))
136+
val packageInfo = fixture.scopes.options.sdkVersion!!.packageSet.firstOrNull { pkg -> pkg.name == "maven:io.sentry:sentry-jdbc" }
137137
assertNotNull(packageInfo)
138138
assert(packageInfo.version == BuildConfig.VERSION_NAME)
139139
}
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
public final class io/sentry/kotlin/SentryContext : kotlin/coroutines/AbstractCoroutineContextElement, kotlinx/coroutines/CopyableThreadContextElement {
22
public fun <init> ()V
3-
public fun <init> (Lio/sentry/IHub;)V
4-
public synthetic fun <init> (Lio/sentry/IHub;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
3+
public fun <init> (Lio/sentry/IScopes;)V
4+
public synthetic fun <init> (Lio/sentry/IScopes;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
55
public fun copyForChild ()Lkotlinx/coroutines/CopyableThreadContextElement;
66
public fun fold (Ljava/lang/Object;Lkotlin/jvm/functions/Function2;)Ljava/lang/Object;
77
public fun get (Lkotlin/coroutines/CoroutineContext$Key;)Lkotlin/coroutines/CoroutineContext$Element;
88
public fun mergeForChild (Lkotlin/coroutines/CoroutineContext$Element;)Lkotlin/coroutines/CoroutineContext;
99
public fun minusKey (Lkotlin/coroutines/CoroutineContext$Key;)Lkotlin/coroutines/CoroutineContext;
1010
public fun plus (Lkotlin/coroutines/CoroutineContext;)Lkotlin/coroutines/CoroutineContext;
11-
public fun restoreThreadContext (Lkotlin/coroutines/CoroutineContext;Lio/sentry/IHub;)V
11+
public fun restoreThreadContext (Lkotlin/coroutines/CoroutineContext;Lio/sentry/IScopes;)V
1212
public synthetic fun restoreThreadContext (Lkotlin/coroutines/CoroutineContext;Ljava/lang/Object;)V
13-
public fun updateThreadContext (Lkotlin/coroutines/CoroutineContext;)Lio/sentry/IHub;
13+
public fun updateThreadContext (Lkotlin/coroutines/CoroutineContext;)Lio/sentry/IScopes;
1414
public synthetic fun updateThreadContext (Lkotlin/coroutines/CoroutineContext;)Ljava/lang/Object;
1515
}
1616

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package io.sentry.kotlin
22

3-
import io.sentry.IHub
3+
import io.sentry.IScopes
44
import io.sentry.Sentry
55
import kotlinx.coroutines.CopyableThreadContextElement
66
import kotlin.coroutines.AbstractCoroutineContextElement
@@ -9,26 +9,32 @@ import kotlin.coroutines.CoroutineContext
99
/**
1010
* Sentry context element for [CoroutineContext].
1111
*/
12-
public class SentryContext(private val hub: IHub = Sentry.getCurrentHub().clone()) :
13-
CopyableThreadContextElement<IHub>, AbstractCoroutineContextElement(Key) {
12+
@SuppressWarnings("deprecation")
13+
// TODO fork instead
14+
public class SentryContext(private val scopes: IScopes = Sentry.getCurrentScopes().clone()) :
15+
CopyableThreadContextElement<IScopes>, AbstractCoroutineContextElement(Key) {
1416

1517
private companion object Key : CoroutineContext.Key<SentryContext>
1618

17-
override fun copyForChild(): CopyableThreadContextElement<IHub> {
18-
return SentryContext(hub.clone())
19+
@SuppressWarnings("deprecation")
20+
override fun copyForChild(): CopyableThreadContextElement<IScopes> {
21+
// TODO fork instead
22+
return SentryContext(scopes.clone())
1923
}
2024

25+
@SuppressWarnings("deprecation")
2126
override fun mergeForChild(overwritingElement: CoroutineContext.Element): CoroutineContext {
22-
return overwritingElement[Key] ?: SentryContext(hub.clone())
27+
// TODO fork instead?
28+
return overwritingElement[Key] ?: SentryContext(scopes.clone())
2329
}
2430

25-
override fun updateThreadContext(context: CoroutineContext): IHub {
26-
val oldState = Sentry.getCurrentHub()
27-
Sentry.setCurrentHub(hub)
31+
override fun updateThreadContext(context: CoroutineContext): IScopes {
32+
val oldState = Sentry.getCurrentScopes()
33+
Sentry.setCurrentScopes(scopes)
2834
return oldState
2935
}
3036

31-
override fun restoreThreadContext(context: CoroutineContext, oldState: IHub) {
32-
Sentry.setCurrentHub(oldState)
37+
override fun restoreThreadContext(context: CoroutineContext, oldState: IScopes) {
38+
Sentry.setCurrentScopes(oldState)
3339
}
3440
}

sentry-kotlin-extensions/src/test/java/io/sentry/kotlin/SentryContextTest.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ class SentryContextTest {
119119

120120
val c2 = launch(
121121
SentryContext(
122-
Sentry.getCurrentHub().clone().also {
122+
Sentry.getCurrentScopes().clone().also {
123123
it.setTag("cloned", "clonedValue")
124124
}
125125
)
@@ -145,7 +145,7 @@ class SentryContextTest {
145145
@Test
146146
fun `mergeForChild returns copy of initial context if Key not present`() {
147147
val initialContextElement = SentryContext(
148-
Sentry.getCurrentHub().clone().also {
148+
Sentry.getCurrentScopes().clone().also {
149149
it.setTag("cloned", "clonedValue")
150150
}
151151
)
@@ -158,7 +158,7 @@ class SentryContextTest {
158158
@Test
159159
fun `mergeForChild returns passed context`() {
160160
val initialContextElement = SentryContext(
161-
Sentry.getCurrentHub().clone().also {
161+
Sentry.getCurrentScopes().clone().also {
162162
it.setTag("cloned", "clonedValue")
163163
}
164164
)

sentry-openfeign/api/sentry-openfeign.api

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
public final class io/sentry/openfeign/SentryCapability : feign/Capability {
22
public fun <init> ()V
3-
public fun <init> (Lio/sentry/IHub;Lio/sentry/openfeign/SentryFeignClient$BeforeSpanCallback;)V
3+
public fun <init> (Lio/sentry/IScopes;Lio/sentry/openfeign/SentryFeignClient$BeforeSpanCallback;)V
44
public fun <init> (Lio/sentry/openfeign/SentryFeignClient$BeforeSpanCallback;)V
55
public fun enrich (Lfeign/Client;)Lfeign/Client;
66
}
77

88
public final class io/sentry/openfeign/SentryFeignClient : feign/Client {
9-
public fun <init> (Lfeign/Client;Lio/sentry/IHub;Lio/sentry/openfeign/SentryFeignClient$BeforeSpanCallback;)V
9+
public fun <init> (Lfeign/Client;Lio/sentry/IScopes;Lio/sentry/openfeign/SentryFeignClient$BeforeSpanCallback;)V
1010
public fun execute (Lfeign/Request;Lfeign/Request$Options;)Lfeign/Response;
1111
}
1212

sentry-openfeign/src/main/java/io/sentry/openfeign/SentryCapability.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,34 @@
22

33
import feign.Capability;
44
import feign.Client;
5-
import io.sentry.HubAdapter;
6-
import io.sentry.IHub;
5+
import io.sentry.IScopes;
6+
import io.sentry.ScopesAdapter;
77
import org.jetbrains.annotations.NotNull;
88
import org.jetbrains.annotations.Nullable;
99

1010
/** Adds Sentry tracing capability to Feign clients. */
1111
public final class SentryCapability implements Capability {
1212

13-
private final @NotNull IHub hub;
13+
private final @NotNull IScopes scopes;
1414
private final @Nullable SentryFeignClient.BeforeSpanCallback beforeSpan;
1515

1616
public SentryCapability(
17-
final @NotNull IHub hub, final @Nullable SentryFeignClient.BeforeSpanCallback beforeSpan) {
18-
this.hub = hub;
17+
final @NotNull IScopes scopes,
18+
final @Nullable SentryFeignClient.BeforeSpanCallback beforeSpan) {
19+
this.scopes = scopes;
1920
this.beforeSpan = beforeSpan;
2021
}
2122

2223
public SentryCapability(final @Nullable SentryFeignClient.BeforeSpanCallback beforeSpan) {
23-
this(HubAdapter.getInstance(), beforeSpan);
24+
this(ScopesAdapter.getInstance(), beforeSpan);
2425
}
2526

2627
public SentryCapability() {
27-
this(HubAdapter.getInstance(), null);
28+
this(ScopesAdapter.getInstance(), null);
2829
}
2930

3031
@Override
3132
public @NotNull Client enrich(final @NotNull Client client) {
32-
return new SentryFeignClient(client, hub, beforeSpan);
33+
return new SentryFeignClient(client, scopes, beforeSpan);
3334
}
3435
}

sentry-openfeign/src/main/java/io/sentry/openfeign/SentryFeignClient.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import io.sentry.BaggageHeader;
1010
import io.sentry.Breadcrumb;
1111
import io.sentry.Hint;
12-
import io.sentry.IHub;
12+
import io.sentry.IScopes;
1313
import io.sentry.ISpan;
1414
import io.sentry.SpanDataConvention;
1515
import io.sentry.SpanStatus;
@@ -30,15 +30,15 @@
3030
public final class SentryFeignClient implements Client {
3131
private static final String TRACE_ORIGIN = "auto.http.openfeign";
3232
private final @NotNull Client delegate;
33-
private final @NotNull IHub hub;
33+
private final @NotNull IScopes scopes;
3434
private final @Nullable BeforeSpanCallback beforeSpan;
3535

3636
public SentryFeignClient(
3737
final @NotNull Client delegate,
38-
final @NotNull IHub hub,
38+
final @NotNull IScopes scopes,
3939
final @Nullable BeforeSpanCallback beforeSpan) {
4040
this.delegate = Objects.requireNonNull(delegate, "delegate is required");
41-
this.hub = Objects.requireNonNull(hub, "hub is required");
41+
this.scopes = Objects.requireNonNull(scopes, "scopes are required");
4242
this.beforeSpan = beforeSpan;
4343
}
4444

@@ -47,7 +47,7 @@ public Response execute(final @NotNull Request request, final @NotNull Request.O
4747
throws IOException {
4848
Response response = null;
4949
try {
50-
final ISpan activeSpan = hub.getSpan();
50+
final ISpan activeSpan = scopes.getSpan();
5151

5252
if (activeSpan == null) {
5353
final @NotNull Request modifiedRequest = maybeAddTracingHeaders(request, null);
@@ -102,7 +102,7 @@ public Response execute(final @NotNull Request request, final @NotNull Request.O
102102

103103
final @Nullable TracingUtils.TracingHeaders tracingHeaders =
104104
TracingUtils.traceIfAllowed(
105-
hub,
105+
scopes,
106106
request.url(),
107107
(requestBaggageHeaders != null ? new ArrayList<>(requestBaggageHeaders) : null),
108108
span);
@@ -139,7 +139,7 @@ private void addBreadcrumb(final @NotNull Request request, final @Nullable Respo
139139
hint.set(OPEN_FEIGN_RESPONSE, response);
140140
}
141141

142-
hub.addBreadcrumb(breadcrumb, hint);
142+
scopes.addBreadcrumb(breadcrumb, hint);
143143
}
144144

145145
static final class RequestWrapper {

sentry-openfeign/src/test/kotlin/io/sentry/openfeign/SentryFeignClientTest.kt

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import feign.HeaderMap
77
import feign.RequestLine
88
import io.sentry.BaggageHeader
99
import io.sentry.Breadcrumb
10-
import io.sentry.IHub
10+
import io.sentry.IScopes
1111
import io.sentry.Scope
1212
import io.sentry.ScopeCallback
1313
import io.sentry.SentryOptions
@@ -37,7 +37,7 @@ import kotlin.test.fail
3737
class SentryFeignClientTest {
3838

3939
class Fixture {
40-
val hub = mock<IHub>()
40+
val scopes = mock<IScopes>()
4141
val server = MockWebServer()
4242
val sentryTracer: SentryTracer
4343
val sentryOptions = SentryOptions().apply {
@@ -46,9 +46,9 @@ class SentryFeignClientTest {
4646
val scope = Scope(sentryOptions)
4747

4848
init {
49-
whenever(hub.options).thenReturn(sentryOptions)
50-
doAnswer { (it.arguments[0] as ScopeCallback).run(scope) }.whenever(hub).configureScope(any())
51-
sentryTracer = SentryTracer(TransactionContext("name", "op"), hub)
49+
whenever(scopes.options).thenReturn(sentryOptions)
50+
doAnswer { (it.arguments[0] as ScopeCallback).run(scope) }.whenever(scopes).configureScope(any())
51+
sentryTracer = SentryTracer(TransactionContext("name", "op"), scopes)
5252
}
5353

5454
fun getSut(
@@ -59,7 +59,7 @@ class SentryFeignClientTest {
5959
beforeSpan: SentryFeignClient.BeforeSpanCallback? = null
6060
): MockApi {
6161
if (isSpanActive) {
62-
whenever(hub.span).thenReturn(sentryTracer)
62+
whenever(scopes.span).thenReturn(sentryTracer)
6363
}
6464
server.enqueue(
6565
MockResponse()
@@ -70,12 +70,12 @@ class SentryFeignClientTest {
7070

7171
return if (!networkError) {
7272
Feign.builder()
73-
.addCapability(SentryCapability(hub, beforeSpan))
73+
.addCapability(SentryCapability(scopes, beforeSpan))
7474
} else {
7575
val mockClient = mock<Client>()
7676
whenever(mockClient.execute(any(), any())).thenThrow(RuntimeException::class.java)
7777
Feign.builder()
78-
.client(SentryFeignClient(mockClient, hub, beforeSpan))
78+
.client(SentryFeignClient(mockClient, scopes, beforeSpan))
7979
}.target(MockApi::class.java, server.url("/").toUrl().toString())
8080
}
8181
}
@@ -201,7 +201,7 @@ class SentryFeignClientTest {
201201
fun `adds breadcrumb when http calls succeeds`() {
202202
val sut = fixture.getSut(responseBody = "response body")
203203
sut.postWithBody("request-body")
204-
verify(fixture.hub).addBreadcrumb(
204+
verify(fixture.scopes).addBreadcrumb(
205205
check<Breadcrumb> {
206206
assertEquals("http", it.type)
207207
assertEquals(13, it.data["response_body_size"])
@@ -215,7 +215,7 @@ class SentryFeignClientTest {
215215
fun `adds breadcrumb when http calls succeeds even though response body is null`() {
216216
val sut = fixture.getSut(responseBody = "")
217217
sut.postWithBody("request-body")
218-
verify(fixture.hub).addBreadcrumb(
218+
verify(fixture.scopes).addBreadcrumb(
219219
check<Breadcrumb> {
220220
assertEquals("http", it.type)
221221
assertEquals(0, it.data["response_body_size"])
@@ -236,7 +236,7 @@ class SentryFeignClientTest {
236236
} catch (e: Exception) {
237237
// ignore me
238238
}
239-
verify(fixture.hub).addBreadcrumb(
239+
verify(fixture.scopes).addBreadcrumb(
240240
check<Breadcrumb> {
241241
assertEquals("http", it.type)
242242
},

0 commit comments

Comments
 (0)