Skip to content

Commit

Permalink
Send breadcrumbs and client error even without transactions (#3087)
Browse files Browse the repository at this point in the history
* moved breadcrumb sent before span null check in SentryOkHttpEvent.finishEvent()
* added client error reporting if call root span is null in SentryOkHttpEvent.finishEvent()
  • Loading branch information
stefanosiano committed Dec 6, 2023
1 parent 1f3652d commit 4e260b3
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 10 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@

- Support multiple debug-metadata.properties ([#3024](https://github.com/getsentry/sentry-java/pull/3024))

### Fixes

- Send breadcrumbs and client error even without transactions ([#3087](https://github.com/getsentry/sentry-java/pull/3087))

### Dependencies

- Bump Gradle from v8.4.0 to v8.5.0 ([#3070](https://github.com/getsentry/sentry-java/pull/3070))
Expand Down
24 changes: 16 additions & 8 deletions sentry-okhttp/src/main/java/io/sentry/okhttp/SentryOkHttpEvent.kt
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,22 @@ internal class SentryOkHttpEvent(private val hub: IHub, private val request: Req

/** Finishes the call root span, and runs [beforeFinish] on it. Then a breadcrumb is sent. */
fun finishEvent(finishDate: SentryDate? = null, beforeFinish: ((span: ISpan) -> Unit)? = null) {
callRootSpan ?: return
// We put data in the hint and send a breadcrumb
val hint = Hint()
hint.set(TypeCheckHint.OKHTTP_REQUEST, request)
response?.let { hint.set(TypeCheckHint.OKHTTP_RESPONSE, it) }

// We send the breadcrumb even without spans.
hub.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)
}
return
}

// We forcefully finish all spans, even if they should already have been finished through finishSpan()
eventSpans.values.filter { !it.isFinished }.forEach {
Expand All @@ -159,13 +174,6 @@ internal class SentryOkHttpEvent(private val hub: IHub, private val request: Req
} else {
callRootSpan.finish()
}

// We put data in the hint and send a breadcrumb
val hint = Hint()
hint.set(TypeCheckHint.OKHTTP_REQUEST, request)
response?.let { hint.set(TypeCheckHint.OKHTTP_RESPONSE, it) }

hub.addBreadcrumb(breadcrumb, hint)
return
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,11 +123,11 @@ class SentryOkHttpEventTest {
}

@Test
fun `when root span is null, no breadcrumb is created`() {
fun `when root span is null, breadcrumb is created anyway`() {
val sut = fixture.getSut(currentSpan = null)
assertNull(sut.callRootSpan)
sut.finishEvent()
verify(fixture.hub, never()).addBreadcrumb(any<Breadcrumb>(), anyOrNull())
verify(fixture.hub).addBreadcrumb(any<Breadcrumb>(), anyOrNull())
}

@Test
Expand Down Expand Up @@ -572,6 +572,28 @@ class SentryOkHttpEventTest {
sut.setClientErrorResponse(clientErrorResponse)
verify(fixture.hub, never()).captureEvent(any(), any<Hint>())
sut.finishEvent()
assertNotNull(sut.callRootSpan)
verify(fixture.hub).captureEvent(
argThat {
throwable is SentryHttpClientException &&
throwable!!.message!!.startsWith("HTTP Client Error with status code: ")
},
argThat<Hint> {
get(TypeCheckHint.OKHTTP_REQUEST) != null &&
get(TypeCheckHint.OKHTTP_RESPONSE) != null
}
)
}

@Test
fun `setClientErrorResponse will capture the client error on finishEvent even when no span is running`() {
val sut = fixture.getSut(currentSpan = null)
val clientErrorResponse = mock<Response>()
whenever(clientErrorResponse.request).thenReturn(fixture.mockRequest)
sut.setClientErrorResponse(clientErrorResponse)
verify(fixture.hub, never()).captureEvent(any(), any<Hint>())
sut.finishEvent()
assertNull(sut.callRootSpan)
verify(fixture.hub).captureEvent(
argThat {
throwable is SentryHttpClientException &&
Expand Down

0 comments on commit 4e260b3

Please sign in to comment.