Track request flows and operations across your application
Tracing allows you to follow a request or operation through your entire application, creating a hierarchical view of nested operations and their timing.
A trace represents a single request or operation flow through your application. It consists of:
Trace ID: Unique identifier for the entire trace
Root Span: The top-level operation (transaction)
Child Spans: Nested operations within the trace
import * as Sentry from '@sentry/browser';// Start a new traceSentry.startSpan({ name: 'process_order', op: 'function' }, (span) => { // This is the root span of a new trace console.log('Trace ID:', span.spanContext().traceId); // Child spans inherit the trace ID Sentry.startSpan({ name: 'validate_order', op: 'function' }, () => { validateOrder(); }); Sentry.startSpan({ name: 'save_order', op: 'db.query' }, () => { saveOrder(); });});
import { withActiveSpan, startInactiveSpan } from '@sentry/browser';const span = startInactiveSpan({ name: 'custom_operation' });withActiveSpan(span, () => { // Inside here, `span` is the active span // Child spans will be children of `span` Sentry.startSpan({ name: 'child_operation' }, () => { // This is a child of `span` });});span.end();
import { withActiveSpan } from '@sentry/browser';withActiveSpan(null, () => { // Spans started here won't have a parent Sentry.startSpan({ name: 'independent_operation' }, () => { // This is a root span });});
Explicitly start a new trace, disconnecting from any parent trace:
import { startNewTrace } from '@sentry/browser';startNewTrace(() => { // This starts a completely new trace // Useful for background jobs or independent operations Sentry.startSpan({ name: 'background_job', op: 'task' }, () => { processBackgroundJob(); });});
Only use startNewTrace when you explicitly want to break the trace chain. In most cases, operations should be part of the ongoing trace.
Sentry.startSpan( { name: 'optional_child', onlyIfParent: true }, (span) => { // This span is only created if there's an active parent span // Otherwise, it's a non-recording span });
Sentry.startSpan( { name: 'forced_transaction', forceTransaction: true }, (span) => { // This creates a new transaction even if there's an active parent });
Sentry.startSpan({ name: 'dynamic_operation' }, (span) => { const result = performOperation(); // Update the span name based on the result span.updateName(`operation_${result.type}`); return result;});