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.

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:

Copied
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:

Copied
import io.sentry.android.gradle.InstrumentationFeature

sentry {
  tracingInstrumentation {
    enabled = true
    features = EnumSet.allOf(InstrumentationFeature) - InstrumentationFeature.OKHTTP
  }
}

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:

Copied
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.

Copied
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:

Copied
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
}

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:

Copied
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.

Copied
import okhttp3.OkHttpClient
import io.sentry.android.okhttp.SentryOkHttpInterceptor

private val client = OkHttpClient.Builder()
  .addInterceptor(SentryOkHttpInterceptor(CustomBeforeSpanCallback()))
  .build()
Help improve this content
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").