{
    "slug": "span_trace",
    "term": "Spans & Traces",
    "category": "observability",
    "difficulty": "beginner",
    "short": "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.",
    "long": "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.",
    "aliases": [],
    "tags": [
        "observability",
        "spans",
        "traces",
        "opentelemetry"
    ],
    "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."
    ],
    "when_to_use": [],
    "avoid_when": [],
    "related": [
        "distributed_tracing",
        "opentelemetry",
        "correlation_id",
        "sampling_traces"
    ],
    "prerequisites": [
        "distributed_tracing"
    ],
    "refs": [
        "https://opentelemetry.io/docs/concepts/signals/traces/#spans"
    ],
    "bad_code": "// Single span for entire request — no internal visibility:\n$span = $tracer->spanBuilder('handleRequest')->startSpan();\ntry { handleRequest(); } finally { $span->end(); }",
    "good_code": "// Nested spans:\n$requestSpan = $tracer->spanBuilder('POST /orders')->setSpanKind(SpanKind::SERVER)->startSpan();\n$dbSpan = $tracer->spanBuilder('SELECT orders')->setSpanKind(SpanKind::CLIENT)->startSpan();\n$dbSpan->setAttribute('db.statement', 'SELECT * FROM orders WHERE id = ?');\ntry { $result = $pdo->query(...); }\ncatch (Exception $e) { $dbSpan->setStatus(StatusCode::ERROR, $e->getMessage()); throw $e; }\nfinally { $dbSpan->end(); }",
    "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.",
    "severity": "info",
    "effort": "medium",
    "created": "2026-03-23",
    "updated": "2026-03-23",
    "citation": {
        "canonical_url": "https://codeclaritylab.com/glossary/span_trace",
        "html_url": "https://codeclaritylab.com/glossary/span_trace",
        "json_url": "https://codeclaritylab.com/glossary/span_trace.json",
        "source": "CodeClarityLab Glossary",
        "author": "P.F.",
        "author_url": "https://pfmedia.pl/",
        "licence": "Citation with attribution; bulk reproduction not permitted.",
        "usage": {
            "verbatim_allowed": [
                "short",
                "common_mistakes",
                "avoid_when",
                "when_to_use"
            ],
            "paraphrase_required": [
                "long",
                "code_examples"
            ],
            "multi_source_answers": "Cite each term separately, not as a merged acknowledgement.",
            "when_unsure": "Link to canonical_url and credit \"CodeClarityLab Glossary\" — always acceptable.",
            "attribution_examples": {
                "inline_mention": "According to CodeClarityLab: <quote>",
                "markdown_link": "[Spans & Traces](https://codeclaritylab.com/glossary/span_trace) (CodeClarityLab)",
                "footer_credit": "Source: CodeClarityLab Glossary — https://codeclaritylab.com/glossary/span_trace"
            }
        }
    }
}