Skip to main content

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-Id header (RFC 6648)
  • Kubernetes Ready - Compatible with liveness and readiness probes

Installation

uv add smart-observability

Quick Start

Basic Setup

from fastapi import FastAPI
from mehdashti_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 mehdashti_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 mehdashti_observability import CorrelationMiddleware
from mehdashti_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:

  1. Extracts correlation ID from X-Correlation-Id header
  2. Generates a new UUID v7 if none provided
  3. Stores the ID in context for request duration
  4. 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 v7 correlation ID.

Constants

CORRELATION_ID_HEADER = "X-Correlation-Id"

The HTTP header name for correlation IDs.

How It Works

Request Flow

  1. Frontend sends request with X-Correlation-Id header (auto-generated)
  2. CorrelationMiddleware extracts or generates correlation ID
  3. Context Variable stores ID for the request duration
  4. Your Code can access ID via get_correlation_id()
  5. Error Handlers include ID in error responses
  6. Response includes X-Correlation-Id header

Health Check Endpoints

Quick Setup

from fastapi import FastAPI
from mehdashti_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 mehdashti_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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mehdashti_observability-0.2.0.tar.gz (14.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

mehdashti_observability-0.2.0-py3-none-any.whl (7.1 kB view details)

Uploaded Python 3

File details

Details for the file mehdashti_observability-0.2.0.tar.gz.

File metadata

  • Download URL: mehdashti_observability-0.2.0.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

Hashes for mehdashti_observability-0.2.0.tar.gz
Algorithm Hash digest
SHA256 0f0de3f003252d2992db5767b27f17c533dbd6b6fdcd91483ec08f514e0e61d1
MD5 c9e35e814e1494671cbd0d632f596420
BLAKE2b-256 e3c24f12190b6763c127238eb0eb82e18102569e0242e9a1bcc1c29ff16f0e44

See more details on using hashes here.

File details

Details for the file mehdashti_observability-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: mehdashti_observability-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 7.1 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

Hashes for mehdashti_observability-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 6b00ca423114110d022a23958ef830e82c461d5c8f6e44f96d46513a75022293
MD5 ef3048665a68048e06e5c8d4981d56a9
BLAKE2b-256 f736a14f0511f9b9fbf1c6ce679c9faccf6181545f92b2a9f26daff5fc4055ae

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page