Menu
Grafana Cloud

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

Set a sampling rate

Sets the sampling rate to 80% of sessions.

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
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;
    },
  };
});