-
-
Notifications
You must be signed in to change notification settings - Fork 435
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
SentryOkHttpEvent report exceptions only on the call root span #2961
Merged
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
96 changes: 96 additions & 0 deletions
96
sentry-android-okhttp/src/main/java/io/sentry/android/okhttp/SentryOkHttpUtils.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package io.sentry.android.okhttp | ||
|
||
import io.sentry.Hint | ||
import io.sentry.IHub | ||
import io.sentry.SentryEvent | ||
import io.sentry.TypeCheckHint | ||
import io.sentry.exception.ExceptionMechanismException | ||
import io.sentry.exception.SentryHttpClientException | ||
import io.sentry.protocol.Mechanism | ||
import io.sentry.util.HttpUtils | ||
import io.sentry.util.UrlUtils | ||
import okhttp3.Headers | ||
import okhttp3.Request | ||
import okhttp3.Response | ||
|
||
object SentryOkHttpUtils { | ||
|
||
fun captureClientError(hub: IHub, request: Request, response: Response) { | ||
// not possible to get a parameterized url, but we remove at least the | ||
// query string and the fragment. | ||
// url example: https://api.github.com/users/getsentry/repos/#fragment?query=query | ||
// url will be: https://api.github.com/users/getsentry/repos/ | ||
// ideally we'd like a parameterized url: https://api.github.com/users/{user}/repos/ | ||
// but that's not possible | ||
val urlDetails = UrlUtils.parse(request.url.toString()) | ||
|
||
val mechanism = Mechanism().apply { | ||
type = "SentryOkHttpInterceptor" | ||
} | ||
val exception = SentryHttpClientException( | ||
"HTTP Client Error with status code: ${response.code}" | ||
) | ||
val mechanismException = ExceptionMechanismException(mechanism, exception, Thread.currentThread(), true) | ||
val event = SentryEvent(mechanismException) | ||
|
||
val hint = Hint() | ||
hint.set(TypeCheckHint.OKHTTP_REQUEST, request) | ||
hint.set(TypeCheckHint.OKHTTP_RESPONSE, response) | ||
|
||
val sentryRequest = io.sentry.protocol.Request().apply { | ||
urlDetails.applyToRequest(this) | ||
// Cookie is only sent if isSendDefaultPii is enabled | ||
cookies = if (hub.options.isSendDefaultPii) request.headers["Cookie"] else null | ||
method = request.method | ||
headers = getHeaders(hub, request.headers) | ||
|
||
request.body?.contentLength().ifHasValidLength { | ||
bodySize = it | ||
} | ||
} | ||
|
||
val sentryResponse = io.sentry.protocol.Response().apply { | ||
// Set-Cookie is only sent if isSendDefaultPii is enabled due to PII | ||
cookies = if (hub.options.isSendDefaultPii) response.headers["Set-Cookie"] else null | ||
headers = getHeaders(hub, response.headers) | ||
statusCode = response.code | ||
|
||
response.body?.contentLength().ifHasValidLength { | ||
bodySize = it | ||
} | ||
} | ||
|
||
event.request = sentryRequest | ||
event.contexts.setResponse(sentryResponse) | ||
|
||
hub.captureEvent(event, hint) | ||
} | ||
|
||
private fun Long?.ifHasValidLength(fn: (Long) -> Unit) { | ||
if (this != null && this != -1L) { | ||
fn.invoke(this) | ||
} | ||
} | ||
|
||
private fun getHeaders(hub: IHub, requestHeaders: Headers): MutableMap<String, String>? { | ||
// Headers are only sent if isSendDefaultPii is enabled due to PII | ||
if (!hub.options.isSendDefaultPii) { | ||
return null | ||
} | ||
|
||
val headers = mutableMapOf<String, String>() | ||
|
||
for (i in 0 until requestHeaders.size) { | ||
val name = requestHeaders.name(i) | ||
|
||
// header is only sent if isn't sensitive | ||
if (HttpUtils.containsSensitiveHeader(name)) { | ||
continue | ||
} | ||
|
||
val value = requestHeaders.value(i) | ||
headers[name] = value | ||
} | ||
return headers | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's actually HTTP errors we're catching here, how about naming it this way?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually it's both
any exception caught by the listener is now associated to the call root span only, and the
SentryHttpClientException
is also associated to the call root span only.What about "Always attach
OkHttp
errors andHttp Client Errors
only to call root span"?