diff --git a/sentry-okhttp/api/sentry-okhttp.api b/sentry-okhttp/api/sentry-okhttp.api index 3095659c88..9cb875ff34 100644 --- a/sentry-okhttp/api/sentry-okhttp.api +++ b/sentry-okhttp/api/sentry-okhttp.api @@ -6,12 +6,12 @@ public final class io/sentry/okhttp/BuildConfig { public class io/sentry/okhttp/SentryOkHttpEventListener : okhttp3/EventListener { public static final field Companion Lio/sentry/okhttp/SentryOkHttpEventListener$Companion; public fun ()V - public fun (Lio/sentry/IHub;Lkotlin/jvm/functions/Function1;)V - public synthetic fun (Lio/sentry/IHub;Lkotlin/jvm/functions/Function1;ILkotlin/jvm/internal/DefaultConstructorMarker;)V - public fun (Lio/sentry/IHub;Lokhttp3/EventListener$Factory;)V - public synthetic fun (Lio/sentry/IHub;Lokhttp3/EventListener$Factory;ILkotlin/jvm/internal/DefaultConstructorMarker;)V - public fun (Lio/sentry/IHub;Lokhttp3/EventListener;)V - public synthetic fun (Lio/sentry/IHub;Lokhttp3/EventListener;ILkotlin/jvm/internal/DefaultConstructorMarker;)V + public fun (Lio/sentry/IScopes;Lkotlin/jvm/functions/Function1;)V + public synthetic fun (Lio/sentry/IScopes;Lkotlin/jvm/functions/Function1;ILkotlin/jvm/internal/DefaultConstructorMarker;)V + public fun (Lio/sentry/IScopes;Lokhttp3/EventListener$Factory;)V + public synthetic fun (Lio/sentry/IScopes;Lokhttp3/EventListener$Factory;ILkotlin/jvm/internal/DefaultConstructorMarker;)V + public fun (Lio/sentry/IScopes;Lokhttp3/EventListener;)V + public synthetic fun (Lio/sentry/IScopes;Lokhttp3/EventListener;ILkotlin/jvm/internal/DefaultConstructorMarker;)V public fun (Lokhttp3/EventListener$Factory;)V public fun (Lokhttp3/EventListener;)V public fun cacheConditionalHit (Lokhttp3/Call;Lokhttp3/Response;)V @@ -50,9 +50,9 @@ public final class io/sentry/okhttp/SentryOkHttpEventListener$Companion { public class io/sentry/okhttp/SentryOkHttpInterceptor : okhttp3/Interceptor { public fun ()V - public fun (Lio/sentry/IHub;)V - public fun (Lio/sentry/IHub;Lio/sentry/okhttp/SentryOkHttpInterceptor$BeforeSpanCallback;ZLjava/util/List;Ljava/util/List;)V - public synthetic fun (Lio/sentry/IHub;Lio/sentry/okhttp/SentryOkHttpInterceptor$BeforeSpanCallback;ZLjava/util/List;Ljava/util/List;ILkotlin/jvm/internal/DefaultConstructorMarker;)V + public fun (Lio/sentry/IScopes;)V + public fun (Lio/sentry/IScopes;Lio/sentry/okhttp/SentryOkHttpInterceptor$BeforeSpanCallback;ZLjava/util/List;Ljava/util/List;)V + public synthetic fun (Lio/sentry/IScopes;Lio/sentry/okhttp/SentryOkHttpInterceptor$BeforeSpanCallback;ZLjava/util/List;Ljava/util/List;ILkotlin/jvm/internal/DefaultConstructorMarker;)V public fun (Lio/sentry/okhttp/SentryOkHttpInterceptor$BeforeSpanCallback;)V public fun intercept (Lokhttp3/Interceptor$Chain;)Lokhttp3/Response; } diff --git a/sentry-okhttp/src/main/java/io/sentry/okhttp/SentryOkHttpEvent.kt b/sentry-okhttp/src/main/java/io/sentry/okhttp/SentryOkHttpEvent.kt index 00cc26e754..153499210c 100644 --- a/sentry-okhttp/src/main/java/io/sentry/okhttp/SentryOkHttpEvent.kt +++ b/sentry-okhttp/src/main/java/io/sentry/okhttp/SentryOkHttpEvent.kt @@ -2,7 +2,7 @@ package io.sentry.okhttp import io.sentry.Breadcrumb import io.sentry.Hint -import io.sentry.IHub +import io.sentry.IScopes import io.sentry.ISpan import io.sentry.SentryDate import io.sentry.SentryLevel @@ -30,7 +30,7 @@ private const val RESPONSE_BODY_TIMEOUT_MILLIS = 800L internal const val TRACE_ORIGIN = "auto.http.okhttp" @Suppress("TooManyFunctions") -internal class SentryOkHttpEvent(private val hub: IHub, private val request: Request) { +internal class SentryOkHttpEvent(private val scopes: IScopes, private val request: Request) { private val eventSpans: MutableMap = ConcurrentHashMap() private val breadcrumb: Breadcrumb internal val callRootSpan: ISpan? @@ -47,7 +47,7 @@ internal class SentryOkHttpEvent(private val hub: IHub, private val request: Req val method: String = request.method // We start the call span that will contain all the others - val parentSpan = if (Platform.isAndroid()) hub.transaction else hub.span + val parentSpan = if (Platform.isAndroid()) scopes.transaction else scopes.span callRootSpan = parentSpan?.startChild("http.client", "$method $url") callRootSpan?.spanContext?.origin = TRACE_ORIGIN urlDetails.applyToSpan(callRootSpan) @@ -149,13 +149,13 @@ internal class SentryOkHttpEvent(private val hub: IHub, private val request: Req response?.let { hint.set(TypeCheckHint.OKHTTP_RESPONSE, it) } // We send the breadcrumb even without spans. - hub.addBreadcrumb(breadcrumb, hint) + scopes.addBreadcrumb(breadcrumb, hint) // No span is created (e.g. no transaction is running) if (callRootSpan == null) { // We report the client error even without spans. clientErrorResponse?.let { - SentryOkHttpUtils.captureClientError(hub, it.request, it) + SentryOkHttpUtils.captureClientError(scopes, it.request, it) } return } @@ -173,7 +173,7 @@ internal class SentryOkHttpEvent(private val hub: IHub, private val request: Req // We report the client error here, after all sub-spans finished, so that it will be bound // to the root call span. clientErrorResponse?.let { - SentryOkHttpUtils.captureClientError(hub, it.request, it) + SentryOkHttpUtils.captureClientError(scopes, it.request, it) } if (finishDate != null) { callRootSpan.finish(callRootSpan.status, finishDate) @@ -204,7 +204,7 @@ internal class SentryOkHttpEvent(private val hub: IHub, private val request: Req fun scheduleFinish(timestamp: SentryDate) { try { - hub.options.executorService.schedule({ + scopes.options.executorService.schedule({ if (!isReadingResponseBody.get() && (eventSpans.values.all { it.isFinished } || callRootSpan?.isFinished != true) ) { @@ -212,7 +212,7 @@ internal class SentryOkHttpEvent(private val hub: IHub, private val request: Req } }, RESPONSE_BODY_TIMEOUT_MILLIS) } catch (e: RejectedExecutionException) { - hub.options + scopes.options .logger .log( SentryLevel.ERROR, diff --git a/sentry-okhttp/src/main/java/io/sentry/okhttp/SentryOkHttpEventListener.kt b/sentry-okhttp/src/main/java/io/sentry/okhttp/SentryOkHttpEventListener.kt index 67a8cd8b56..f20e2ef0cb 100644 --- a/sentry-okhttp/src/main/java/io/sentry/okhttp/SentryOkHttpEventListener.kt +++ b/sentry-okhttp/src/main/java/io/sentry/okhttp/SentryOkHttpEventListener.kt @@ -1,7 +1,7 @@ package io.sentry.okhttp -import io.sentry.HubAdapter -import io.sentry.IHub +import io.sentry.IScopes +import io.sentry.ScopesAdapter import io.sentry.SpanDataConvention import io.sentry.SpanStatus import okhttp3.Call @@ -41,7 +41,7 @@ import java.util.concurrent.ConcurrentHashMap */ @Suppress("TooManyFunctions") public open class SentryOkHttpEventListener( - private val hub: IHub = HubAdapter.getInstance(), + private val scopes: IScopes = ScopesAdapter.getInstance(), private val originalEventListenerCreator: ((call: Call) -> EventListener)? = null ) : EventListener() { @@ -62,27 +62,27 @@ public open class SentryOkHttpEventListener( } public constructor() : this( - HubAdapter.getInstance(), + ScopesAdapter.getInstance(), originalEventListenerCreator = null ) public constructor(originalEventListener: EventListener) : this( - HubAdapter.getInstance(), + ScopesAdapter.getInstance(), originalEventListenerCreator = { originalEventListener } ) public constructor(originalEventListenerFactory: Factory) : this( - HubAdapter.getInstance(), + ScopesAdapter.getInstance(), originalEventListenerCreator = { originalEventListenerFactory.create(it) } ) - public constructor(hub: IHub = HubAdapter.getInstance(), originalEventListener: EventListener) : this( - hub, + public constructor(scopes: IScopes = ScopesAdapter.getInstance(), originalEventListener: EventListener) : this( + scopes, originalEventListenerCreator = { originalEventListener } ) - public constructor(hub: IHub = HubAdapter.getInstance(), originalEventListenerFactory: Factory) : this( - hub, + public constructor(scopes: IScopes = ScopesAdapter.getInstance(), originalEventListenerFactory: Factory) : this( + scopes, originalEventListenerCreator = { originalEventListenerFactory.create(it) } ) @@ -92,7 +92,7 @@ public open class SentryOkHttpEventListener( // If the wrapped EventListener is ours, we can just delegate the calls, // without creating other events that would create duplicates if (canCreateEventSpan()) { - eventMap[call] = SentryOkHttpEvent(hub, call.request()) + eventMap[call] = SentryOkHttpEvent(scopes, call.request()) } } @@ -318,7 +318,7 @@ public open class SentryOkHttpEventListener( it.status = SpanStatus.fromHttpStatusCode(response.code) } } - okHttpEvent.scheduleFinish(responseHeadersSpan?.finishDate ?: hub.options.dateProvider.now()) + okHttpEvent.scheduleFinish(responseHeadersSpan?.finishDate ?: scopes.options.dateProvider.now()) } override fun responseBodyStart(call: Call) { diff --git a/sentry-okhttp/src/main/java/io/sentry/okhttp/SentryOkHttpInterceptor.kt b/sentry-okhttp/src/main/java/io/sentry/okhttp/SentryOkHttpInterceptor.kt index efa472963d..a079864612 100644 --- a/sentry-okhttp/src/main/java/io/sentry/okhttp/SentryOkHttpInterceptor.kt +++ b/sentry-okhttp/src/main/java/io/sentry/okhttp/SentryOkHttpInterceptor.kt @@ -4,9 +4,9 @@ import io.sentry.BaggageHeader import io.sentry.Breadcrumb import io.sentry.Hint import io.sentry.HttpStatusCodeRange -import io.sentry.HubAdapter -import io.sentry.IHub +import io.sentry.IScopes import io.sentry.ISpan +import io.sentry.ScopesAdapter import io.sentry.SentryIntegrationPackageStorage import io.sentry.SentryOptions.DEFAULT_PROPAGATION_TARGETS import io.sentry.SpanDataConvention @@ -29,7 +29,7 @@ import java.io.IOException * out of the active span bound to the scope for each HTTP Request. * If [captureFailedRequests] is enabled, the SDK will capture HTTP Client errors as well. * - * @param hub The [IHub], internal and only used for testing. + * @param scopes The [IScopes], internal and only used for testing. * @param beforeSpan The [ISpan] can be customized or dropped with the [BeforeSpanCallback]. * @param captureFailedRequests The SDK will only capture HTTP Client errors if it is enabled, * Defaults to false. @@ -39,7 +39,7 @@ import java.io.IOException * is a match for any of the defined targets. */ public open class SentryOkHttpInterceptor( - private val hub: IHub = HubAdapter.getInstance(), + private val scopes: IScopes = ScopesAdapter.getInstance(), private val beforeSpan: BeforeSpanCallback? = null, private val captureFailedRequests: Boolean = true, private val failedRequestStatusCodes: List = listOf( @@ -48,9 +48,9 @@ public open class SentryOkHttpInterceptor( private val failedRequestTargets: List = listOf(DEFAULT_PROPAGATION_TARGETS) ) : Interceptor { - public constructor() : this(HubAdapter.getInstance()) - public constructor(hub: IHub) : this(hub, null) - public constructor(beforeSpan: BeforeSpanCallback) : this(HubAdapter.getInstance(), beforeSpan) + public constructor() : this(ScopesAdapter.getInstance()) + public constructor(scopes: IScopes) : this(scopes, null) + public constructor(beforeSpan: BeforeSpanCallback) : this(ScopesAdapter.getInstance(), beforeSpan) init { addIntegrationToSdkVersion(javaClass) @@ -76,7 +76,7 @@ public open class SentryOkHttpInterceptor( } else { // read the span from the bound scope okHttpEvent = null - val parentSpan = if (Platform.isAndroid()) hub.transaction else hub.span + val parentSpan = if (Platform.isAndroid()) scopes.transaction else scopes.span span = parentSpan?.startChild("http.client", "$method $url") } @@ -92,7 +92,7 @@ public open class SentryOkHttpInterceptor( val requestBuilder = request.newBuilder() TracingUtils.traceIfAllowed( - hub, + scopes, request.url.toString(), request.headers(BaggageHeader.BAGGAGE_HEADER), span @@ -121,7 +121,7 @@ public open class SentryOkHttpInterceptor( if (isFromEventListener && okHttpEvent != null) { okHttpEvent.setClientErrorResponse(response) } else { - SentryOkHttpUtils.captureClientError(hub, request, response) + SentryOkHttpUtils.captureClientError(scopes, request, response) } } @@ -157,7 +157,7 @@ public open class SentryOkHttpInterceptor( hint[OKHTTP_RESPONSE] = it } - hub.addBreadcrumb(breadcrumb, hint) + scopes.addBreadcrumb(breadcrumb, hint) } private fun finishSpan(span: ISpan?, request: Request, response: Response?, isFromEventListener: Boolean) { diff --git a/sentry-okhttp/src/main/java/io/sentry/okhttp/SentryOkHttpUtils.kt b/sentry-okhttp/src/main/java/io/sentry/okhttp/SentryOkHttpUtils.kt index 0cfc1c5a75..eea35ca22e 100644 --- a/sentry-okhttp/src/main/java/io/sentry/okhttp/SentryOkHttpUtils.kt +++ b/sentry-okhttp/src/main/java/io/sentry/okhttp/SentryOkHttpUtils.kt @@ -1,7 +1,7 @@ package io.sentry.okhttp import io.sentry.Hint -import io.sentry.IHub +import io.sentry.IScopes import io.sentry.SentryEvent import io.sentry.TypeCheckHint import io.sentry.exception.ExceptionMechanismException @@ -15,7 +15,7 @@ import okhttp3.Response internal object SentryOkHttpUtils { - internal fun captureClientError(hub: IHub, request: Request, response: Response) { + internal fun captureClientError(scopes: IScopes, request: Request, response: Response) { // not possible to get a parameterized url, but we remove at least the // query string and the fragment. // url example: https://api.github.com/users/getsentry/repos/#fragment?query=query @@ -40,9 +40,9 @@ internal object SentryOkHttpUtils { val sentryRequest = io.sentry.protocol.Request().apply { urlDetails.applyToRequest(this) // Cookie is only sent if isSendDefaultPii is enabled - cookies = if (hub.options.isSendDefaultPii) request.headers["Cookie"] else null + cookies = if (scopes.options.isSendDefaultPii) request.headers["Cookie"] else null method = request.method - headers = getHeaders(hub, request.headers) + headers = getHeaders(scopes, request.headers) request.body?.contentLength().ifHasValidLength { bodySize = it @@ -51,8 +51,8 @@ internal object SentryOkHttpUtils { val sentryResponse = io.sentry.protocol.Response().apply { // Set-Cookie is only sent if isSendDefaultPii is enabled due to PII - cookies = if (hub.options.isSendDefaultPii) response.headers["Set-Cookie"] else null - headers = getHeaders(hub, response.headers) + cookies = if (scopes.options.isSendDefaultPii) response.headers["Set-Cookie"] else null + headers = getHeaders(scopes, response.headers) statusCode = response.code response.body?.contentLength().ifHasValidLength { @@ -63,7 +63,7 @@ internal object SentryOkHttpUtils { event.request = sentryRequest event.contexts.setResponse(sentryResponse) - hub.captureEvent(event, hint) + scopes.captureEvent(event, hint) } private fun Long?.ifHasValidLength(fn: (Long) -> Unit) { @@ -72,9 +72,9 @@ internal object SentryOkHttpUtils { } } - private fun getHeaders(hub: IHub, requestHeaders: Headers): MutableMap? { + private fun getHeaders(scopes: IScopes, requestHeaders: Headers): MutableMap? { // Headers are only sent if isSendDefaultPii is enabled due to PII - if (!hub.options.isSendDefaultPii) { + if (!scopes.options.isSendDefaultPii) { return null } diff --git a/sentry-okhttp/src/test/java/io/sentry/okhttp/SentryOkHttpEventListenerTest.kt b/sentry-okhttp/src/test/java/io/sentry/okhttp/SentryOkHttpEventListenerTest.kt index 1d90da70fe..3a90a4c05c 100644 --- a/sentry-okhttp/src/test/java/io/sentry/okhttp/SentryOkHttpEventListenerTest.kt +++ b/sentry-okhttp/src/test/java/io/sentry/okhttp/SentryOkHttpEventListenerTest.kt @@ -1,7 +1,7 @@ package io.sentry.okhttp import io.sentry.BaggageHeader -import io.sentry.IHub +import io.sentry.IScopes import io.sentry.SentryOptions import io.sentry.SentryTraceHeader import io.sentry.SentryTracer @@ -36,7 +36,7 @@ import kotlin.test.assertTrue class SentryOkHttpEventListenerTest { class Fixture { - val hub = mock() + val scopes = mock() val server = MockWebServer() val mockEventListener = mock() val mockEventListenerFactory = mock() @@ -63,12 +63,12 @@ class SentryOkHttpEventListenerTest { isSendDefaultPii = sendDefaultPii configureOptions(this) } - whenever(hub.options).thenReturn(options) + whenever(scopes.options).thenReturn(options) - sentryTracer = SentryTracer(TransactionContext("name", "op"), hub) + sentryTracer = SentryTracer(TransactionContext("name", "op"), scopes) if (isSpanActive) { - whenever(hub.span).thenReturn(sentryTracer) + whenever(scopes.span).thenReturn(sentryTracer) } server.enqueue( MockResponse() @@ -80,12 +80,12 @@ class SentryOkHttpEventListenerTest { val builder = OkHttpClient.Builder() if (useInterceptor) { - builder.addInterceptor(SentryOkHttpInterceptor(hub)) + builder.addInterceptor(SentryOkHttpInterceptor(scopes)) } sentryOkHttpEventListener = when { - eventListenerFactory != null -> SentryOkHttpEventListener(hub, eventListenerFactory) - eventListener != null -> SentryOkHttpEventListener(hub, eventListener) - else -> SentryOkHttpEventListener(hub) + eventListenerFactory != null -> SentryOkHttpEventListener(scopes, eventListenerFactory) + eventListener != null -> SentryOkHttpEventListener(scopes, eventListener) + else -> SentryOkHttpEventListener(scopes) } return builder.eventListener(sentryOkHttpEventListener).build() } @@ -276,7 +276,7 @@ class SentryOkHttpEventListenerTest { @Test fun `propagate all calls to the SentryOkHttpEventListener passed in the ctor`() { - val originalListener = spy(SentryOkHttpEventListener(fixture.hub, fixture.mockEventListener)) + val originalListener = spy(SentryOkHttpEventListener(fixture.scopes, fixture.mockEventListener)) val sut = fixture.getSut(eventListener = originalListener) val listener = fixture.sentryOkHttpEventListener val request = postRequest(body = "requestBody") @@ -288,7 +288,7 @@ class SentryOkHttpEventListenerTest { @Test fun `propagate all calls to the SentryOkHttpEventListener factory passed in the ctor`() { - val originalListener = spy(SentryOkHttpEventListener(fixture.hub, fixture.mockEventListener)) + val originalListener = spy(SentryOkHttpEventListener(fixture.scopes, fixture.mockEventListener)) val sut = fixture.getSut(eventListenerFactory = { originalListener }) val listener = fixture.sentryOkHttpEventListener val request = postRequest(body = "requestBody") @@ -300,7 +300,7 @@ class SentryOkHttpEventListenerTest { @Test fun `does not duplicated spans if an SentryOkHttpEventListener is passed in the ctor`() { - val originalListener = spy(SentryOkHttpEventListener(fixture.hub, fixture.mockEventListener)) + val originalListener = spy(SentryOkHttpEventListener(fixture.scopes, fixture.mockEventListener)) val sut = fixture.getSut(eventListener = originalListener) val request = postRequest(body = "requestBody") val call = sut.newCall(request) @@ -363,8 +363,8 @@ class SentryOkHttpEventListenerTest { @Test fun `responseHeadersEnd schedules event finish`() { - val listener = SentryOkHttpEventListener(fixture.hub, fixture.mockEventListener) - whenever(fixture.hub.options).thenReturn(SentryOptions()) + val listener = SentryOkHttpEventListener(fixture.scopes, fixture.mockEventListener) + whenever(fixture.scopes.options).thenReturn(SentryOptions()) val call = mock() whenever(call.request()).thenReturn(getRequest()) val okHttpEvent = mock() diff --git a/sentry-okhttp/src/test/java/io/sentry/okhttp/SentryOkHttpEventTest.kt b/sentry-okhttp/src/test/java/io/sentry/okhttp/SentryOkHttpEventTest.kt index 337f722848..4d9f005143 100644 --- a/sentry-okhttp/src/test/java/io/sentry/okhttp/SentryOkHttpEventTest.kt +++ b/sentry-okhttp/src/test/java/io/sentry/okhttp/SentryOkHttpEventTest.kt @@ -2,7 +2,7 @@ package io.sentry.okhttp import io.sentry.Breadcrumb import io.sentry.Hint -import io.sentry.IHub +import io.sentry.IScopes import io.sentry.ISentryExecutorService import io.sentry.ISpan import io.sentry.SentryDate @@ -50,14 +50,14 @@ import kotlin.test.assertTrue class SentryOkHttpEventTest { private class Fixture { - val hub = mock() + val scopes = mock() val server = MockWebServer() val span: ISpan val mockRequest: Request val response: Response init { - whenever(hub.options).thenReturn( + whenever(scopes.options).thenReturn( SentryOptions().apply { dsn = "https://key@sentry.io/proj" } @@ -65,8 +65,8 @@ class SentryOkHttpEventTest { span = Span( TransactionContext("name", "op", TracesSamplingDecision(true)), - SentryTracer(TransactionContext("name", "op", TracesSamplingDecision(true)), hub), - hub, + SentryTracer(TransactionContext("name", "op", TracesSamplingDecision(true)), scopes), + scopes, null, SpanOptions() ) @@ -86,7 +86,7 @@ class SentryOkHttpEventTest { } fun getSut(currentSpan: ISpan? = span, requestUrl: String ? = null): SentryOkHttpEvent { - whenever(hub.span).thenReturn(currentSpan) + whenever(scopes.span).thenReturn(currentSpan) val request = if (requestUrl == null) { mockRequest } else { @@ -96,7 +96,7 @@ class SentryOkHttpEventTest { .url(server.url(requestUrl)) .build() } - return SentryOkHttpEvent(hub, request) + return SentryOkHttpEvent(scopes, request) } } @@ -126,7 +126,7 @@ class SentryOkHttpEventTest { val sut = fixture.getSut(currentSpan = null) assertNull(sut.callRootSpan) sut.finishEvent() - verify(fixture.hub).addBreadcrumb(any(), anyOrNull()) + verify(fixture.scopes).addBreadcrumb(any(), anyOrNull()) } @Test @@ -240,7 +240,7 @@ class SentryOkHttpEventTest { fun `when finishEvent, a breadcrumb is captured with request in the hint`() { val sut = fixture.getSut() sut.finishEvent() - verify(fixture.hub).addBreadcrumb( + verify(fixture.scopes).addBreadcrumb( check { assertEquals(fixture.mockRequest.url.toString(), it.data["url"]) assertEquals(fixture.mockRequest.url.host, it.data["host"]) @@ -258,7 +258,7 @@ class SentryOkHttpEventTest { val sut = fixture.getSut() sut.finishEvent() sut.finishEvent() - verify(fixture.hub, times(1)).addBreadcrumb(any(), any()) + verify(fixture.scopes, times(1)).addBreadcrumb(any(), any()) } @Test @@ -283,7 +283,7 @@ class SentryOkHttpEventTest { assertEquals(fixture.response.code, sut.callRootSpan?.getData(SpanDataConvention.HTTP_STATUS_CODE_KEY)) sut.finishEvent() - verify(fixture.hub).addBreadcrumb( + verify(fixture.scopes).addBreadcrumb( check { assertEquals(fixture.response.protocol.name, it.data["protocol"]) assertEquals(fixture.response.code, it.data["status_code"]) @@ -300,7 +300,7 @@ class SentryOkHttpEventTest { sut.setProtocol("protocol") assertEquals("protocol", sut.callRootSpan?.getData("protocol")) sut.finishEvent() - verify(fixture.hub).addBreadcrumb( + verify(fixture.scopes).addBreadcrumb( check { assertEquals("protocol", it.data["protocol"]) }, @@ -314,7 +314,7 @@ class SentryOkHttpEventTest { sut.setProtocol(null) assertNull(sut.callRootSpan?.getData("protocol")) sut.finishEvent() - verify(fixture.hub).addBreadcrumb( + verify(fixture.scopes).addBreadcrumb( check { assertNull(it.data["protocol"]) }, @@ -328,7 +328,7 @@ class SentryOkHttpEventTest { sut.setRequestBodySize(10) assertEquals(10L, sut.callRootSpan?.getData("http.request_content_length")) sut.finishEvent() - verify(fixture.hub).addBreadcrumb( + verify(fixture.scopes).addBreadcrumb( check { assertEquals(10L, it.data["request_content_length"]) }, @@ -342,7 +342,7 @@ class SentryOkHttpEventTest { sut.setRequestBodySize(-1) assertNull(sut.callRootSpan?.getData("http.request_content_length")) sut.finishEvent() - verify(fixture.hub).addBreadcrumb( + verify(fixture.scopes).addBreadcrumb( check { assertNull(it.data["request_content_length"]) }, @@ -356,7 +356,7 @@ class SentryOkHttpEventTest { sut.setResponseBodySize(10) assertEquals(10L, sut.callRootSpan?.getData(SpanDataConvention.HTTP_RESPONSE_CONTENT_LENGTH_KEY)) sut.finishEvent() - verify(fixture.hub).addBreadcrumb( + verify(fixture.scopes).addBreadcrumb( check { assertEquals(10L, it.data["response_content_length"]) }, @@ -370,7 +370,7 @@ class SentryOkHttpEventTest { sut.setResponseBodySize(-1) assertNull(sut.callRootSpan?.getData(SpanDataConvention.HTTP_RESPONSE_CONTENT_LENGTH_KEY)) sut.finishEvent() - verify(fixture.hub).addBreadcrumb( + verify(fixture.scopes).addBreadcrumb( check { assertNull(it.data["response_content_length"]) }, @@ -384,7 +384,7 @@ class SentryOkHttpEventTest { sut.setError("errorMessage") assertEquals("errorMessage", sut.callRootSpan?.getData("error_message")) sut.finishEvent() - verify(fixture.hub).addBreadcrumb( + verify(fixture.scopes).addBreadcrumb( check { assertEquals("errorMessage", it.data["error_message"]) }, @@ -399,7 +399,7 @@ class SentryOkHttpEventTest { assertNotNull(sut.callRootSpan) assertNull(sut.callRootSpan.getData("error_message")) sut.finishEvent() - verify(fixture.hub).addBreadcrumb( + verify(fixture.scopes).addBreadcrumb( check { assertNull(it.data["error_message"]) }, @@ -532,7 +532,7 @@ class SentryOkHttpEventTest { @Test fun `scheduleFinish schedules finishEvent and finish running spans to specific timestamp`() { - fixture.hub.options.executorService = ImmediateExecutorService() + fixture.scopes.options.executorService = ImmediateExecutorService() val sut = spy(fixture.getSut()) val timestamp = mock() sut.startSpan(CONNECTION_EVENT) @@ -554,7 +554,7 @@ class SentryOkHttpEventTest { fun `scheduleFinish does not throw if executor is shut down`() { val executorService = mock() whenever(executorService.schedule(any(), any())).thenThrow(RejectedExecutionException()) - whenever(fixture.hub.options).thenReturn(SentryOptions().apply { this.executorService = executorService }) + whenever(fixture.scopes.options).thenReturn(SentryOptions().apply { this.executorService = executorService }) val sut = fixture.getSut() sut.scheduleFinish(mock()) } @@ -565,10 +565,10 @@ class SentryOkHttpEventTest { val clientErrorResponse = mock() whenever(clientErrorResponse.request).thenReturn(fixture.mockRequest) sut.setClientErrorResponse(clientErrorResponse) - verify(fixture.hub, never()).captureEvent(any(), any()) + verify(fixture.scopes, never()).captureEvent(any(), any()) sut.finishEvent() assertNotNull(sut.callRootSpan) - verify(fixture.hub).captureEvent( + verify(fixture.scopes).captureEvent( argThat { throwable is SentryHttpClientException && throwable!!.message!!.startsWith("HTTP Client Error with status code: ") @@ -586,10 +586,10 @@ class SentryOkHttpEventTest { val clientErrorResponse = mock() whenever(clientErrorResponse.request).thenReturn(fixture.mockRequest) sut.setClientErrorResponse(clientErrorResponse) - verify(fixture.hub, never()).captureEvent(any(), any()) + verify(fixture.scopes, never()).captureEvent(any(), any()) sut.finishEvent() assertNull(sut.callRootSpan) - verify(fixture.hub).captureEvent( + verify(fixture.scopes).captureEvent( argThat { throwable is SentryHttpClientException && throwable!!.message!!.startsWith("HTTP Client Error with status code: ") @@ -605,7 +605,7 @@ class SentryOkHttpEventTest { fun `when setClientErrorResponse is not called, no client error is captured`() { val sut = fixture.getSut() sut.finishEvent() - verify(fixture.hub, never()).captureEvent(any(), any()) + verify(fixture.scopes, never()).captureEvent(any(), any()) } /** Retrieve all the spans started in the event using reflection. */ diff --git a/sentry-okhttp/src/test/java/io/sentry/okhttp/SentryOkHttpInterceptorTest.kt b/sentry-okhttp/src/test/java/io/sentry/okhttp/SentryOkHttpInterceptorTest.kt index fce16d9220..f40b2c4cb5 100644 --- a/sentry-okhttp/src/test/java/io/sentry/okhttp/SentryOkHttpInterceptorTest.kt +++ b/sentry-okhttp/src/test/java/io/sentry/okhttp/SentryOkHttpInterceptorTest.kt @@ -6,8 +6,8 @@ import io.sentry.BaggageHeader import io.sentry.Breadcrumb import io.sentry.Hint import io.sentry.HttpStatusCodeRange -import io.sentry.IHub import io.sentry.IScope +import io.sentry.IScopes import io.sentry.Scope import io.sentry.ScopeCallback import io.sentry.SentryOptions @@ -47,7 +47,7 @@ import kotlin.test.fail class SentryOkHttpInterceptorTest { class Fixture { - val hub = mock() + val scopes = mock() val server = MockWebServer() lateinit var sentryTracer: SentryTracer lateinit var options: SentryOptions @@ -82,13 +82,13 @@ class SentryOkHttpInterceptorTest { isSendDefaultPii = sendDefaultPii } scope = Scope(options) - whenever(hub.options).thenReturn(options) - doAnswer { (it.arguments[0] as ScopeCallback).run(scope) }.whenever(hub).configureScope(any()) + whenever(scopes.options).thenReturn(options) + doAnswer { (it.arguments[0] as ScopeCallback).run(scope) }.whenever(scopes).configureScope(any()) - sentryTracer = SentryTracer(TransactionContext("name", "op"), hub) + sentryTracer = SentryTracer(TransactionContext("name", "op"), scopes) if (isSpanActive) { - whenever(hub.span).thenReturn(sentryTracer) + whenever(scopes.span).thenReturn(sentryTracer) } server.enqueue( MockResponse() @@ -100,14 +100,14 @@ class SentryOkHttpInterceptorTest { val interceptor = when (captureFailedRequests) { null -> SentryOkHttpInterceptor( - hub, + scopes, beforeSpan, failedRequestTargets = failedRequestTargets, failedRequestStatusCodes = failedRequestStatusCodes ) else -> SentryOkHttpInterceptor( - hub, + scopes, beforeSpan, captureFailedRequests = captureFailedRequests, failedRequestTargets = failedRequestTargets, @@ -281,7 +281,7 @@ class SentryOkHttpInterceptorTest { fun `adds breadcrumb when http calls succeeds`() { val sut = fixture.getSut(responseBody = "response body") sut.newCall(postRequest()).execute() - verify(fixture.hub).addBreadcrumb( + verify(fixture.scopes).addBreadcrumb( check { assertEquals("http", it.type) assertEquals(13L, it.data[SpanDataConvention.HTTP_RESPONSE_CONTENT_LENGTH_KEY]) @@ -296,7 +296,7 @@ class SentryOkHttpInterceptorTest { fun `adds breadcrumb when http calls results in exception`() { // to setup mocks fixture.getSut() - val interceptor = SentryOkHttpInterceptor(fixture.hub) + val interceptor = SentryOkHttpInterceptor(fixture.scopes) val chain = mock() whenever(chain.call()).thenReturn(mock()) whenever(chain.proceed(any())).thenThrow(IOException()) @@ -308,7 +308,7 @@ class SentryOkHttpInterceptorTest { } catch (e: IOException) { // ignore me } - verify(fixture.hub).addBreadcrumb( + verify(fixture.scopes).addBreadcrumb( check { assertEquals("http", it.type) }, @@ -385,7 +385,7 @@ class SentryOkHttpInterceptorTest { ) sut.newCall(getRequest()).execute() - verify(fixture.hub).captureEvent(any(), any()) + verify(fixture.scopes).captureEvent(any(), any()) } @Test @@ -396,7 +396,7 @@ class SentryOkHttpInterceptorTest { ) sut.newCall(getRequest()).execute() - verify(fixture.hub).captureEvent(any(), any()) + verify(fixture.scopes).captureEvent(any(), any()) } @Test @@ -406,7 +406,7 @@ class SentryOkHttpInterceptorTest { ) sut.newCall(getRequest()).execute() - verify(fixture.hub, never()).captureEvent(any(), any()) + verify(fixture.scopes, never()).captureEvent(any(), any()) } @Test @@ -417,7 +417,7 @@ class SentryOkHttpInterceptorTest { ) sut.newCall(getRequest()).execute() - verify(fixture.hub, never()).captureEvent(any(), any()) + verify(fixture.scopes, never()).captureEvent(any(), any()) } @Test @@ -429,7 +429,7 @@ class SentryOkHttpInterceptorTest { ) sut.newCall(getRequest()).execute() - verify(fixture.hub, never()).captureEvent(any(), any()) + verify(fixture.scopes, never()).captureEvent(any(), any()) } @Test @@ -440,7 +440,7 @@ class SentryOkHttpInterceptorTest { ) sut.newCall(getRequest()).execute() - verify(fixture.hub).captureEvent( + verify(fixture.scopes).captureEvent( any(), check { assertNotNull(it.get(TypeCheckHint.OKHTTP_REQUEST)) @@ -462,7 +462,7 @@ class SentryOkHttpInterceptorTest { val request = getRequest(url = "/hello?myQuery=myValue#myFragment") val response = sut.newCall(request).execute() - verify(fixture.hub).captureEvent( + verify(fixture.scopes).captureEvent( check { val sentryRequest = it.request!! assertEquals("http://localhost:${fixture.server.port}/hello", sentryRequest.url) @@ -503,7 +503,7 @@ class SentryOkHttpInterceptorTest { sut.newCall(postRequest(body = body)).execute() - verify(fixture.hub).captureEvent( + verify(fixture.scopes).captureEvent( check { val sentryRequest = it.request!! assertEquals(body.contentLength(), sentryRequest.bodySize) @@ -522,7 +522,7 @@ class SentryOkHttpInterceptorTest { sut.newCall(getRequest()).execute() - verify(fixture.hub).captureEvent( + verify(fixture.scopes).captureEvent( check { val sentryRequest = it.request!! assertEquals("myValue", sentryRequest.headers!!["myHeader"]) @@ -540,7 +540,7 @@ class SentryOkHttpInterceptorTest { // to setup mocks fixture.getSut() val interceptor = SentryOkHttpInterceptor( - fixture.hub, + fixture.scopes, captureFailedRequests = true ) val chain = mock() @@ -554,7 +554,7 @@ class SentryOkHttpInterceptorTest { } catch (e: IOException) { // ignore me } - verify(fixture.hub, never()).captureEvent(any(), any()) + verify(fixture.scopes, never()).captureEvent(any(), any()) } @Test @@ -565,7 +565,7 @@ class SentryOkHttpInterceptorTest { call.execute() val httpClientSpan = fixture.sentryTracer.children.firstOrNull() assertNull(httpClientSpan) - verify(fixture.hub, never()).addBreadcrumb(any(), anyOrNull()) + verify(fixture.scopes, never()).addBreadcrumb(any(), anyOrNull()) } @Test @@ -573,7 +573,7 @@ class SentryOkHttpInterceptorTest { val sut = fixture.getSut(captureFailedRequests = true, httpStatusCode = 500) val call = sut.newCall(getRequest()) call.execute() - verify(fixture.hub).captureEvent(any(), any()) + verify(fixture.scopes).captureEvent(any(), any()) } @Test @@ -582,6 +582,6 @@ class SentryOkHttpInterceptorTest { val call = sut.newCall(getRequest()) SentryOkHttpEventListener.eventMap[call] = mock() call.execute() - verify(fixture.hub, never()).captureEvent(any(), any()) + verify(fixture.scopes, never()).captureEvent(any(), any()) } } diff --git a/sentry-okhttp/src/test/java/io/sentry/okhttp/SentryOkHttpUtilsTest.kt b/sentry-okhttp/src/test/java/io/sentry/okhttp/SentryOkHttpUtilsTest.kt index ec19454327..c7194e5994 100644 --- a/sentry-okhttp/src/test/java/io/sentry/okhttp/SentryOkHttpUtilsTest.kt +++ b/sentry-okhttp/src/test/java/io/sentry/okhttp/SentryOkHttpUtilsTest.kt @@ -1,7 +1,7 @@ package io.sentry.okhttp import io.sentry.Hint -import io.sentry.IHub +import io.sentry.IScopes import io.sentry.SentryOptions import io.sentry.SentryTracer import io.sentry.TransactionContext @@ -29,7 +29,7 @@ import kotlin.test.assertTrue class SentryOkHttpUtilsTest { class Fixture { - val hub = mock() + val scopes = mock() val server = MockWebServer() fun getSut( @@ -43,11 +43,11 @@ class SentryOkHttpUtilsTest { setTracePropagationTargets(listOf(server.hostName)) isSendDefaultPii = sendDefaultPii } - whenever(hub.options).thenReturn(options) + whenever(scopes.options).thenReturn(options) - val sentryTracer = SentryTracer(TransactionContext("name", "op"), hub) + val sentryTracer = SentryTracer(TransactionContext("name", "op"), scopes) - whenever(hub.span).thenReturn(sentryTracer) + whenever(scopes.span).thenReturn(sentryTracer) server.enqueue( MockResponse() @@ -78,8 +78,8 @@ class SentryOkHttpUtilsTest { val request = getRequest() val response = sut.newCall(request).execute() - SentryOkHttpUtils.captureClientError(fixture.hub, request, response) - verify(fixture.hub).captureEvent( + SentryOkHttpUtils.captureClientError(fixture.scopes, request, response) + verify(fixture.scopes).captureEvent( check { val req = it.request val resp = it.contexts.response @@ -103,8 +103,8 @@ class SentryOkHttpUtilsTest { val request = getRequest() val response = sut.newCall(request).execute() - SentryOkHttpUtils.captureClientError(fixture.hub, request, response) - verify(fixture.hub).captureEvent( + SentryOkHttpUtils.captureClientError(fixture.scopes, request, response) + verify(fixture.scopes).captureEvent( check { val req = it.request val resp = it.contexts.response @@ -127,8 +127,8 @@ class SentryOkHttpUtilsTest { val request = getRequest() val response = sut.newCall(request).execute() - SentryOkHttpUtils.captureClientError(fixture.hub, request, response) - verify(fixture.hub).captureEvent( + SentryOkHttpUtils.captureClientError(fixture.scopes, request, response) + verify(fixture.scopes).captureEvent( check { val req = it.request val resp = it.contexts.response