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.

To initialize Sentry in your Vue application, add the following code snippet to your main.js:

main.js
Copied
import {createApp} from 'vue';
import {createRouter} from 'vue-router';
import * as Sentry from '@sentry/vue';

const app = createApp({
  // ...
});
const router = createRouter({
  // ...
});

Sentry.init({
  app,
  dsn: 'https://examplePublicKey@o0.ingest.sentry.io/0',
  integrations: [Sentry.browserTracingIntegration({router}), Sentry.replayIntegration()],

  // Set tracesSampleRate to 1.0 to capture 100%
  // of transactions for performance monitoring.
  // We recommend adjusting this value in production
  tracesSampleRate: 1.0,

  // Set `tracePropagationTargets` to control for which URLs distributed tracing should be enabled
  tracePropagationTargets: ['localhost', /^https:\/\/yourserver\.io\/api/],

  // Capture Replay for 10% of all sessions,
  // plus for 100% of sessions with an error
  replaysSessionSampleRate: 0.1,
  replaysOnErrorSampleRate: 1.0,
});

app.use(router);
app.mount('#app');

main.js
Copied
import Vue from 'vue';
import Router from 'vue-router';
import * as Sentry from '@sentry/vue';

Vue.use(Router);

const router = new Router({
  // ...
});

Sentry.init({
  Vue,
  dsn: 'https://examplePublicKey@o0.ingest.sentry.io/0',
  integrations: [Sentry.browserTracingIntegration({router}), Sentry.replayIntegration()],

  // Set tracesSampleRate to 1.0 to capture 100%
  // of transactions for performance monitoring.
  // We recommend adjusting this value in production
  tracesSampleRate: 1.0,

  // Set `tracePropagationTargets` to control for which URLs distributed tracing should be enabled
  tracePropagationTargets: ['localhost', /^https:\/\/yourserver\.io\/api/],

  // Capture Replay for 10% of all sessions,
  // plus for 100% of sessions with an error
  replaysSessionSampleRate: 0.1,
  replaysOnErrorSampleRate: 1.0,
});

// ...

new Vue({
  router,
  render: h => h(App),
}).$mount('#app');

If you're creating more than one Vue 3 app within your application, check out how to initialize the SDK for multiple apps.

The SDK accepts a few Vue-specific Sentry.init configuration options:

  • attachProps (defaults to true) - Includes all Vue components' props with the events.
  • logErrors (defaults to true) - Decides whether SDK should call Vue's original logError function as well.
  • trackComponents (defaults to false) - Track your app's components. Learn more about component tracking and all its options.

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").