Lab 12: Observability Platform
Overview
Prerequisites
pip install opentelemetry-sdk prometheus-client structlogStep 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'}")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
structlog — Structured JSON LoggingStep 6: Integrated Observability Middleware
Step 7: Health Check Endpoint Pattern
Step 8: Capstone — Complete Observability Stack
Summary
Concept
Library/API
Use Case
Last updated
