Distributed tracing with Jaeger

Distributed tracing with Jaeger

Follow the thread. Learn how to use distributed tracing to visualize the entire journey of a request through your AI microservices, identifying exactly where latency is hiding.

Distributed Tracing: Mapping the Microservice Labyrinth

In the previous lessons, we've looked at Metrics (numbers) and Logs (text). But what if your user reports: "The AI agent took 12 seconds to answer"?

  • Your Metrics show that CPU is fine.
  • Your Logs show that the backend started at 10:00:00 and finished at 10:00:12.

But where were those 12 seconds spent? Was it the database query? Was it the model inference in AWS? Was it an authentication check in a different namespace? Or was it just a slow network hop?

Logs and metrics cannot answer this. To see the "Inside" of a multi-step request, you need Distributed Tracing. In this lesson, we will master Jaeger and OpenTelemetry. We will learn how to instrument your FastAPI code to generate Spans and how to use the Jaeger UI to find the "Bottlenecks" in your AI pipeline.


1. The Anatomy of a Trace

Distributed tracing breaks a request down into a parent-child hierarchy.

  • Trace: The entire journey of a single request from the moment the user clicks "Submit" until they see the result.
  • Span: A single "Step" or "Unit of work" within that journey (e.g., "SQL Query", "Model Call", "Json Parsing").
  • Context Propagation: The "Secret Sauce" that allows different services to know they belong to the same trace. The Parent service passes a "Trace ID" header to the Child service.

2. Introducing Jaeger

Jaeger is an open-source, distributed tracing system. It was originally built by Uber and is now a core part of the Cloud Native Computing Foundation (CNCF).

Components of Jaeger:

  1. Agent: Listens for spans sent by your applications.
  2. Collector: Aggregates spans and stores them in a database (like Cassandra or Elasticsearch).
  3. Query Service & UI: Where you search and visualize your traces.

3. Visualizing a Trace Flow

In an AI request for a personalized summary:

gantt
    title AI Request Trace (12s Total)
    dateFormat  X
    axisFormat %s
    
    section Frontend
    Receive Request      :0, 12s
    
    section Auth
    Validate Token       :1, 2s
    
    section AI Backend
    Fetch User Profile   :2, 4s
    Vector DB Search     :4, 7s
    LLM Inference (AWS)  :7, 11s
    JSON Formatting      :11, 12s

By looking at this Gantt chart, you can clearly see that the LLM Inference is the longest part, but the Vector DB Search is also taking a surprisingly long time. This gives you a clear target for optimization.


4. How to Instrument Your Code: OpenTelemetry

Gone are the days of using proprietary libraries. The industry has standardized on OpenTelemetry (OTel).

Example: Instrumenting a FastAPI Application

from opentelemetry import trace
from opentelemetry.instrumentation.fastapi import FastAPIInstrumentor
from fastapi import FastAPI

app = FastAPI()

# This one line automatically starts tracing every API request
FastAPIInstrumentor.instrument_app(app)

tracer = trace.get_tracer(__name__)

@app.get("/summarize")
async def summarize():
    # Create a 'Custom Span' for a specific block of code
    with tracer.start_as_current_span("ai_logic"):
        # Your LangChain / Model logic here
        return {"result": "ok"}

5. Tracing Across Namespaces

In Kubernetes, your tracing data must jump across network boundaries.

The B3/W3C Headers:

When your UI Pod calls your API Pod, it must include a header like traceparent. If you use a Service Mesh (like Istio), this happens automatically. If you don't, your OpenTelemetry library will handle it for you, as long as you use the provided HTTP clients (like httpx or requests) that are instrumented for OTel.


6. Practical Example: Installing Jaeger

For a development or small production cluster, the Jaeger Operator is recommended.

kubectl apply -f https://github.com/jaegertracing/jaeger-operator/releases/download/v1.48.0/jaeger-operator.yaml

Once installed, you create a jaeger resource, and the operator will set up all the deployments and services for you. You can then access the UI via a standard Ingress (Module 5.3).


7. AI Implementation: Debugging "Chain of Thought"

When using LangGraph or Agentic Workflows, an AI agent might make 20 different decisions in a loop.

The Trace Benefit:

Instead of drowning in 5,000 log lines from the agents, you can look at one Jaeger Trace.

  • You will see 20 "Loops" as children of the main request.
  • You can click on any loop to see the specific Tool it called and the latency of that tool.
  • You can quickly identify if one specific "Tool" (like a web search) is dragging down the entire user experience.

8. Summary and Key Takeaways

  • Distributed Tracing: Visualizes the path and latency of requests across microservices.
  • Trace vs Span: A Trace is the story; a Span is a chapter.
  • OpenTelemetry: The vendor-neutral standard for instrumentation.
  • Context Propagation: Passing the Trace ID via HTTP headers.
  • Performance: Identifying the "Longest Pole in the Tent" for latency optimization.

Congratulations!

You have completed the Observability Module. You can now:

  1. See the real-time health with Metrics Server.
  2. Build long-term dashboards with Prometheus.
  3. Search every log line with Loki.
  4. Map user journeys with Jaeger.

You are officially a "Cluster Detective."

Next Stop: In Module 10: Security in Kubernetes, we will move beyond networking and focus on RBAC, Service Accounts, and Pod Security Standards.


9. SEO Metadata & Keywords

Focus Keywords: Kubernetes distributed tracing Jaeger tutorial, OpenTelemetry K8s FastAPI instrumentation, tracing microservices latency Jaeger, K8s trace parent context propagation, visualize LangChain request flow Jaeger, debugging distributed systems K8s.

Meta Description: Master distributed tracing in Kubernetes. Learn how to use Jaeger and OpenTelemetry to map the entire journey of a request across your microservices, helping you identify and eliminate latency in your AI and web applications.

Subscribe to our newsletter

Get the latest posts delivered right to your inbox.

Subscribe on LinkedIn