Custom Instrumentation for Requests Module

Learn how to manually instrument your code to use Sentry's Requests module.

As a prerequisite to setting up Requests, you’ll need to first set up performance monitoring. Once this is done, the JavaScript SDK will automatically instrument outgoing HTTP requests. If that doesn't fit your use case, you can set up using custom instrumentation.

For detailed information about which data can be set, see the Requests Module developer specifications.

You should init the SDK in the deviceReady function, to make sure the native integrations runs.

Copied
onDeviceReady: function() {
  var Sentry = cordova.require("sentry-cordova.Sentry");
  Sentry.init({ dsn: 'https://examplePublicKey@o0.ingest.sentry.io/0' });
}

You will also need to allow Sentry's domain in your config.xml:

config.xml
Copied
<access origin="sentry.io" />

If you want to run the debug information (also known as debug symbols) upload when building your app, you can use the scripts provided in Debug Information.

If you are experiencing errors and want to remove unused architectures from your binary when submitting the build to iTunes Connect, you can use the scripts provided in Troubleshooting.

NOTE: Refer to HTTP Span Data Conventions for a full list of the span data attributes.

Here is an example of an instrumented function that makes HTTP requests:

Copied
async function makeRequest(method, url) {
  return await Sentry.startSpan(
    {op: 'http.client', name: `${method} ${url}`},
    async span => {
      const parsedURL = new URL(url, location.origin);

      const response = await fetch(url, {
        method,
      });

      span?.setAttribute('http.request.method', method);

      span?.setAttribute('server.address', parsedURL.hostname);
      span?.setAttribute('server.port', parsedURL.port || undefined);

      span?.setAttribute('http.response.status_code', response.status);
      span?.setAttribute(
        'http.response_content_length',
        Number(response.headers.get('content-length'))
      );

      // A good place to set other span attributes

      return response;
    }
  );
}
Help improve this content
Our documentation is open source and available on GitHub. Your contributions are welcome, whether fixing a typo (drat!) or suggesting an update ("yeah, this would be better").