OkHttp network request monitoring

If you use the OkHttp library for network requests in your app, you can install the bugsnag-plugin-android-okhttp package to intercept network requests. These requests can then be used to:

  • Add breadcrumbs so that each request is shown in error reports to help diagnose what events led to the error;
  • Report requests with error status codes as events to help identify network issues affecting your users.

The breadcrumbs and events contain detailed metadata about each request, including the URL, HTTP method, response status code and duration:

Android Network Breadcrumb screenshot

Installation

Add the bugsnag-plugin-android-okhttp dependency to your Module Gradle Settings, usually found at <project_dir>/app/build.gradle or build.gradle.kts:

dependencies {
    implementation "com.bugsnag:bugsnag-android:6.+"
    implementation "com.bugsnag:bugsnag-plugin-android-okhttp:6.+"
    implementation "com.squareup.okhttp3:okhttp:4.+"
}
dependencies {
    implementation("com.bugsnag:bugsnag-android:6.+")
    implementation("com.bugsnag:bugsnag-plugin-android-okhttp:6.+")
    implementation("com.squareup.okhttp3:okhttp:4.+")
}

BugSnag supports both OkHttp 3 and 4.

Basic configuration

Use the BugsnagOkHttp class to create a configured interceptor for your OKHttp clients:

BugsnagOkHttp instrumentation = new BugsnagOkHttp()
    .logBreadcrumbs()
    .addHttpErrorCodes(400, 599);
OkHttpClient okHttpClient = new OkHttpClient.Builder()
    .addInterceptor(instrumentation.createInterceptor())
    .build();
val instrumentation = BugsnagOkHttp()
    .logBreadcrumbs()
    .addHttpErrorCodes(400, 599)
OkHttpClient.Builder()
    .addInterceptor(instrumentation.createInterceptor())
    .build()

By default, the interceptor will log all requests as breadcrumbs, including failed requests (such as connectivity errors), but will only report errors if status codes are configured.

Controlling captured data

The plugin captures request and response header data including URLs, headers and status codes.

Headers and query string parameters are captured as maps of key-value pairs and will be redacted according to the redactedKeys configuration option.

We recommend adding Cookie and Authorization to the redactedKeys list to avoid capturing sensitive authentication data.

It is also possible to capture the request and response bodies for text-based payloads, although this is not enabled by default to avoid potential size and confidentiality issues. See the Advanced configuration section below for details.

The plugin also exposes a callback for the request and response, so that you can modify the data captured before it is sent. The provided request and response objects contain the data that will be reported as well as the okhttp Request and Response objects for further inspection. These callbacks can be used to redact further sensitive information from the payload or transform the URL for grouping purposes. They also allow per-request control of whether breadcrumbs or error events are sent by setting the isBreadcrumbReported and isErrorReported flags:

new BugsnagOkHttp()
    .addRequestCallback(new HttpRequestCallback() {
        @Override
        public void onRequest(HttpRequest request) {
            if (request.reportedUrl.startsWith("http://example.domain.com")) {
                request.isBreadcrumbReported = true;
                request.reportedUrl = "http://example.domain.com/REDACTED";
            }
        }
    })
    .addResponseCallback(new HttpResponseCallback() {
        @Override
        public void onResponse(HttpResponse response) {
            if (response.reportedUrl.startsWith("http://example.domain.com") &&
                response.response.code == 404) {
                response.isErrorReported = false;
            }
        }
    });
BugsnagOkHttp()
    .addRequestCallback { request ->
        if (request.reportedUrl.startsWith("http://example.domain.com")) {
            request.isBreadcrumbReported = true
            request.reportedUrl = "http://example.domain.com/REDACTED"
        }
    }
    .addResponseCallback { response ->
        if (response.reportedUrl.startsWith("http://example.domain.com") &&
            response.response.code == 404) {
            response.isErrorReported = false
        }
    }

The response object also contains an errorCallback field for fine-tuning the reported event, if there is one, with an OnErrorCallback. In particular this allows you to add custom metadata or modify the severity of the event.

Advanced configuration

The BugsnagOkHttp class provides several methods to customize its behavior:

Logging breadcrumbs

To log requests as breadcrumbs, call logBreadcrumbs (or logBreadcrumbs(true)):

new BugsnagOkHttp()
    .logBreadcrumbs();
BugsnagOkHttp()
    .logBreadcrumbs()

Reporting failed requests

To report failed requests as error events, call addHttpErrorCodes with one or more HTTP status codes or ranges, or individually via addHttpErrorCode:

new BugsnagOkHttp()
    .addHttpErrorCodes(400, 599);
BugsnagOkHttp()
    .addHttpErrorCodes(400, 599)

Capturing request and response bodies

To capture request and response bodies, call setCaptureRequestBody and setCaptureResponseBody with a maximum size in bytes:

 new BugsnagOkHttp()
    .setCaptureRequestBody(1024)
    .setCaptureResponseBody(1024);
BugsnagOkHttp()
    .setCaptureRequestBody(1024)
    .setCaptureResponseBody(1024)

Bodies larger than this size will be truncated.