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

Manual spans

View as Markdown

The browser client automatically times page loads, navigations, and HTTP requests. It can't time your own code for you, for example a slow client side calculation. For that, wrap the code in a manual span.

A manual span automatically becomes a child of whatever span is currently active, for example the current page load. You don't need to pass a parent yourself.

withSpan

withSpan wraps a function, times it, and ends the span for you when the function returns:

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

function generateInvoiceReport(rows) {
    return flare.withSpan('generate-invoice-report', () => {
        return rows
            .filter((row) => row.status === 'paid')
            .map((row) => calculateRowTotals(row))
            .sort((a, b) => b.total - a.total);
    });
}

If the function returns a promise, withSpan waits for it and ends the span once the promise settles. If the function throws or its promise rejects, the span is marked as an error before it ends. Either way, you never call end() yourself.

startSpan and span.end()

Use startSpan when the work you're timing doesn't fit inside a single function call, for example when it spans multiple event handlers. Call end() on the span yourself once the work is done:

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

function generateInvoiceReport(rows) {
    const span = flare.startSpan('generate-invoice-report');

    const result = rows
        .filter((row) => row.status === 'paid')
        .map((row) => calculateRowTotals(row))
        .sort((a, b) => b.total - a.total);

    span.end();

    return result;
}

Unlike withSpan, startSpan does not end the span or set an error status for you. If the code between startSpan and end() throws, make sure end() still runs, for example in a finally block.

If tracing is off

startSpan and withSpan work even when enableTracing is false. They still create a span, and your code still runs, so you don't need to manually check whether tracing is on before calling startSpan or withSpan. The span just isn't recorded or sent to Flare.

Web vitals Sampling

On this page

  • withSpan
  • startSpan and span.end()
  • If tracing is off

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