Skip to content

Commit 25b5638

Browse files
authored
fix(okhttp): Finalize OkHttpEvent even if no active span (#4469)
* fix(okhttp): Finalize OkHttpEvent even if no active span * Changelog * Update sentry-okhttp/src/main/java/io/sentry/okhttp/SentryOkHttpInterceptor.kt
1 parent 6df3e18 commit 25b5638

4 files changed

Lines changed: 15 additions & 1 deletion

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
- Send UI Profiling app start chunk when it finishes ([#4423](https://github.com/getsentry/sentry-java/pull/4423))
88
- Republish Javadoc [#4457](https://github.com/getsentry/sentry-java/pull/4457)
9+
- Finalize `OkHttpEvent` even if no active span in `SentryOkHttpInterceptor` [#4469](https://github.com/getsentry/sentry-java/pull/4469)
910

1011
## 8.13.2
1112

sentry-okhttp/src/main/java/io/sentry/okhttp/SentryOkHttpEvent.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ internal class SentryOkHttpEvent(private val scopes: IScopes, private val reques
2727
internal val callSpan: ISpan?
2828
private var response: Response? = null
2929
private var clientErrorResponse: Response? = null
30-
private val isEventFinished = AtomicBoolean(false)
30+
internal val isEventFinished = AtomicBoolean(false)
3131
private var url: String
3232
private var method: String
3333

sentry-okhttp/src/main/java/io/sentry/okhttp/SentryOkHttpInterceptor.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,8 @@ public open class SentryOkHttpInterceptor(
192192

193193
private fun finishSpan(span: ISpan?, request: Request, response: Response?, isFromEventListener: Boolean, okHttpEvent: SentryOkHttpEvent?) {
194194
if (span == null) {
195+
// tracing can be disabled, or there can be no active span, but we still want to finalize the OkHttpEvent when both SentryOkHttpInterceptor and SentryOkHttpEventListener are used
196+
okHttpEvent?.finish()
195197
return
196198
}
197199
if (beforeSpan != null) {

sentry-okhttp/src/test/java/io/sentry/okhttp/SentryOkHttpInterceptorTest.kt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -650,4 +650,15 @@ class SentryOkHttpInterceptorTest {
650650
val okHttpEvent = SentryOkHttpEventListener.eventMap[call]!!
651651
assertEquals(fixture.server.url("/hello/v1").toUrl().toString(), okHttpEvent.callSpan!!.getData("url"))
652652
}
653+
654+
@Test
655+
fun `when no active http span still finalizes okHttpEvent`() {
656+
val client = fixture.getSut(isSpanActive = false, eventListener = SentryOkHttpEventListener(fixture.scopes))
657+
val request = getRequest("/hello/")
658+
val call = client.newCall(request)
659+
call.execute()
660+
661+
val okHttpEvent = SentryOkHttpEventListener.eventMap[call]!!
662+
assertTrue(okHttpEvent.isEventFinished.get())
663+
}
653664
}

0 commit comments

Comments
 (0)