---
title: "Configure Sampling"
description: "Learn how to configure sampling in your app."
url: https://docs.sentry.io/platforms/javascript/guides/express/tracing/configure-sampling/
---

# Configure Sampling | Sentry for Express

Sentry's tracing functionality helps you monitor application performance by capturing distributed traces, attaching attributes, and span performance across your application. However, Capturing traces for every transaction can generate significant volumes of data. Sampling allows you to control the amount of spans that are sent to Sentry from your application.

The JavaScript SDK provides two main options for controlling the sampling rate:

1. [Uniform Sample Rate](https://docs.sentry.io/platforms/javascript/guides/express/tracing/configure-sampling.md#uniform-sample-rate-tracessamplerate) (recommended)
2. [Sampling Function](https://docs.sentry.io/platforms/javascript/guides/express/tracing/configure-sampling.md#sampling-function-tracessampler)

## [Uniform Sample Rate (`tracesSampleRate`)](https://docs.sentry.io/platforms/javascript/guides/express/tracing/configure-sampling.md#uniform-sample-rate-tracessamplerate)

`tracesSampleRate` is floating point value 0.0 and 1.0, which controls the probability that a transaction will be sampled.

```javascript
Sentry.init({
  dsn: "___PUBLIC_DSN___",
  // Capture 25% of all transactions
  tracesSampleRate: 0.25,
});
```

With `tracesSampleRate` set to `0.25`, each transaction in your application is randomly sampled with a probability of 25%, so you can expect that one in every four transactions will be sent to Sentry.

## [Sampling Function (`tracesSampler`)](https://docs.sentry.io/platforms/javascript/guides/express/tracing/configure-sampling.md#sampling-function-tracessampler)

For more granular control, you provide a `tracesSampler` function. This approach allows you to:

* Apply different sampling rates to different types of transactions
* Filter out specific transactions entirely
* Make sampling decisions based on transaction data
* Control the inheritance of sampling decisions in distributed traces

```javascript
Sentry.init({
  dsn: "___PUBLIC_DSN___",
  tracesSampler: (samplingContext) => {
    // Access transaction details from the sampling context
    const { name, attributes, inheritOrSampleWith } = samplingContext;

    // Skip health checks entirely
    if (name.includes("healthcheck")) {
      return 0;
    }

    // Capture all auth-related transactions
    if (name.includes("auth")) {
      return 1;
    }

    // Sample only 1% of comment-related transactions
    if (name.includes("comment")) {
      return 0.01;
    }

    // For everything else, inherit parent sampling decision or use 0.5
    return inheritOrSampleWith(0.5);
  },
});
```

### [The Sampling Context Object](https://docs.sentry.io/platforms/javascript/guides/express/tracing/configure-sampling.md#the-sampling-context-object)

When the `tracesSampler` function is called, it receives a `samplingContext` object with valuable information to help make sampling decisions:

```typescript
interface SamplingContext {
  // Name of the span/transaction
  name: string;

  // Initial attributes of the span/transaction
  attributes: SpanAttributes | undefined;

  // Whether the parent span was sampled (undefined if no incoming trace)
  parentSampled: boolean | undefined;

  // Sample rate from incoming trace (undefined if no incoming trace)
  parentSampleRate: number | undefined;

  // Utility function to inherit parent decision or fallback
  inheritOrSampleWith: (fallbackRate: number) => number;
}
```

### [Traces Sampler Examples](https://docs.sentry.io/platforms/javascript/guides/express/tracing/configure-sampling.md#traces-sampler-examples)

1. Prioritizing Critical User Flows

```javascript
tracesSampler: (samplingContext) => {
  const { name, attributes, inheritOrSampleWith } = samplingContext;

  // Sample all checkout transactions
  if (name.includes("/checkout") || attributes?.flow === "checkout") {
    return 1.0;
  }

  // Sample 50% of login transactions
  if (name.includes("/login") || attributes?.flow === "login") {
    return 0.5;
  }

  // Sample 10% of everything else
  return inheritOrSampleWith(0.1);
};
```

2. Handling Different Environments

```javascript
tracesSampler: (samplingContext) => {
  const { inheritOrSampleWith } = samplingContext;

  // Sample all transactions in development
  if (process.env.NODE_ENV === "development") {
    return 1.0;
  }

  // Sample 5% in production
  if (process.env.NODE_ENV === "production") {
    return 0.05;
  }

  // Sample 20% in staging
  return inheritOrSampleWith(0.2);
};
```

3. Controlling Sampling Based on User or Transaction Properties

```javascript
tracesSampler: (samplingContext) => {
  const { attributes, inheritOrSampleWith } = samplingContext;

  // Always sample for premium users
  if (attributes?.userTier === "premium") {
    return 1.0;
  }

  // Sample more transactions for users experiencing errors
  if (attributes?.hasRecentErrors === true) {
    return 0.8;
  }

  // Sample less for high-volume, low-value paths
  if (attributes?.path?.includes("/api/metrics")) {
    return 0.01;
  }

  // Default sampling rate
  return inheritOrSampleWith(0.2);
};
```

## [Sampling Decision Precedence](https://docs.sentry.io/platforms/javascript/guides/express/tracing/configure-sampling.md#sampling-decision-precedence)

When multiple sampling mechanisms could apply, Sentry follows this order of precedence:

* If `tracesSampler` is defined, its decision is used. Although the `tracesSampler` can override the parent sampling decision, most users will want to ensure their `tracesSampler` respects the parent sampling decision.
* If no `tracesSampler` is defined, but there is a parent sampling decision from an incoming distributed trace, we use the parent sampling decision
* If neither of the above, `tracesSampleRate` is used
* If `tracesSampleRate` is set to 0, no spans will be sampled, and no downstream spans will be sampled either since they inherit the parent sampling decision
* If none of the above are set, no transactions are sampled and tracing will be disabled

## [Conclusion](https://docs.sentry.io/platforms/javascript/guides/express/tracing/configure-sampling.md#conclusion)

Effective sampling is key to getting the most value from Sentry's performance monitoring while minimizing overhead. The `tracesSampler` function gives you precise control over which transactions to record, allowing you to focus on the most important parts of your application.
