Skip to content

Commit

Permalink
Fix: Set Span status for OkHttp integration
Browse files Browse the repository at this point in the history
Fixes #1421
  • Loading branch information
maciejwalkowiak committed Apr 28, 2021
1 parent a33afef commit 628a5f6
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ class SentryOkHttpInterceptor(
code = response.code
return response
} catch (e: IOException) {
span?.throwable = e
span?.apply {
this.throwable = e
this.status = SpanStatus.INTERNAL_ERROR
}
throw e
} finally {
span?.finish(SpanStatus.fromHttpStatusCode(code, SpanStatus.INTERNAL_ERROR))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertNotNull
import kotlin.test.assertNull
import kotlin.test.assertTrue
import kotlin.test.fail
import okhttp3.Interceptor
import okhttp3.MediaType.Companion.toMediaType
Expand All @@ -25,6 +26,7 @@ import okhttp3.Request
import okhttp3.RequestBody.Companion.toRequestBody
import okhttp3.mockwebserver.MockResponse
import okhttp3.mockwebserver.MockWebServer
import okhttp3.mockwebserver.SocketPolicy

class SentryOkHttpInterceptorTest {

Expand All @@ -38,11 +40,11 @@ class SentryOkHttpInterceptorTest {
whenever(hub.options).thenReturn(SentryOptions())
}

fun getSut(isSpanActive: Boolean = true, httpStatusCode: Int = 201, responseBody: String = "success"): OkHttpClient {
fun getSut(isSpanActive: Boolean = true, httpStatusCode: Int = 201, responseBody: String = "success", socketPolicy: SocketPolicy = SocketPolicy.KEEP_OPEN): OkHttpClient {
if (isSpanActive) {
whenever(hub.span).thenReturn(sentryTracer)
}
server.enqueue(MockResponse().setBody(responseBody).setResponseCode(httpStatusCode))
server.enqueue(MockResponse().setBody(responseBody).setSocketPolicy(socketPolicy).setResponseCode(httpStatusCode))
server.start()
return OkHttpClient.Builder().addInterceptor(interceptor).build()
}
Expand Down Expand Up @@ -120,4 +122,15 @@ class SentryOkHttpInterceptorTest {
assertEquals("http", it.type)
})
}

@Test
fun `sets status and throwable when call results in IOException`() {
val sut = fixture.getSut(socketPolicy = SocketPolicy.DISCONNECT_AT_START)
try {
sut.newCall(Request.Builder().get().url(fixture.server.url("/hello")).build()).execute()
} catch (e: IOException) {}
val httpClientSpan = fixture.sentryTracer.children.first()
assertEquals(SpanStatus.INTERNAL_ERROR, httpClientSpan.status)
assertTrue(httpClientSpan.throwable is IOException)
}
}

0 comments on commit 628a5f6

Please sign in to comment.