{
    "slug": "distributed_tracing",
    "term": "Distributed Tracing",
    "category": "observability",
    "difficulty": "intermediate",
    "short": "Distributed tracing tracks a request as it flows through multiple services — each service adds a span to a shared trace, giving end-to-end visibility into latency and failures.",
    "long": "A trace represents one request journey. Each service adds a span (operation + timing). Spans link via trace ID + parent span ID propagated in headers (W3C Trace Context: traceparent header). Tools: Jaeger, Zipkin, Datadog APM, AWS X-Ray, Honeycomb. OpenTelemetry: the standard SDK for instrumentation. PHP: opentelemetry-php SDK, Laravel/Symfony integration. Key use: find where in a multi-service request the latency is — which service is slow, which DB query takes 2s. Without tracing, cross-service debugging requires correlating logs by timestamp — fragile and slow.",
    "aliases": [],
    "tags": [
        "observability",
        "tracing",
        "distributed",
        "microservices"
    ],
    "misconception": "Distributed tracing replaces logging — they're complementary. Traces show the request flow and where time is spent; logs show what happened within each service.",
    "why_it_matters": "Distributed tracing is the only way to efficiently debug latency issues in microservices — without it, finding which of 10 services is slow requires educated guessing.",
    "common_mistakes": [
        "Not propagating trace headers between services — breaks the trace chain.",
        "100% sampling in production — high volume, high cost. Use sampling.",
        "Not adding custom attributes to spans — spans without context are hard to use."
    ],
    "when_to_use": [],
    "avoid_when": [],
    "related": [
        "span_trace",
        "opentelemetry",
        "correlation_id",
        "apm_concepts"
    ],
    "prerequisites": [
        "correlation_id"
    ],
    "refs": [
        "https://opentelemetry.io/docs/concepts/signals/traces/"
    ],
    "bad_code": "// No trace propagation — broken traces:\n$response = $httpClient->post('/api/orders', $data);\n// No traceparent header passed — downstream sees a new root trace",
    "good_code": "// Propagate W3C trace context:\n$response = $httpClient->post('/api/orders', $data, [\n    'headers' => [\n        'traceparent' => $tracer->getCurrentSpan()->getContext()->getTraceId(),\n    ]\n]);\n\n// OpenTelemetry PHP:\n$span = $tracer->spanBuilder('processOrder')->startSpan();\n$span->setAttribute('order.id', $orderId);\ntry { processOrder(); }\nfinally { $span->end(); }",
    "quick_fix": "Add OpenTelemetry SDK. Propagate W3C traceparent header in all HTTP/queue calls. Add custom span attributes for key IDs. Sample 5-10% in production.",
    "severity": "high",
    "effort": "high",
    "created": "2026-03-23",
    "updated": "2026-03-23",
    "citation": {
        "canonical_url": "https://codeclaritylab.com/glossary/distributed_tracing",
        "html_url": "https://codeclaritylab.com/glossary/distributed_tracing",
        "json_url": "https://codeclaritylab.com/glossary/distributed_tracing.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": "[Distributed Tracing](https://codeclaritylab.com/glossary/distributed_tracing) (CodeClarityLab)",
                "footer_credit": "Source: CodeClarityLab Glossary — https://codeclaritylab.com/glossary/distributed_tracing"
            }
        }
    }
}