Skip to content

Commit

Permalink
Replace IHub with IScopes in okhttp integration
Browse files Browse the repository at this point in the history
  • Loading branch information
adinauer committed Apr 2, 2024
1 parent 305c217 commit da927bc
Show file tree
Hide file tree
Showing 9 changed files with 125 additions and 125 deletions.
18 changes: 9 additions & 9 deletions sentry-okhttp/api/sentry-okhttp.api
Original file line number Diff line number Diff line change
Expand Up @@ -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 <init> ()V
public fun <init> (Lio/sentry/IHub;Lkotlin/jvm/functions/Function1;)V
public synthetic fun <init> (Lio/sentry/IHub;Lkotlin/jvm/functions/Function1;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
public fun <init> (Lio/sentry/IHub;Lokhttp3/EventListener$Factory;)V
public synthetic fun <init> (Lio/sentry/IHub;Lokhttp3/EventListener$Factory;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
public fun <init> (Lio/sentry/IHub;Lokhttp3/EventListener;)V
public synthetic fun <init> (Lio/sentry/IHub;Lokhttp3/EventListener;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
public fun <init> (Lio/sentry/IScopes;Lkotlin/jvm/functions/Function1;)V
public synthetic fun <init> (Lio/sentry/IScopes;Lkotlin/jvm/functions/Function1;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
public fun <init> (Lio/sentry/IScopes;Lokhttp3/EventListener$Factory;)V
public synthetic fun <init> (Lio/sentry/IScopes;Lokhttp3/EventListener$Factory;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
public fun <init> (Lio/sentry/IScopes;Lokhttp3/EventListener;)V
public synthetic fun <init> (Lio/sentry/IScopes;Lokhttp3/EventListener;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
public fun <init> (Lokhttp3/EventListener$Factory;)V
public fun <init> (Lokhttp3/EventListener;)V
public fun cacheConditionalHit (Lokhttp3/Call;Lokhttp3/Response;)V
Expand Down Expand Up @@ -50,9 +50,9 @@ public final class io/sentry/okhttp/SentryOkHttpEventListener$Companion {

public class io/sentry/okhttp/SentryOkHttpInterceptor : okhttp3/Interceptor {
public fun <init> ()V
public fun <init> (Lio/sentry/IHub;)V
public fun <init> (Lio/sentry/IHub;Lio/sentry/okhttp/SentryOkHttpInterceptor$BeforeSpanCallback;ZLjava/util/List;Ljava/util/List;)V
public synthetic fun <init> (Lio/sentry/IHub;Lio/sentry/okhttp/SentryOkHttpInterceptor$BeforeSpanCallback;ZLjava/util/List;Ljava/util/List;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
public fun <init> (Lio/sentry/IScopes;)V
public fun <init> (Lio/sentry/IScopes;Lio/sentry/okhttp/SentryOkHttpInterceptor$BeforeSpanCallback;ZLjava/util/List;Ljava/util/List;)V
public synthetic fun <init> (Lio/sentry/IScopes;Lio/sentry/okhttp/SentryOkHttpInterceptor$BeforeSpanCallback;ZLjava/util/List;Ljava/util/List;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
public fun <init> (Lio/sentry/okhttp/SentryOkHttpInterceptor$BeforeSpanCallback;)V
public fun intercept (Lokhttp3/Interceptor$Chain;)Lokhttp3/Response;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<String, ISpan> = ConcurrentHashMap()
private val breadcrumb: Breadcrumb
internal val callRootSpan: ISpan?
Expand All @@ -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)
Expand Down Expand Up @@ -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
}
Expand All @@ -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)
Expand Down Expand Up @@ -204,15 +204,15 @@ 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)
) {
finishEvent(timestamp)
}
}, RESPONSE_BODY_TIMEOUT_MILLIS)
} catch (e: RejectedExecutionException) {
hub.options
scopes.options
.logger
.log(
SentryLevel.ERROR,
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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() {

Expand All @@ -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) }
)

Expand All @@ -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())
}
}

Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand All @@ -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<HttpStatusCodeRange> = listOf(
Expand All @@ -48,9 +48,9 @@ public open class SentryOkHttpInterceptor(
private val failedRequestTargets: List<String> = 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)
Expand All @@ -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")
}

Expand All @@ -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
Expand Down Expand Up @@ -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)
}
}

Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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 {
Expand All @@ -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) {
Expand All @@ -72,9 +72,9 @@ internal object SentryOkHttpUtils {
}
}

private fun getHeaders(hub: IHub, requestHeaders: Headers): MutableMap<String, String>? {
private fun getHeaders(scopes: IScopes, requestHeaders: Headers): MutableMap<String, String>? {
// Headers are only sent if isSendDefaultPii is enabled due to PII
if (!hub.options.isSendDefaultPii) {
if (!scopes.options.isSendDefaultPii) {
return null
}

Expand Down
Loading

0 comments on commit da927bc

Please sign in to comment.