Lab 12: Observability Platform

Time: 60 minutes | Level: Architect | Docker: docker run -it --rm python:3.11-slim bash

Overview

Production systems need observability: distributed tracing (OpenTelemetry), metrics (Prometheus), and structured logging (structlog). This lab builds a complete observability stack for Python services.

Prerequisites

pip install opentelemetry-sdk prometheus-client structlog

Step 1: OpenTelemetry Tracing — TracerProvider Setup

from opentelemetry import trace
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import (
    SimpleSpanProcessor,
    ConsoleSpanExporter,
)
from opentelemetry.sdk.trace.export.in_memory_span_exporter import InMemorySpanExporter

# Setup: use in-memory exporter for testing
exporter = InMemorySpanExporter()
provider = TracerProvider()
provider.add_span_processor(SimpleSpanProcessor(exporter))
trace.set_tracer_provider(provider)

tracer = trace.get_tracer("myapp.tracer", "1.0.0")

# Create spans
with tracer.start_as_current_span("http.request") as span:
    span.set_attribute("http.method", "GET")
    span.set_attribute("http.url", "/api/users")
    span.set_attribute("http.host", "api.example.com")
    
    with tracer.start_as_current_span("db.query") as child:
        child.set_attribute("db.system", "postgresql")
        child.set_attribute("db.statement", "SELECT * FROM users WHERE active=true")
        child.set_attribute("db.row_count", 42)

# Inspect captured spans
spans = exporter.get_finished_spans()
print(f"Spans captured: {len(spans)}")
for s in spans:
    print(f"  Span: {s.name}")
    print(f"    Attributes: {dict(s.attributes)}")
    print(f"    Status: {s.status.status_code}")
    parent = s.parent
    print(f"    Parent: {parent.span_id if parent else 'root'}")

📸 Verified Output:

💡 Spans are finished when the with block exits. The exporter receives them in LIFO order (innermost first). Use InMemorySpanExporter for unit tests.

Step 2: Span Kinds and Status

Step 3: Baggage — Cross-Service Context

Step 4: Prometheus Metrics — Counter, Histogram, Gauge

Step 5: structlog — Structured JSON Logging

Step 6: Integrated Observability Middleware

Step 7: Health Check Endpoint Pattern

Step 8: Capstone — Complete Observability Stack

📸 Verified Output (OpenTelemetry):

Summary

Concept
Library/API
Use Case

Distributed tracing

opentelemetry-sdk

Request flow across services

Span attributes

span.set_attribute

Contextual metadata

Span status

StatusCode.OK/ERROR

Error tracking

Baggage

opentelemetry.baggage

Cross-service context

Request counter

prometheus_client.Counter

Request rate, error rate

Latency histogram

Histogram + buckets

P50/P95/P99 latency

Active gauge

Gauge.set

Current resource usage

Structured logging

structlog.JSONRenderer

Machine-parseable logs

Health checks

Custom checker registry

Service readiness/liveness

Last updated