Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Send breadcrumbs and client error even without transactions #3087

Merged
merged 4 commits into from
Dec 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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