# Set Up Tracing | Sentry for Next.js

With [tracing](https://docs.sentry.io/product/insights/overview.md), Sentry automatically tracks your software performance across your application services, measuring metrics like throughput and latency, and displaying the impact of errors across multiple systems.

If you’re adopting Tracing in a high-throughput environment, we recommend testing prior to deployment to ensure that your service’s performance characteristics maintain expectations.

Sentry can integrate with **OpenTelemetry**. You can find more information about it [here](https://docs.sentry.io/platforms/javascript/guides/nextjs/opentelemetry.md).

## [Configure](https://docs.sentry.io/platforms/javascript/guides/nextjs/tracing.md#configure)

Enable tracing by setting the sample rate for your traces.

Set `tracesSampleRate` in your config files:

* `sentry.server.config.js`
* `sentry.edge.config.js`:
* `instrumentation-client.js`
  * If you previously had a file called `sentry.client.config.(js|ts)`, you can safely rename this to `instrumentation-client.(js|ts)` for all Next.js versions.

`instrumentation-client.(js|ts)`

```javascript
import * as Sentry from "@sentry/nextjs";

Sentry.init({
  dsn: "___PUBLIC_DSN___",
  // We recommend adjusting this value in production, or using `tracesSampler`
  // for finer control
  tracesSampleRate: 1.0,
  // ... rest of your config
});

// This export will instrument router navigations, and is only relevant if you enable tracing.
// `captureRouterTransitionStart` is available from SDK version 9.12.0 onwards
export const onRouterTransitionStart = Sentry.captureRouterTransitionStart;
```

Additional Configuration for App Router on Next.js 14

In order to enable distributed tracing for App Router on Next.js 14 you need to add or modify the `generateMetadata` function of your root layout:

`app/layout.tsx`

```typescript
import * as Sentry from "@sentry/nextjs";
import type { Metadata } from "next";

export function generateMetadata(): Metadata {
  return {
    // ... your existing metadata
    other: {
      ...Sentry.getTraceData(),
    },
  };
}
```

* You can establish a uniform sample rate for all transactions by setting the `tracesSampleRate` option in your SDK config to a number between `0` and `1`. (For example, to send 20% of transactions, set `tracesSampleRate` to `0.2`.)
* For more granular control over sampling, you can set the sample rate based on the transaction itself and the context in which it's captured, by providing a function to the `tracesSampler` config option.

The two options are mutually exclusive. If both are set, `tracesSampler` will take precedence.

You can find more in-depth explanations and examples about sampling configuration in [Configure Sampling](https://docs.sentry.io/platforms/javascript/guides/nextjs/tracing/configure-sampling.md).

## [Distributed Tracing](https://docs.sentry.io/platforms/javascript/guides/nextjs/tracing.md#distributed-tracing)

Sentry captures distributed traces consisting of transactions and spans, which measure individual services and individual operations within those services, respectively. Learn more about our model in [Distributed Tracing](https://docs.sentry.io/product/sentry-basics/tracing/distributed-tracing.md).

## [Verify](https://docs.sentry.io/platforms/javascript/guides/nextjs/tracing.md#verify)

While you're testing, set `tracesSampleRate` to `1.0`, as that ensures that every transaction will be sent to Sentry. Once testing is complete, you may want to set a lower `tracesSampleRate` value, or switch to using `tracesSampler` to selectively sample and filter your transactions, based on contextual data.

If you leave your sample rate at `1.0`, a transaction will be sent every time a user loads a page or navigates within your app. Depending on the amount of traffic your application gets, this may mean a lot of transactions. If you have a high-load, backend application, you may want to consider setting a lower `tracesSampleRate` value, or switching to using `tracesSampler` to selectively sample and filter your transactions, based on contextual data.

## [Automatic Instrumentation](https://docs.sentry.io/platforms/javascript/guides/nextjs/tracing.md#automatic-instrumentation)

See [Automatic Instrumentation](https://docs.sentry.io/platforms/javascript/guides/nextjs/tracing/instrumentation/automatic-instrumentation.md) to learn about all the things that the SDK automatically instruments for you.

## [Custom Instrumentation](https://docs.sentry.io/platforms/javascript/guides/nextjs/tracing.md#custom-instrumentation)

You can also manually start spans to instrument specific parts of your code. This is useful when you want to measure the performance of a specific operation or function.

* [Tracing APIs](https://docs.sentry.io/platforms/javascript/guides/nextjs/apis.md#tracing): Find information about APIs for custom tracing instrumentation
* [Instrumentation](https://docs.sentry.io/platforms/javascript/guides/nextjs/tracing/instrumentation.md): Find information about manual instrumentation with the Sentry SDK
* [Sending Span Metrics](https://docs.sentry.io/platforms/javascript/guides/nextjs/tracing/span-metrics.md): Learn how to capture metrics on your spans

## [Disabling Tracing](https://docs.sentry.io/platforms/javascript/guides/nextjs/tracing.md#disabling-tracing)

If you want to disable tracing, you *should not* set `tracesSampleRate` at all. Setting it to `0` will not disable tracing, it will simply never send any traces to Sentry. Instead, neither `tracesSampleRate` nor `tracesSampler` should be defined in your SDK config to fully disable tracing.

In addition, you can set `__SENTRY_TRACING__` to `false` to ensure the tracing code is removed from your production build. This will result in<!-- --> <!-- -->[trace propagation](https://docs.sentry.io/platforms/javascript/guides/nextjs/tracing/distributed-tracing.md) <!-- -->being disabled as well. See<!-- --> <!-- -->[Tree Shaking](https://docs.sentry.io/platforms/javascript/guides/nextjs/configuration/tree-shaking.md) <!-- -->for more information.

## [Next Steps](https://docs.sentry.io/platforms/javascript/guides/nextjs/tracing.md#next-steps)

* #### [Sending Span Metrics](https://docs.sentry.io/platforms/javascript/guides/nextjs/tracing/span-metrics.md)

  Learn how to add attributes to spans to monitor performance and debug applications

* #### [Set Up Distributed Tracing](https://docs.sentry.io/platforms/javascript/guides/nextjs/tracing/distributed-tracing.md)

  Learn how to connect events across applications/services.

* #### [Configure Sampling](https://docs.sentry.io/platforms/javascript/guides/nextjs/tracing/configure-sampling.md)

  Learn how to configure sampling in your app.

* #### [Instrumentation](https://docs.sentry.io/platforms/javascript/guides/nextjs/tracing/instrumentation.md)

  Learn how to configure spans to capture trace data on any action in your app.

* #### [Troubleshooting](https://docs.sentry.io/platforms/javascript/guides/nextjs/tracing/troubleshooting.md)

  Learn how to troubleshoot your tracing setup.
