Observability utilities for Smart Platform - correlation IDs, health checks, and request tracing
Project description
smart-observability
Observability utilities for Smart Platform - correlation IDs, health checks, and request tracing
Features
- ✅ Correlation ID Middleware - Automatic request tracing across frontend and backend
- ✅ Health Check Endpoints - Standardized /health and /ready endpoints
- ✅ Context Management - Store correlation IDs in context for the request lifecycle
- ✅ FastAPI Integration - Drop-in middleware and helpers for FastAPI/Starlette apps
- ✅ Header Standardization - Uses
X-Correlation-Idheader (RFC 6648) - ✅ Kubernetes Ready - Compatible with liveness and readiness probes
Installation
uv add smart-observability
Quick Start
Basic Setup
from fastapi import FastAPI
from smart_observability import CorrelationMiddleware
app = FastAPI()
# Add correlation middleware
app.add_middleware(CorrelationMiddleware)
@app.get("/users")
async def get_users():
# Correlation ID is automatically available in context
return {"users": []}
Using Correlation IDs
from smart_observability import get_correlation_id
from loguru import logger
@app.get("/users/{user_id}")
async def get_user(user_id: int):
correlation_id = get_correlation_id()
logger.info(
f"Fetching user {user_id}",
extra={"correlation_id": correlation_id}
)
# Your logic here
return {"id": user_id}
With smart-errors
from fastapi import FastAPI
from smart_observability import CorrelationMiddleware
from smart_errors import setup_error_handlers
app = FastAPI()
# IMPORTANT: Add CorrelationMiddleware BEFORE setting up error handlers
app.add_middleware(CorrelationMiddleware)
# Error handlers will automatically include correlation IDs
setup_error_handlers(app)
API Reference
Middleware
CorrelationMiddleware
FastAPI/Starlette middleware that:
- Extracts correlation ID from
X-Correlation-Idheader - Generates a new UUID v4 if none provided
- Stores the ID in context for request duration
- Adds the ID to response headers
Functions
get_correlation_id() -> str | None
Get the current correlation ID from context.
generate_correlation_id() -> str
Generate a new UUID v4 correlation ID.
Constants
CORRELATION_ID_HEADER = "X-Correlation-Id"
The HTTP header name for correlation IDs.
How It Works
Request Flow
- Frontend sends request with
X-Correlation-Idheader (auto-generated) - CorrelationMiddleware extracts or generates correlation ID
- Context Variable stores ID for the request duration
- Your Code can access ID via
get_correlation_id() - Error Handlers include ID in error responses
- Response includes
X-Correlation-Idheader
Health Check Endpoints
Quick Setup
from fastapi import FastAPI
from smart_observability import (
create_liveness_endpoint,
create_readiness_endpoint,
)
app = FastAPI()
# Simple liveness probe
app.get("/health")(create_liveness_endpoint("1.0.0"))
# Readiness probe with dependency checks
async def check_database():
await db.execute("SELECT 1")
async def check_redis():
await redis.ping()
app.get("/ready")(
create_readiness_endpoint([check_database, check_redis], "1.0.0")
)
Health Check Responses
Liveness (/health):
{
"status": "ok",
"version": "1.0.0",
"timestamp": "2025-12-21T10:00:00Z"
}
Readiness (/ready):
{
"status": "ok",
"checks": [
{
"name": "check_database",
"status": "ok",
"latency_ms": 12.5
},
{
"name": "check_redis",
"status": "ok",
"latency_ms": 5.2
}
],
"version": "1.0.0",
"timestamp": "2025-12-21T10:00:00Z"
}
Kubernetes Integration
apiVersion: v1
kind: Pod
metadata:
name: my-service
spec:
containers:
- name: app
image: my-service:latest
livenessProbe:
httpGet:
path: /health
port: 8000
initialDelaySeconds: 5
periodSeconds: 10
readinessProbe:
httpGet:
path: /ready
port: 8000
initialDelaySeconds: 10
periodSeconds: 5
Custom Health Checks
from smart_observability import (
measure_health_check,
create_readiness_response,
HealthCheck,
CheckStatus,
)
@app.get("/ready")
async def custom_readiness():
# Automatic timing and error handling
db_check = await measure_health_check("database", check_database)
redis_check = await measure_health_check("redis", check_redis)
# Manual checks
api_check = HealthCheck(
name="external-api",
status=CheckStatus.OK if api_available else CheckStatus.FAIL,
latency_ms=api_latency,
message="API reachable" if api_available else "API timeout"
)
return create_readiness_response(
[db_check, redis_check, api_check],
version="1.0.0"
)
Integration with Frontend
The frontend (@smart/data-client) automatically adds correlation IDs:
import { apiFetch } from "@smart/data-client";
// Automatically includes X-Correlation-Id header
const user = await apiFetch<User>("/api/users/123");
License
MIT
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file mehdashti_observability-0.1.1.tar.gz.
File metadata
- Download URL: mehdashti_observability-0.1.1.tar.gz
- Upload date:
- Size: 14.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.9.11 {"installer":{"name":"uv","version":"0.9.11"},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e03ca398931694e7798456678dde0cad0f91ccc22403be602bb20a65d3b1f857
|
|
| MD5 |
a8ede3779a86bb211e037afc250f955c
|
|
| BLAKE2b-256 |
05d4d3a236cc24d7e22adb813d8aa7b70b3bea1a17df25fb6e2b8f8e41ee68ae
|
File details
Details for the file mehdashti_observability-0.1.1-py3-none-any.whl.
File metadata
- Download URL: mehdashti_observability-0.1.1-py3-none-any.whl
- Upload date:
- Size: 7.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.9.11 {"installer":{"name":"uv","version":"0.9.11"},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7b03d23d6bf968ef14fa25358d57c537aac09efdedc1b0a8961dc1d7b88a4724
|
|
| MD5 |
c31db3f4ea471b8fe9f99c3ad36beb89
|
|
| BLAKE2b-256 |
a1964430447825b584354f48be3fa3043ca3aae04cedbe6ca1ae6caf48931d8f
|