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.
The minimum supported version of okhttp
is 3.13.0
due to its incompatibilities with Android versions below 5.0. However, you are free to adapt our SentryOkHttpInterceptor if you're using an older okhttp
version.
Auto-Installation With the Sentry Android Gradle Plugin
Starting from version 3.1.0
, the Sentry Android Gradle plugin will automatically add the sentry-android-okhttp
dependency and instrument all of your OkHttpClient
instances through bytecode manipulation. The plugin will only add the sentry-android-okhttp
dependency if an okhttp
dependency was discovered on the classpath.
Install
Add the Sentry Android Gradle plugin in build.gradle
:
buildscript {
repositories {
mavenCentral()
}
}
plugins {
id "io.sentry.android.gradle" version "3.1.0-beta.1"
}
Then, initialize the Android SDK.
Disable
If you would like to disable the OkHttp instrumentation feature, we expose a configuration option for that:
import io.sentry.android.gradle.InstrumentationFeature
sentry {
tracingInstrumentation {
enabled = true
features = EnumSet.allOf(InstrumentationFeature) - InstrumentationFeature.OKHTTP
}
}
Learn more about the Sentry Android Gradle plugin in our Gradle documentation.
Manual Installation
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:5.7.4'
implementation 'io.sentry:sentry-android-okhttp:5.7.4'
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()
Our documentation is open source and available on GitHub. Your contributions are welcome, whether fixing a typo (drat!) to suggesting an update ("yeah, this would be better").
- Package:
- maven:io.sentry:sentry-android
- Version:
- 5.7.4
- Repository:
- https://github.com/getsentry/sentry-java