Spans & Traces
TL;DR
A trace is one request's full journey; spans are the individual operations within it — each span has a name, start time, duration, status, and optional attributes.
Explanation
Trace: collection of spans sharing a trace ID. Root span: first span in a request. Child spans: nested operations (DB query, HTTP call, cache lookup). Span attributes: key-value data (db.statement, http.url, user.id). Span events: timestamped events within a span (cache.miss). Span status: OK, ERROR, UNSET. Span kind: SERVER, CLIENT, PRODUCER, CONSUMER, INTERNAL. W3C Trace Context: traceparent header carries (version, trace-id, parent-id, flags). Sampling: recording every span is expensive — sample 1-10% in production or use head/tail-based sampling.
Common Misconception
✗ Spans must represent function calls — a span can represent any operation: a DB query, an HTTP call, a business step ('checkout'), or an external API call.
Why It Matters
Understanding span granularity determines how useful traces are — too coarse misses bottlenecks; too fine creates noise and cost.
Common Mistakes
- One span per request — no internal visibility.
- Span per line of code — noise and cost.
- Missing DB spans — usually the most important.
- Not setting span status=ERROR on exception — traces look successful.
Code Examples
✗ Vulnerable
// Single span for entire request — no internal visibility:
$span = $tracer->spanBuilder('handleRequest')->startSpan();
try { handleRequest(); } finally { $span->end(); }
✓ Fixed
// Nested spans:
$requestSpan = $tracer->spanBuilder('POST /orders')->setSpanKind(SpanKind::SERVER)->startSpan();
$dbSpan = $tracer->spanBuilder('SELECT orders')->setSpanKind(SpanKind::CLIENT)->startSpan();
$dbSpan->setAttribute('db.statement', 'SELECT * FROM orders WHERE id = ?');
try { $result = $pdo->query(...); }
catch (Exception $e) { $dbSpan->setStatus(StatusCode::ERROR, $e->getMessage()); throw $e; }
finally { $dbSpan->end(); }
Tags
🤝 Adopt this term
£79/year · your link shown here
Added
23 Mar 2026
Views
25
🤖 AI Guestbook educational data only
|
|
Last 30 days
Agents 0
No pings yet today
No pings yesterday
Amazonbot 8
Perplexity 8
Google 2
Ahrefs 2
ChatGPT 1
Unknown AI 1
Also referenced
How they use it
crawler 20
crawler_json 1
pre-tracking 1
Related categories
⚡
DEV INTEL
Tools & Severity
🔵 Info
⚙ Fix effort: Medium
⚡ Quick Fix
Add spans for: DB queries, HTTP calls, cache operations, and key business steps. Set ERROR status on exceptions. Add relevant attributes (IDs, URLs, query). Sample in production.
📦 Applies To
web
cli
queue-worker
🔗 Prerequisites
🔍 Detection Hints
spanBuilder|startSpan
Auto-detectable:
✗ No
opentelemetry
⚠ Related Problems
🤖 AI Agent
Confidence: Low
False Positives: High
✗ Manual fix
Fix: Medium
Context: Function