scaffold-observability
Configure ArchiPy 5.x OpenTelemetry traces, metrics, logs, and instrumentation. Use for "add tracing/metrics/OTel", "Prometheus/Pushgateway", or migrating off Sentry, Elastic APM, or old Prometheus.
- License 许可
- MIT
- 最近更新
- 2026-11-07
- Stars
- ★ 3
- Forks
- 1
01 前言 Front Matter
- name
- scaffold-observability
- description
- Configure ArchiPy 5.x OpenTelemetry traces, metrics, logs, and instrumentation. Use for "add tracing/metrics/OTel", "Prometheus/Pushgateway", or migrating off Sentry, Elastic APM, or old Prometheus.
03 SKILL.md 正文
Scaffold ArchiPy OpenTelemetry
Read first
Read these plugin rules in full before generating files (paths relative to this skill directory; Claude Code: ${CLAUDE_SKILL_DIR}/../../rules/):
../../rules/config-and-di.mdc
Before writing files
- Inspect
pyproject.toml/uv.lock,AppConfig, bootstrap order, app transports, adapters, and.env.example. - Infer required instrumentation extras from the installed stack.
- Ask only for unresolved choices: enabled signals, metrics exporter (
otlp/pull/pushgateway), OTLP endpoint/protocol or Pushgateway URL, service name, sampling ratio, and log level. - Preserve existing config/bootstrap. Never write collector credentials or OTLP headers with secret values.
Install only matching extras
| Stack | Extra |
|---|---|
| Core OTLP traces, metrics, logs | otel |
| FastAPI | otel + fastapi (no separate extra) |
| gRPC | otel-grpc |
| SQLAlchemy | otel-sqlalchemy |
| Redis | otel-redis |
| Elasticsearch | otel-elasticsearch |
| Kafka | otel-kafka |
| ScyllaDB | otel-scylladb |
| MinIO/botocore | otel-minio |
Combine required extras, for example:
uv add "archipy[otel,fastapi,otel-sqlalchemy,otel-redis]"
Do not use removed ArchiPy 4.x extras: prometheus, sentry, or elastic-apm.
Configure
Add non-secret defaults/documentation to .env.example:
OTEL__IS_ENABLED=true
OTEL__SERVICE_NAME=my-service
OTEL__OTLP_ENDPOINT=http://localhost:4317
OTEL__PROTOCOL=grpc
OTEL__TRACES_ENABLED=true
OTEL__METRICS_ENABLED=true
OTEL__METRICS_EXPORTER=otlp
# Pull scrape: OTEL__METRICS_EXPORTER=pull
# Pushgateway: OTEL__METRICS_EXPORTER=pushgateway
# OTEL__METRICS_PUSHGATEWAY_URL=http://pushgateway.monitoring:9091
OTEL__METRICS_PULL_HOST=0.0.0.0
OTEL__METRICS_PULL_PORT=8200
OTEL__SYSTEM_METRICS_ENABLED=true
OTEL__LOGS_ENABLED=true
OTEL__LOGS_EXPORTER=console
OTEL__TRACES_SAMPLE_RATIO=0.1
OTEL__LOGS_LEVEL=WARNING
OTEL__FASTAPI_EXCLUDED_URLS=health,docs,redoc,openapi.json
Use BaseConfig.OTEL; ArchiPy builds providers programmatically. Do not rely on OpenTelemetry SDK OTEL_*
autoconfiguration. For http/protobuf, use port 4318; ArchiPy appends /v1/{signal} when the base endpoint has no path.
Set OTEL__METRICS_EXPORTER=pull for scrape-only metrics. Set OTEL__METRICS_EXPORTER=pushgateway
and OTEL__METRICS_PUSHGATEWAY_URL (job defaults to SERVICE_NAME) for Prometheus Pushgateway —
no local /metrics server. otlp pushes OTLP to a collector; do not confuse it with Pushgateway.
If pull scrape cannot bind, ArchiPy logs a warning and keeps traces/logs/metrics providers.
Set OTEL__LOGS_EXPORTER=otlp to push logs to a collector (default console writes INFO/DEBUG to
stdout and WARNING+ to stderr).
Initialize before adapters
After BaseConfig.set_global(config) and before constructing the DI container/adapters:
from archipy.configs.base_config import BaseConfig
from archipy.helpers.utils.otel_utils import OtelUtils
config = AppConfig()
BaseConfig.set_global(config)
OtelUtils.init_otel_if_needed(config)
container = ApplicationContainer()
Initialization is idempotent. AppUtils calls it again safely. Early initialization matters for SQLAlchemy engines, Kafka clients, ScyllaDB sessions, and logs created during bootstrap.
Instrument application code
- FastAPI:
AppUtils.create_fastapi_app(config)witharchipy[otel,fastapi]. - gRPC server:
create_grpc_app/create_async_grpc_appwitharchipy[otel-grpc].- Traces: contrib server interceptor when
OTEL.TRACES_ENABLED. - Metrics: ArchiPy
GrpcServerOtelMetricsInterceptor/AsyncGrpcServerOtelMetricsInterceptorrecordsrpc.server.call.durationwhenOTEL.METRICS_ENABLED(works without traces).
- Traces: contrib server interceptor when
- Tuning (archipy >= 5.16.0):
OTEL.TRACES_SAMPLER,PROPAGATORS(b3,b3multi; B3 ships inarchipy[otel]),OTLP_COMPRESSION(noneby default),SPAN_BATCH_*,RESOURCE_DETECTORS;USE_STANDARD_ENVmakes the standardOTEL_*variables fill unset settings. - gRPC clients:
OtelUtils.grpc_client_interceptors()/async_grpc_client_interceptors(). - Traces:
trace_root,trace_span,async_trace_root,async_trace_span,trace_class. - Metrics:
measure_duration,async_measure_duration,count_calls,async_count_calls. - Exceptions:
BaseUtils.capture_exceptionrecords on the current span.
Import decorators from archipy.helpers.decorators. measure_duration, count_calls, trace_span and trace_root detect async def; the async_* twins remain.
Only capture non-sensitive argument names in trace attributes.
ArchiPy 4.x migration
TracingUtils→OtelUtilscapture_transaction/capture_span→trace_root/trace_span(+ async twins)- Prometheus/Sentry/Elastic APM config →
BaseConfig.OTEL - Removed FastAPI rate-limit handler →
fastapi-redis-sdk
Do not
- Never commit OTLP credentials; no secrets in source, logs, or committed env files.
Verify
- Run the repository formatter/linter and import the bootstrap without starting long-lived servers.
- Confirm OTel initializes before adapter construction and AppUtils receives the same global/injected config.
- Test with in-memory exporters or
OtelUtils.configure_for_testing; do not require a production collector. - Confirm disabled signals no-op and no credentials appear in source, logs, or committed env files.
- Run the
archipy-reviewersubagent on the changes and fix every Must fix finding it reports. - Report extras, config keys, bootstrap changes, and commands run.
Docs
04 同仓库更多 Skills
scaffold-utils
Add or wire pure helpers/utils, preferring ArchiPy utils (datetime, JWT, password, file, string). Use for "add a helper/util function", "format dates", "hash passwords", or "generate tokens".
scaffold-service
Scaffold a thin FastAPI router or gRPC servicer under services/{domain}/v{n}/ with AppUtils bootstrap. Use for "add an endpoint/route/API", "expose via REST/gRPC", or "new API version".
scaffold-models
Scaffold ArchiPy models under models/: domain/repository DTOs, errors, optional entities and types. Use for "add DTO/schema/request/response model", "new error type", or "SQLAlchemy entity".
scaffold-logic
Scaffold an ArchiPy logic (use case) under logics/{domain}/ with a unit-of-work decorator and DTO I/O. Use for "add business logic", "new use case", "transactional operation", or "service layer method".
scaffold-interceptor
Add or wire cross-cutting FastAPI/gRPC interceptors or middleware, preferring ArchiPy's. Use for "add middleware", "request ID/logging/auth interceptor", or "gRPC interceptor".
scaffold-health-checks
Scaffold FastAPI/gRPC liveness, readiness, and startup health checks, graceful shutdown, and optional Kubernetes probes. Use for "health endpoint", "readiness/liveness probe", or "k8s probes".