OkHttp
The sentry-android-okhttp
library provides OkHttp support for Sentry via the OkHttp Interceptor. The source can be found on GitHub.
On this page, we get you up and running with Sentry's OkHttp Integration, so that it will automatically add a breadcrumb and start a span out of the active span bound to the scope for each HTTP Request.
Install
Sentry captures data by adding an OkHttp Interceptor
. To add the OkHttp integration, initialize the Android SDK, then add the sentry-android-okhttp
dependency. Using Gradle:
implementation 'io.sentry:sentry-android:7.13.0'
implementation 'io.sentry:sentry-android-okhttp:7.13.0'
Configure
Configuration should happen once you create your OkHttpClient
instance.
import okhttp3.OkHttpClient
import io.sentry.android.okhttp.SentryOkHttpInterceptor
private val client = OkHttpClient.Builder()
.addInterceptor(SentryOkHttpInterceptor())
.build()
Verify
This snippet includes a HTTP Request and captures an intentional message, so you can test that everything is working as soon as you set it up:
import io.sentry.android.okhttp.SentryOkHttpInterceptor
import io.sentry.Sentry
import java.io.IOException
import okhttp3.OkHttpClient
import okhttp3.Request
@Throws(IOException::class)
fun run(url: String): String? {
val request = Request.Builder()
.url(url)
.build()
val bodyStr = client
.newCall(request)
.execute()
.body?.toString()
Sentry.captureMessage("The Message $bodyStr")
return bodyStr
}
Learn more about manually capturing an error or message, in our Usage documentation.
To view and resolve the recorded message, log into sentry.io and open your project. Clicking on the error's title will open a page where you can see detailed information and mark it as resolved.
Customize the Recorded Span
The captured span can be customized or dropped with a BeforeSpanCallback
:
import io.sentry.ISpan
import io.sentry.android.okhttp.SentryOkHttpInterceptor
import okhttp3.Request
import okhttp3.Response
class CustomBeforeSpanCallback : SentryOkHttpInterceptor.BeforeSpanCallback {
override fun execute(span: ISpan, request: Request, response: Response?): ISpan? {
return if (request.url.toUri().toString().contains("/admin")) {
null
} else {
span
}
}
}
The callback instance must be set on the SentryOkHttpInterceptor
once you create your OkHttpClient
instance.
import okhttp3.OkHttpClient
import io.sentry.android.okhttp.SentryOkHttpInterceptor
private val client = OkHttpClient.Builder()
.addInterceptor(SentryOkHttpInterceptor(CustomBeforeSpanCallback()))
.build()
- Package:
- maven:io.sentry:sentry-android
- Version:
- 7.13.0
- Repository:
- https://github.com/getsentry/sentry-java