Flare by Spatie
    • Error Tracking
    • Performance Monitoring
    • Logs
  • Pricing
  • Docs
  • Insights
  • Changelog
  • Back to Flare ⌘↵ Shortcut: Command or Control Enter
  • Sign in
  • Try Flare for free
  • Error Tracking
  • Performance Monitoring
  • Logs
  • Pricing
  • Docs
  • Insights
  • Changelog
    • Back to Flare ⌘↵ Shortcut: Command or Control Enter
    • Try Flare for free
    • Sign in
Flare Flare PHP PHP JavaScript JavaScript Protocol Protocol
JavaScript
  • JavaScript
  • React
  • Vue
  • Svelte
  • Inertia
  • React Native
  • Electron
  • Getting Started
  • Introduction
  • Quick start
  • CDN installation
  • Errors
  • Reporting errors
  • Client hooks
  • Sourcemaps
  • Tracing
  • How tracing works
  • What gets traced
  • Web vitals
  • Manual spans
  • Sampling
  • Profiling
  • How component profiling works
  • Logs
  • Introduction
  • Levels
  • Attributes
  • Data Collection
  • Adding custom context
  • Adding glows
  • Identifying users
  • Reference
  • API
  • Configuration

Sampling

View as Markdown

Sending a trace for every single page load can be more data than you need. Sampling lets you control the amount of traces that are sent. The SDK is still recording full traces, it's just not sending every single one to Flare. We recommend values between 0.1 and 0.2 to avoid exhausting your trace limit too quickly.

There are two ways to control sampling. Only one of them runs at a time: if you set tracesSampler, it takes over completely and tracesSampleRate is ignored.

tracesSampleRate

A number between 0 and 1. It defaults to 1, meaning every trace is sent. A value outside that range is clamped to that range.

import { flare } from '@flareapp/js';

flare.configure({
    enableTracing: true,
    tracesSampleRate: 0.2,
});

0.2 sends roughly 1 in 5 traces. Each trace is sampled independently, using a random number, so you can't predict which ones get through, only roughly how many.

tracesSampler

A function that decides sampling itself, instead of using a fixed rate. Set it and it takes precedence over tracesSampleRate:

import { flare } from '@flareapp/js';

flare.configure({
    enableTracing: true,
    tracesSampler: (ctx) => {
        if (ctx.spanType === 'browser_pageload') {
            return 1;
        }

        return 0.1;
    },
});

The example above always samples a full page load, but only 1 in 10 navigations after that.

Your function is called with one object describing the span that is about to start a trace:

  • name: the span's name.
  • attributes: the attributes set on it so far.
  • spanType: its span type, for example 'browser_pageload', if it has one.

Return a number between 0 and 1 to sample by chance, the same way tracesSampleRate does. Return true or false instead to force a definite decision for that trace, with no randomness involved. If your function throws, the trace is not sampled.

The decision happens once, per trace

A trace has one root span: the first span in it with no parent. tracesSampler or tracesSampleRate runs exactly once, when that root span is created. Every span added to the trace after that reuses the same decision.

This means a trace is never half sampled. Either every span in it is recorded and sent, or none of them are.

Manual spans How component profiling works

On this page

  • tracesSampleRate
  • tracesSampler
  • The decision happens once, per trace

Catch errors and fix slowdowns with Flare, the full-stack application monitoring platform for Laravel, PHP & JavaScript.

  • Platform
  • Error Tracking
  • Performance Monitoring
  • Pricing
  • Support
  • Resources
  • Insights
  • Newsletter
  • Changelog
  • Documentation
  • Affiliate program
  • uptime status badge Service status
  • Terms of use
  • DPA
  • Privacy & cookie Policy
Made in by Spatie logo
Flare