Start free trial
Start free trial Log in

14-day free trial. No credit card.

What is a span?

  • Glossary
  • 3 minute read

What is a span in observability?

A span is one unit of work within a trace: an HTTP request, a DB query, a function call. Each span has a name, start and end timestamps, parent/child pointers to other spans, and attributes (the SQL query, HTTP status, controller name). A trace is a tree of spans. In Laravel, OpenTelemetry instrumentation records each DB query, cache call and outgoing HTTP request as its own span; laravel/nightwatch records the same operations as entries on the request's timeline.

Updated

If a trace is the story of a request, spans are the sentences. Each one records a single thing the request did and how long it took, and the timestamps and parent pointers are what let a tool reassemble them into the waterfall you actually read. Most of the time you never create a span by hand. The framework instrumentation does it for the operations that usually matter. The reason to understand them is for the times it doesn't.

Anatomy of a span

A single DB query span
name:         db.query
trace_id:     0af7651916cd43dd8448eb211c80319c
span_id:      b7ad6b7169203331
parent_id:    0f9e8d7c6b5a4392
start_time:   2026-04-13T14:23:05.240Z
end_time:     2026-04-13T14:23:05.260Z
duration_ms:  20

attributes:
  db.system.name:  postgresql
  db.query.text:   SELECT * FROM orders WHERE user_id = ? LIMIT 10
  laravel.connection: pgsql

status:
  code: OK

The span tells you everything you need to reconstruct that one operation's timing, inputs, and outcome, but nothing about the surrounding request. That's what the parent span is for.

Parent-child relationships

Spans form a tree via parent_id. The root span (the HTTP request) has no parent. Every other span points to its parent. The tracing backend reconstructs the tree from these pointers to render the waterfall.

Attributes

Spans carry structured key-value metadata, called attributes. OpenTelemetry defines semantic conventions so every vendor uses the same attribute names for the same concepts:

  • http.request.method, http.response.status_code, http.route for HTTP spans
  • db.system.name, db.query.text, db.operation.name for DB spans
  • messaging.system, messaging.destination.name for queue spans
  • exception.type, exception.message, exception.stacktrace for error spans

Creating custom spans in Laravel

For an expensive internal operation that isn't captured by auto-instrumentation:

use OpenTelemetry\API\Globals;
use OpenTelemetry\API\Trace\StatusCode;

$tracer = Globals::tracerProvider()->getTracer('app');

$span = $tracer->spanBuilder('image.resize')
    ->setAttribute('image.width', $width)
    ->setAttribute('image.height', $height)
    ->startSpan();

$scope = $span->activate();

try {
    $resized = $this->resize($image, $width, $height);
    $span->setStatus(StatusCode::STATUS_OK);
} catch (\Throwable $e) {
    $span->recordException($e);
    $span->setStatus(StatusCode::STATUS_ERROR);
    throw $e;
} finally {
    $scope->detach();
    $span->end();
}

NightOwl doesn't record custom spans. Its request timeline shows the operations the Nightwatch package captures (queries, cache calls, outgoing HTTP, dispatched jobs, mail, notifications), so for a span around your own code, use OpenTelemetry as above.

Span events vs child spans

A span event is a timestamped annotation within a span: not a child span, just a marker. Use events for point-in-time signals (cache.hit, retry.attempted). Use child spans when the thing has its own duration worth tracking. Events are cheaper but less queryable.

Spans aren't free

Once you can make spans, it's tempting to make a lot of them. Resist it. Every span carries its attributes into storage, and at production traffic a span-per-loop-iteration becomes millions of near-identical records that cost money to keep and bury the spans that matter. The heuristic that holds up: a span should correspond to a decision you might make. If seeing it wouldn't change what you do next, it's an attribute on the parent span, not a span of its own.

See the spans Laravel already records

NightOwl's request timeline shows each query, cache call, outgoing HTTP request, dispatched job, mail and notification the Nightwatch package captured, in the order they ran. It doesn't record custom spans, so use OpenTelemetry for a span around your own code.

No credit card needed.

Frequently asked questions

Anything else, write to support@usenightowl.com

What is a span?

A span is one unit of work within a trace: an HTTP request, a database query, a function call, an external API call. Each span has a name, start/end timestamps, parent/child pointers to other spans, and a set of attributes (key-value metadata like the SQL query, HTTP status, controller name). A trace is a tree of spans.

What's the difference between a span and a transaction?

Nothing technical. It's terminology. 'Transaction' is older APM language for a top-level span (the root of a request trace). 'Span' is OpenTelemetry's term for any unit, including the root. New Relic and older Sentry use 'transaction'; modern OTel-aligned tools use 'span' uniformly.

What attributes should I put on a Laravel span?

OpenTelemetry semantic conventions define standard attributes: http.request.method, http.response.status_code, http.route, db.system.name, db.query.text, server.address. Add Laravel-specific context as needed: laravel.controller, user.id (if privacy allows). Keep spans lean, because attributes are stored per span and multiply storage cost.

Should I create custom spans in Laravel?

For expensive internal operations, yes. A slow image-resizing function in a controller won't show up as a span unless you wrap it. Auto-instrumentation catches the framework's common operations (HTTP, DB, cache, queues), and custom spans fill in the pieces that are specific to your code.

What's a span event?

A timestamped annotation within a span: not a child span, but a point-in-time marker. Useful for 'something happened mid-span' signals: cache.miss, queue.job.released, exception.caught. Span events are cheaper than full spans but still attach to the span's attribute storage.

How long should a span be?

Granular enough to isolate bottlenecks, coarse enough to not drown in data. Typical useful granularity: one span per SQL query, one per external HTTP call, one per queued job dispatch, one per view render. Per-line spans are overkill and inflate storage. A good heuristic: if a span wouldn't reveal a useful decision, it probably shouldn't be a span.

Keep reading

Flat pricing, whatever your traffic does

Every plan starts with a 14-day free trial. No credit card needed.

Hobby
$5 a month

One app for a side project. Issues and alerts, with a 14-day dashboard window.

Start my 14-day trial
TeamRecommended
$15 a month

Up to 3 apps and 5 people per team. Full history, the MCP server, data clearing and agent health.

Start my 14-day trial
Agency
$69 a month

Unlimited client apps and people, one overview of every app, and apps you can hand over.

Start my 14-day trial