---
title: "How to configure sampling in Faro | Grafana Cloud documentation"
description: "Faro's built-in session tracker to create, resume and extend sessions and to provide sampling capabilities based on user sessions."
---

> For a curated documentation index, see [llms.txt](/llms.txt). For the complete documentation index, see [llms-full.txt](/llms-full.txt).

# How to configure sampling in Faro

Faro makes sampling decisions on a per user-session basis. For sampled sessions, Faro samples and transmits every signal. Faro discards signals from un-sampled sessions.

Faro uses the configured sampling rate or a custom sampling function to determine what to sample. The sampling rate and sampling function need to set or return a sampling rate between 0 and 1. Where 0 is no signals sent and 1 is all. By default, Faro uses a sampling rate of 1.

The sampling function computes a sampling decision based on other properties for customer sampling needs. It gets contextual information injected, including multiple Faro Metadata objects.

Faro calculates a sampling decision:

- On page load, when Faro initializes and finds an invalid session in web-storage
- On session extend, when Faro auto-creates a new session or when you manually call `setSession(...)`

> Note
> 
> Faro uses a session-based sampling configured within the session configuration object. Consult the [session tracking documentation](/docs/grafana-cloud/monitor-applications/frontend-observability/instrument/session-tracking/) to understand how Faro tracks and handles sessions and how to configure the session manager.

## How to ensure sampling consistency between frontend and backend

Faro uses a head based sampling approach based on sessions. If a session isn’t part of a sample Faro doesn’t send any data for that specific session, see the preceding section.

To ensure consistency between the sampled frontend and backend data it’s recommended to configure your backends to use `parent-based` sampling.

> Note
> 
> Parent-based sampling is a technique to control sampling in a way that’s consistent across the different components of a distributed system. It ensures that a sampling decision propagates to all subsequent components that are part of the journey of a request.

If you use Faro’s [web-tracing](/docs/grafana-cloud/monitor-applications/frontend-observability/instrument/tracing-instrumentation/) instrumentation, Faro tells the underlying tracing instrumentation the sampling result which sets the W3C sampled flag to inform subsequent instruments to record spans.

If you use OpenTelemtry for your backend instrumentation, you should set a `parent-based` sampler. For more information see OpenTelemetry docs for the [parent-based sampler](https://opentelemetry.io/docs/languages/erlang/sampling/#parentbasedsampler).

## Set a sampling rate

Sets the sampling rate to 80% of sessions.

ts ![Copy code to clipboard](/media/images/icons/icon-copy-small-2.svg) Copy

```ts
initializeFaro({
  ...
  sessionTracking?: {
    samplingRate: 0.8,
  }
```

## Use the sampler function to calculate custom sampling rates

Use a custom sampler function to calculate a tailored sampling decision. In this case different sampling decisions based on the planet name.

> Note
> 
> Defining sampling rates based on metadata or other properties can introduce bias in the sampling decisions. A custom sampler function is commonly used when running experiments in production or when there is an interest on specific types of traffic.

ts ![Copy code to clipboard](/media/images/icons/icon-copy-small-2.svg) Copy

```ts
initializeFaro({
  ...
  sessionTracking?: {
    sampler(context) {
      const planet = context.metas.user?.attributes?.['planet'];

      if (!planet) {
         return 0; // 0%
      }

      if (planet === 'mars') {
         return 0.8; // 80%
      }

      if (planet === 'moon') {
         return 0.3; // 30%
      }

      if (planet === 'earth') {
         return 0.1; // 10%
      }

      return 1;
    },
  };
});
```
