Skip to content

Commit

Permalink
Use root transaction for Apollo spans on Android
Browse files Browse the repository at this point in the history
  • Loading branch information
markushi committed Aug 30, 2023
1 parent aa6e1af commit a1914e9
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import io.sentry.util.PropagationTargetsUtils
import io.sentry.util.TracingUtils
import io.sentry.util.UrlUtils
import io.sentry.vendor.Base64
import okhttp3.internal.platform.Platform
import okio.Buffer
import org.jetbrains.annotations.ApiStatus

Expand Down Expand Up @@ -62,7 +63,7 @@ class SentryApollo3HttpInterceptor @JvmOverloads constructor(
request: HttpRequest,
chain: HttpInterceptorChain
): HttpResponse {
val activeSpan = hub.span
val activeSpan = if (io.sentry.util.Platform.isAndroid()) hub.transaction else hub.span

val operationName = getHeader(HEADER_APOLLO_OPERATION_NAME, request.headers)
val operationType = decodeHeaderValue(request, SENTRY_APOLLO_3_OPERATION_TYPE)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,13 @@ import io.sentry.TransactionContext
import io.sentry.apollo3.SentryApollo3HttpInterceptor.BeforeSpanCallback
import io.sentry.protocol.SdkVersion
import io.sentry.protocol.SentryTransaction
import io.sentry.util.Apollo3PlatformTestManipulator
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import okhttp3.mockwebserver.MockResponse
import okhttp3.mockwebserver.MockWebServer
import okhttp3.mockwebserver.SocketPolicy
import org.junit.Before
import org.mockito.kotlin.any
import org.mockito.kotlin.anyOrNull
import org.mockito.kotlin.check
Expand Down Expand Up @@ -112,6 +114,11 @@ class SentryApollo3InterceptorTest {

private val fixture = Fixture()

@Before
fun setup() {
Apollo3PlatformTestManipulator.pretendIsAndroid(false)
}

@Test
fun `creates a span around the successful request`() {
executeQuery()
Expand Down Expand Up @@ -307,6 +314,20 @@ class SentryApollo3InterceptorTest {
assert(packageInfo.version == BuildConfig.VERSION_NAME)
}

@Test
fun `attaches to root transaction on Android`() {
Apollo3PlatformTestManipulator.pretendIsAndroid(true)
executeQuery(fixture.getSut())
verify(fixture.hub).transaction
}

@Test
fun `attaches to child span on non-Android`() {
Apollo3PlatformTestManipulator.pretendIsAndroid(false)
executeQuery(fixture.getSut())
verify(fixture.hub).span
}

private fun assertTransactionDetails(it: SentryTransaction, httpStatusCode: Int? = 200, contentLength: Long? = 0L) {
assertEquals(1, it.spans.size)
val httpClientSpan = it.spans.first()
Expand All @@ -328,6 +349,7 @@ class SentryApollo3InterceptorTest {
var tx: ITransaction? = null
if (isSpanActive) {
tx = SentryTracer(TransactionContext("op", "desc", TracesSamplingDecision(true)), fixture.hub)
whenever(fixture.hub.transaction).thenReturn(tx)
whenever(fixture.hub.span).thenReturn(tx)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package io.sentry.util

object Apollo3PlatformTestManipulator {

fun pretendIsAndroid(isAndroid: Boolean) {
Platform.isAndroid = isAndroid
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class SentryApolloInterceptor(
}

override fun interceptAsync(request: InterceptorRequest, chain: ApolloInterceptorChain, dispatcher: Executor, callBack: CallBack) {
val activeSpan = hub.span
val activeSpan = if (io.sentry.util.Platform.isAndroid()) hub.transaction else hub.span
if (activeSpan == null) {
val headers = addTracingHeaders(request, null)
val modifiedRequest = request.toBuilder().requestHeaders(headers).build()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@ import io.sentry.TracesSamplingDecision
import io.sentry.TransactionContext
import io.sentry.protocol.SdkVersion
import io.sentry.protocol.SentryTransaction
import io.sentry.util.ApolloPlatformTestManipulator
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import okhttp3.mockwebserver.MockResponse
import okhttp3.mockwebserver.MockWebServer
import okhttp3.mockwebserver.SocketPolicy
import org.junit.Before
import org.mockito.kotlin.any
import org.mockito.kotlin.anyOrNull
import org.mockito.kotlin.check
Expand Down Expand Up @@ -93,6 +95,11 @@ class SentryApolloInterceptorTest {

private val fixture = Fixture()

@Before
fun setup() {
ApolloPlatformTestManipulator.pretendIsAndroid(false)
}

@Test
fun `creates a span around the successful request`() {
executeQuery()
Expand Down Expand Up @@ -234,6 +241,20 @@ class SentryApolloInterceptorTest {
assert(packageInfo.version == BuildConfig.VERSION_NAME)
}

@Test
fun `attaches to root transaction on Android`() {
ApolloPlatformTestManipulator.pretendIsAndroid(true)
executeQuery(fixture.getSut())
verify(fixture.hub).transaction
}

@Test
fun `attaches to child span on non-Android`() {
ApolloPlatformTestManipulator.pretendIsAndroid(false)
executeQuery(fixture.getSut())
verify(fixture.hub).span
}

private fun assertTransactionDetails(it: SentryTransaction) {
assertEquals(1, it.spans.size)
val httpClientSpan = it.spans.first()
Expand All @@ -250,6 +271,7 @@ class SentryApolloInterceptorTest {
var tx: ITransaction? = null
if (isSpanActive) {
tx = SentryTracer(TransactionContext("op", "desc", TracesSamplingDecision(true)), fixture.hub)
whenever(fixture.hub.transaction).thenReturn(tx)
whenever(fixture.hub.span).thenReturn(tx)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package io.sentry.util

object ApolloPlatformTestManipulator {

fun pretendIsAndroid(isAndroid: Boolean) {
Platform.isAndroid = isAndroid
}
}

0 comments on commit a1914e9

Please sign in to comment.