/claude-kit:observability-and-loggingfrom Claude Kit SDLC
Structured logging (structlog), PII/secret redaction, OpenTelemetry tracing, Sentry, Prometheus RED metrics, and health probes for FastAPI backends. Use when adding observability or redacting PII from logs.
Ships in Claude Kit SDLC. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
Read when SKILL.md points to them.
Production observability patterns for FastAPI backends: structured logging, distributed tracing, error tracking, metrics, and health endpoints.
Processor pipeline: Configure structlog.configure() with processors: merge_contextvars → filter_by_level → add_logger_name → add_log_level → TimeStamper(fmt="iso") → StackInfoRenderer() → format_exc_info → UnicodeDecoder() → ProcessorFormatter.wrap_for_formatter. (reference service pattern)
JSON vs. console rendering: Use structlog.dev.ConsoleRenderer() when DEBUG=true, otherwise structlog.processors.JSONRenderer() for machine-parseable logs in production. (reference service pattern)
Propagate to stdlib loggers: Clear handlers on uvicorn, uvicorn.access, sqlalchemy.engine and set propagate = True so they flow through the structlog pipeline. (reference service pattern)
Log level from settings: Read settings.LOG_LEVEL (default INFO), convert to stdlib constant with getattr(logging, level.upper(), logging.INFO). (reference service pattern)
Structured logging makes it easy to log a whole request body, user object, or DB URL — which is exactly how PII and secrets leak into log aggregators. Redact in the pipeline, not at each call site, so it can't be forgotten.
Redaction processor in the pipeline: Add a redact_processor to structlog.configure(processors=[...])
before the renderer. It walks the event dict and (a) masks values whose key is sensitive and
(b) pattern-masks PII inside string values. Because it runs in the pipeline, every log call is covered.
(reference service pattern)
Sensitive-key denylist: Mask any key matching password, passwd, secret, token,
authorization, api_key/apikey, set-cookie/cookie, private_key, client_secret,
x-user-data, ssn, card/card_number, cvv. Replace the value with "***" (don't drop the key —
keep the shape for debugging). (reference service pattern)
Pattern masking inside strings: Mask email local parts, long digit runs (cards/phones), and bearer
tokens in free-text message values via regex (e.g. email → j***@example.com, 13–19 digit runs →
****). Apply to the rendered event message and any string field. (reference service pattern)
Mask DB URLs / connection strings: Never log a DSN with credentials; rewrite
postgresql://user:pass@host/db → postgresql://user:***@host/db before logging connector config or
errors. (reference service pattern)
Audit-event field allowlist: For audit/access logs, allowlist the fields you emit
(user_id, user_role, tenant_id, action, resource_id, result) rather than dumping the user
object. Prefer opaque identifiers over user_email / user_name; if you must include a name/email,
mask it. (reference service pattern)
Truncate large payloads: Cap logged request/response bodies (e.g. 2 KB) — full bodies both leak data and blow up log volume. (reference service pattern)
CustomRequestRoute pattern: Subclass APIRoute, override get_route_handler(), wrap the original handler to log every request with method, path, status_code, duration (via time.perf_counter()), and catch validation/HTTP/unhandled exceptions. (reference service pattern)
Validation error normalization: Extract [{"field": e["loc"][-1], "msg": e["msg"]}] from Pydantic errors, log as validation_error, return 400 with structured ResponseData.error(). (reference service pattern)
Unhandled exception logging: Log as unhandled_exception with logger.exception(...) to include traceback, return 500 with masked error message. (reference service pattern)
Usage: Set router = APIRouter(route_class=CustomRequestRoute) on routers that need centralized logging. (reference service pattern)
This section is the app-side wiring (SDK init, FastAPI/asyncpg/Redis/Kafka auto-instrumentation, the
OTEL_ENABLEDswitch). For the language-neutral tracing pipeline and backend — the OTLP/Collector topology, head-plus-tail sampling, the Grafana Tempo backend, TraceQL, and trace↔logs↔metrics correlation — see the siblingotel-tracingskill.
Initialize SDK first: Call initialize_telemetry() in the app startup hook; create TracerProvider(resource=Resource({SERVICE_NAME: ...})), add BatchSpanProcessor(OTLPSpanExporter(endpoint=...)), set global provider with trace.set_tracer_provider(). (reference service pattern)
Auto-instrumentation: After SDK init, call FastAPIInstrumentor.instrument_app(app), AsyncPGInstrumentor().instrument(), RedisInstrumentor().instrument(). Kafka consumers: KafkaInstrumentor().instrument(). (production services pattern)
Graceful degradation: Wrap all OTEL imports in try/except ImportError and log warnings when packages are missing; app must still start if OTEL libraries are absent. (reference service pattern)
Shutdown hook: On app shutdown, call trace.get_tracer_provider().shutdown() to flush pending spans. (reference service pattern)
OTEL_ENABLED master switch: Check settings.OTEL_ENABLED before any OTEL calls; no-op if disabled. (reference service pattern)
TraceIDMiddleware: ASGI middleware that extracts trace_id and span_id from the current span (via get_current_span().get_span_context()), formats them as 32-char hex (trace) and 16-char hex (span), and injects X-Trace-ID / X-Span-ID headers into the response. (production service pattern)
Usage: Add to app with app.add_middleware(TraceIDMiddleware) after OTEL instrumentation so spans are already active. (production service pattern)
Request ID from Kafka messages: For consumers, extract request_id from message.get("meta", {}).get("request_details", {}).get("request_id", "") and set as a span attribute. (production service pattern)
Initialization: Call sentry_sdk.init(integrations=[LoggingIntegration(event_level=logging.ERROR), SqlalchemyIntegration()]) in app startup. (production service pattern)
DSN from environment: Read Sentry DSN from SENTRY_DSN env var (or settings); only init if present. (production service pattern)
Integration list: Include LoggingIntegration (sends ERROR+ logs as breadcrumbs), SqlalchemyIntegration() (DB query tracking), FastApiIntegration() if available. (production service pattern)
Custom registry: Create _metrics_registry = CollectorRegistry() and register all metrics to it (not the default registry) for isolation. (production service pattern)
Histogram buckets: Define CUSTOM_HISTOGRAM_BUCKETS = [0.05, 0.1, 0.2, 0.4, 0.8, 1, 2, 5, 7, 10, float("inf")] optimized for API latency. (production service pattern)
HTTP request received metric: Histogram("app_http_request_received", "Duration of HTTP requests received", ["method", "path", "status_code", "service_name"], buckets=..., registry=_metrics_registry). (production service pattern)
Path normalization to reduce cardinality: Replace UUIDs with {uuid} (regex [0-9a-f]{8}-...), numeric IDs with {id} (/\d+), collapse consecutive slashes, strip trailing slashes. (production service pattern)
PrometheusMiddleware: ASGI middleware that wraps call_next, measures duration, calls record_http_request_received(method, path, status_code, duration) which invokes HTTP_REQUEST_RECEIVED.labels(...).observe(duration). (production service pattern)
Excluded paths: Skip metrics for {"/health", "/_healthz", "/_readyz", "/metrics", "/docs", "/openapi.json"} to avoid noise. (production service pattern)
Error tracking: If call_next raises, record status 500 before re-raising. (production service pattern)
Background task pattern: Start an async task on app startup with asyncio.create_task(start_metrics_exporter()) and cancel on shutdown. (production service pattern)
Periodic file write: Every METRICS_EXPORT_INTERVAL seconds (default 30), call generate_latest(registry=get_metrics_registry()), write to /var/data/metrics/{POD_NAME}.prom (or /tmp/metrics/{POD_NAME}.prom for local dev). (production service pattern)
Overwrite not append: Open file in w mode each time to avoid unbounded growth. (production service pattern)
File size warning: Log warning if metrics file exceeds 1MB. (production service pattern)
Why file export: Allows Prometheus to scrape metrics via node exporter textfile collector pattern; decouples scrape from app HTTP endpoint. (production service pattern)
Kafka consumer: KAFKA_MESSAGE_PROCESSED = Histogram("app_consumer_kafka_message_processed", ..., ["topic", "partition", "status", "service_name"]). (production service pattern)
Background workers: WORKER_TASK_EXECUTED = Histogram("app_worker_worker_task_executed", ..., ["task_name", "status", "service_name"]). (production service pattern)
Cron jobs: CRON_JOB_EXECUTED = Histogram("app_cron_cron_job_executed", ..., ["job_name", "status", "service_name"]). (production service pattern)
Temporal workflows: TEMPORAL_WORKFLOW_EXECUTED = Histogram("app_worker_temporal_workflow_executed", ..., ["workflow_name", "status", "service_name"]). (production service pattern)
Recording pattern: Call helper record_<metric_type>(name, status, duration) which does .labels(...).observe(duration). (production service pattern)
HTTP_REQUEST_SENT metric: Histogram("app_http_request_sent", "Duration of HTTP requests sent to external services", ["method", "path", "status_code", "service_name"], buckets=..., registry=_metrics_registry). (production service pattern)
Decorator pattern: Use @prometheus_http_outbound(method_key="method", url_key="raw_url") on async HTTP client methods; extracts method/URL from data dict, measures duration, records metric with status from returned dict's status_code or status field. (production service pattern)
Path normalization from URL: Extract path component via urlparse(url).path, collapse slashes, strip trailing slash (except root), ignore query params and hostname to reduce cardinality. (production service pattern)
Service name per mode: Build service identifier as f"{SERVICE_NAME}-{DEPLOYMENT_NAME}-{DEPLOYMENT_TYPE}" from env vars; each deployment mode (server, consumer, worker, cron, temporal_worker) gets a distinct label. (production service pattern)
Metrics HTTP server for non-FastAPI modes: Call start_http_server(port, registry=get_metrics_registry()) to expose /metrics endpoint on dedicated ports (8001 for consumer, 8002 for worker, 8003 for cron, 8004 for temporal_worker). FastAPI mode (port 8000) uses PrometheusMiddleware instead. (production service pattern)
Execute mode wrapper: Wrap service entrypoints with execute_mode(mode_main_func) to auto-start/stop the metrics exporter task for clean lifecycle management. (production service pattern)
Liveness probe (/_healthz): Unconditional 200 response with {"success": True} or ResponseData.ok(message="OK"). No dependency checks. (reference service pattern)
Readiness probe (/_readyz): Check DB with session.execute(text("SELECT 1")).scalar() and Redis with redis.ping(); return 200 if all pass, 503 with {"database": "error: ...", "redis": "ok"} if any fail. (reference service pattern)
Skip auth and RBAC: Tag health endpoints with tags=["health"] or tags=["skip_rbac"] so they bypass authentication middleware. (reference service pattern)
Minimal allocation: Keep probes lightweight; avoid heavy queries or external API calls. (reference service pattern)
# config/logging.py (reference service pattern)
import logging
import structlog
from config.settings import settings
def setup_logging() -> None:
structlog.configure(
processors=[
structlog.contextvars.merge_contextvars,
structlog.stdlib.filter_by_level,
structlog.stdlib.add_logger_name,
structlog.stdlib.add_log_level,
structlog.processors.TimeStamper(fmt="iso"),
structlog.processors.StackInfoRenderer(),
structlog.processors.format_exc_info,
structlog.processors.UnicodeDecoder(),
structlog.stdlib.ProcessorFormatter.wrap_for_formatter,
],
logger_factory=structlog.stdlib.LoggerFactory(),
wrapper_class=structlog.stdlib.BoundLogger,
cache_logger_on_first_use=True,
)
log_level = getattr(logging, settings.LOG_LEVEL.upper(), logging.INFO)
formatter = structlog.stdlib.ProcessorFormatter(
processor=structlog.dev.ConsoleRenderer() if settings.DEBUG else structlog.processors.JSONRenderer(),
)
handler = logging.StreamHandler()
handler.setFormatter(formatter)
root_logger = logging.getLogger()
root_logger.handlers.clear()
root_logger.addHandler(handler)
root_logger.setLevel(log_level)
# Propagate uvicorn/sqlalchemy logs through structlog
for name in ("uvicorn", "uvicorn.access", "sqlalchemy.engine"):
logging.getLogger(name).handlers.clear()
logging.getLogger(name).propagate = True
# config/redaction.py (reference service pattern) — add to the processors list BEFORE the renderer
import re
SENSITIVE_KEYS = {
"password", "passwd", "secret", "token", "access_token", "refresh_token", "session",
"authorization", "api_key", "apikey", "cookie", "set-cookie", "private_key",
"client_secret", "x-user-data", "ssn", "card", "card_number", "cvv",
}
_EMAIL = re.compile(r"([A-Za-z0-9._%+-])[A-Za-z0-9._%+-]*(@[A-Za-z0-9.-]+)")
_DIGITS = re.compile(r"\b\d{13,19}\b") # card-like runs
_BEARER = re.compile(r"(?i)\bBearer\s+[A-Za-z0-9._-]+")
_DSN_CREDS = re.compile(r"(\w+://[^:/\s]*:)([^@/\s]+)(@)") # creds in a DSN (incl. empty username)
def _mask_str(value: str) -> str:
value = _EMAIL.sub(r"\1***\2", value)
value = _DIGITS.sub("****", value)
value = _BEARER.sub("Bearer ***", value)
value = _DSN_CREDS.sub(r"\1***\3", value)
return value
def _redact(value): # recurse dicts + lists; mask by key and by content
if isinstance(value, dict):
return {k: ("***" if k.lower() in SENSITIVE_KEYS else _redact(v)) for k, v in value.items()}
if isinstance(value, list):
return [_redact(v) for v in value]
if isinstance(value, str):
return _mask_str(value)
return value
def redact_processor(_logger, _method, event_dict):
return _redact(event_dict) # full-tree: nested objects & arrays are covered too
# In setup_logging(): structlog.configure(processors=[..., redact_processor, <renderer>])
# Audit log: allowlist fields, never dump the whole user object
def audit_log(action: str, *, user_id: str, tenant_id: str, resource_id: str, result: str) -> None:
logger.info("audit", action=action, user_id=user_id, tenant_id=tenant_id,
resource_id=resource_id, result=result) # no email/name/token
# app/telemetry.py (reference service pattern)
import structlog
from opentelemetry import trace
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import BatchSpanProcessor
from opentelemetry.sdk.resources import Resource, SERVICE_NAME
from opentelemetry.exporter.otlp.proto.grpc.trace_exporter import OTLPSpanExporter
logger = structlog.get_logger(__name__)
def initialize_telemetry() -> None:
if not settings.OTEL_ENABLED:
logger.info("telemetry.disabled")
return
try:
resource = Resource.create({SERVICE_NAME: settings.OTEL_SERVICE_NAME})
provider = TracerProvider(resource=resource)
exporter = OTLPSpanExporter(endpoint=settings.OTEL_EXPORTER_ENDPOINT)
provider.add_span_processor(BatchSpanProcessor(exporter))
trace.set_tracer_provider(provider)
logger.info("telemetry.initialized", service=settings.OTEL_SERVICE_NAME)
except ImportError:
logger.warning("telemetry.import_error", msg="OTEL packages not installed")
def instrument_app(app):
if not settings.OTEL_ENABLED:
return
try:
from opentelemetry.instrumentation.fastapi import FastAPIInstrumentor
FastAPIInstrumentor.instrument_app(app)
logger.info("telemetry.fastapi_instrumented")
except ImportError:
logger.warning("telemetry.fastapi_import_error")
# app/routing.py (reference service pattern)
from fastapi.routing import APIRoute
from fastapi import Request, Response
import time
class CustomRequestRoute(APIRoute):
def get_route_handler(self) -> Callable:
original_route_handler = super().get_route_handler()
async def custom_route_handler(request: Request) -> Response:
start_time = time.perf_counter()
try:
response = await original_route_handler(request)
duration = time.perf_counter() - start_time
logger.info(
"request_handled",
method=request.method,
path=request.url.path,
status_code=response.status_code,
duration=round(duration, 4),
)
return response
except RequestValidationError as exc:
logger.warning("validation_error", path=request.url.path, errors=exc.errors())
# ... return 400
except HTTPException as exc:
logger.warning("http_exception", path=request.url.path, status=exc.status_code)
# ... return exc.status_code
except Exception as exc:
logger.exception("unhandled_exception", path=request.url.path)
# ... return 500
return custom_route_handler
# middleware/logging.py (production service pattern)
from opentelemetry.trace import get_current_span
class TraceIDMiddleware:
def __init__(self, app):
self.app = app
async def __call__(self, scope, receive, send):
if scope["type"] == "http":
span = get_current_span()
if span and span.is_recording():
scope["trace_id"] = format(span.get_span_context().trace_id, "032x")
scope["span_id"] = format(span.get_span_context().span_id, "016x")
async def send_wrapper(message):
if message["type"] == "http.response.start" and "trace_id" in scope:
headers = list(message.get("headers", []))
headers.append([b"x-trace-id", scope["trace_id"].encode()])
headers.append([b"x-span-id", scope["span_id"].encode()])
message["headers"] = headers
await send(message)
await self.app(scope, receive, send_wrapper)
# middleware/prometheus.py (production service pattern)
import time
import re
from starlette.middleware.base import BaseHTTPMiddleware
UUID_PATTERN = r"[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}"
NUMERIC_ID_PATTERN = r"/\d+(?=/|$)"
EXCLUDED_PATHS = {"/health", "/_healthz", "/_readyz", "/metrics", "/docs", "/openapi.json"}
class PrometheusMiddleware(BaseHTTPMiddleware):
async def dispatch(self, request, call_next):
original_path = request.url.path
path = self._normalize_path(original_path)
if original_path in EXCLUDED_PATHS or path in EXCLUDED_PATHS:
return await call_next(request)
start_time = time.time()
try:
response = await call_next(request)
duration = time.time() - start_time
record_http_request_received(request.method, path, response.status_code, duration)
return response
except Exception:
duration = time.time() - start_time
record_http_request_received(request.method, path, 500, duration)
raise
def _normalize_path(self, path: str) -> str:
path = re.sub(UUID_PATTERN, "{uuid}", path, flags=re.IGNORECASE)
path = re.sub(NUMERIC_ID_PATTERN, "/{id}", path)
path = re.sub(r"/+", "/", path)
if path != "/" and path.endswith("/"):
path = path[:-1]
return path
# metrics/constant.py (production service pattern)
from prometheus_client import CollectorRegistry, Histogram
_metrics_registry = CollectorRegistry()
CUSTOM_HISTOGRAM_BUCKETS = [0.05, 0.1, 0.2, 0.4, 0.8, 1, 2, 5, 7, 10, float("inf")]
HTTP_REQUEST_RECEIVED = Histogram(
"app_http_request_received",
"Duration of HTTP requests received",
["method", "path", "status_code", "service_name"],
buckets=CUSTOM_HISTOGRAM_BUCKETS,
registry=_metrics_registry,
)
# metrics/helper.py
def record_http_request_received(method, path, status_code, duration):
HTTP_REQUEST_RECEIVED.labels(
method=method, path=path, status_code=str(status_code), service_name=METRIC_SERVICE_IDENTIFIER
).observe(duration)
# metrics/exporter.py (production service pattern)
import asyncio
from prometheus_client import generate_latest
async def start_metrics_exporter():
while True:
try:
metrics_data = generate_latest(registry=get_metrics_registry()).decode("utf-8")
with open(LOG_FILE, "w") as f:
f.write(metrics_data)
await asyncio.sleep(METRICS_EXPORT_INTERVAL)
except asyncio.CancelledError:
break
# Health endpoints (reference service pattern)
from sqlalchemy import text
async def healthz():
return ResponseData.ok(message="OK")
async def readyz(connection_handler=Depends(get_connection_handler)):
checks = {}
try:
await connection_handler.session.execute(text("SELECT 1"))
checks["database"] = "ok"
except Exception as e:
checks["database"] = f"error: {str(e)}"
try:
pong = await connection_handler.redis.ping()
checks["redis"] = "ok" if pong else "error: no pong"
except Exception as e:
checks["redis"] = f"error: {str(e)}"
if all(v == "ok" for v in checks.values()):
return ResponseData.ok(data=checks, message="All systems operational")
return ORJSONResponse(status_code=503, content=ResponseData.error(message="Service degraded", errors=[checks]).model_dump())
# app router (reference service pattern)
api_router.add_api_route("/_healthz", healthz, methods=["GET"], tags=["health"])
api_router.add_api_route("/_readyz", readyz, methods=["GET"], tags=["health"])
HTTP_REQUEST_RECEIVED and HTTP_REQUEST_SENT histograms with appropriate labels.try/except ImportError.tracer_provider.shutdown() and cancel metrics exporter tasks in app shutdown hooks to avoid losing spans/metrics.CollectorRegistry() to isolate your metrics from library-injected defaults.README.md (opens in a new tab)references/aea-320-modernizing-logging-at-uber-with-clp.md (opens in a new tab)references/health-and-readiness.md (opens in a new tab)references/htn-caching-task-queues-error-handling-logging-monitoring.md (opens in a new tab)references/logging-and-structlog.md (opens in a new tab)references/outbound-metrics-and-multi-mode.md (opens in a new tab)references/pii-redaction.md (opens in a new tab)references/repo-evidence.md (opens in a new tab)references/tracing-and-metrics.md (opens in a new tab)What SKILL.md sets; muted values are Claude Code's defaults
Three short reads, in order
npx claudepluginhub ajyadav013/claude-kit --plugin claude-kit127 skills in this plugin
/accessibility-reviewUI accessibility (WCAG) review — keyboard operability, focus, ARIA semantics, color contrast, touch targets. Returns prioritized fixes. Distinct from performance/visual review./alembic-migrationsAlembic migration setup and patterns for async SQLAlchemy — async engine + run_sync in env.py, autogenerate, RLS/multi-tenant migrations. Use for alembic init, writing migrations, or debugging migration errors./anthropic-vertex-integrationClaude on Vertex AI via the AnthropicVertex SDK — lazy ADC client init, env-based project/region resolution, async helpers, backoff retry. Use when calling Claude via GCP Vertex AI (not direct Anthropic API)./api-and-interface-designStable API and interface design patterns. Use when designing REST/GraphQL endpoints, module boundaries, type contracts between modules or frontend/backend, or changing public interfaces./api-integrationWire the app to a backend API — data fetching, caching, error handling, loading states. Use when connecting to an endpoint, adding a fetch layer, or fixing API error and loading behaviour. React-query detail lives in tanstack-react-query-patterns./api-pagination-filtering-sortingHTTP query parameter conventions for pagination, filtering, sorting, and search with standardized response metadata. Use when designing list endpoints or building repositories that turn query params into SQL.Other plugins' skills for the same job