bugsnag/bugsnag-js

How to capture OkHttp network requests on react-native?

efstathiosntonas opened this issue · 2 comments

Hi, I'm using react-native@0.72.6 and @bugsnag/react-native@7.21.0. According to the docs we can capture the OkHttp requests so I've patched OkHttpClientProvider.java (link) like this:

/*
 * Copyright (c) Meta Platforms, Inc. and affiliates.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 */

package com.facebook.react.modules.network;

import android.content.Context;
import androidx.annotation.Nullable;
import java.io.File;
import java.util.concurrent.TimeUnit;
import okhttp3.Cache;
import okhttp3.OkHttpClient;
import com.bugsnag.android.okhttp.BugsnagOkHttpPlugin;

/**
 * Helper class that provides the same OkHttpClient instance that will be used for all networking
 * requests.
 */
public class OkHttpClientProvider {

  // Centralized OkHttpClient for all networking requests.
  private static @Nullable OkHttpClient sClient;

  // User-provided OkHttpClient factory
  private static @Nullable OkHttpClientFactory sFactory;

  public static void setOkHttpClientFactory(OkHttpClientFactory factory) {
    sFactory = factory;
  }

  public static OkHttpClient getOkHttpClient() {
    if (sClient == null) {
      sClient = createClient();
    }
    return sClient;
  }

  public static OkHttpClient createClient() {
    if (sFactory != null) {
      return sFactory.createNewNetworkModuleClient();
    }
    return createClientBuilder().build();
  }

  public static OkHttpClient createClient(Context context) {
    if (sFactory != null) {
      return sFactory.createNewNetworkModuleClient();
    }
    return createClientBuilder(context).build();
  }

  public static OkHttpClient.Builder createClientBuilder() {
    // No timeouts by default
    OkHttpClient.Builder client =
        new OkHttpClient.Builder()
            .connectTimeout(0, TimeUnit.MILLISECONDS)
            .eventListener(bugsnagOkHttpPlugin)
            .readTimeout(0, TimeUnit.MILLISECONDS)
            .writeTimeout(0, TimeUnit.MILLISECONDS)
            .cookieJar(new ReactCookieJarContainer());

    return client;
  }

  public static OkHttpClient.Builder createClientBuilder(Context context) {
    int cacheSize = 10 * 1024 * 1024; // 10 Mo
    return createClientBuilder(context, cacheSize);
  }

  public static OkHttpClient.Builder createClientBuilder(Context context, int cacheSize) {
    OkHttpClient.Builder client = createClientBuilder();

    if (cacheSize == 0) {
      return client;
    }

    File cacheDirectory = new File(context.getCacheDir(), "http-cache");
    Cache cache = new Cache(cacheDirectory, cacheSize);

    return client.cache(cache);
  }
}

Issue is, I do not see anything being logged in the breadcrumbs. Can someone give a hint what is going wrong?

clr182 commented

Hi @efstathiosntonas

Our documentation here should outline how to configure the Android layer of your react native application to capture OkHttp network requests. After following our documentation, if you are still having issues configuring OkHttp network request captures please let us know

Hi @efstathiosntonas

As there hasn't been any activity here for a while, we are now closing this issue.

If you continue experiencing problems, then please feel free to reopen this issue and share any additional relevant information. Or you can open a ticket with us directly by contacting support@bugsnag.com