decision-cascade

Local engine for typed, calibrated decisions for AI agents.

View the Project on GitHub slavabars/decision-cascade

Observability: metrics, request id and logs

English · Русский

The service exposes Prometheus metrics, sets X-Request-ID on every response and can write structured JSON logs. Request bodies (state, images, feedback examples) never reach metrics or logs — this is pinned by tests/test_logging_privacy.py.

1. GET /metrics

Prometheus text format 0.0.4, without authentication — as is customary for sidecar scraping. The endpoint is not counted in its own counters, so the scraper does not inflate decision_http_requests_total.

curl -fsS http://127.0.0.1:8080/metrics | head -20
Metric Type Labels Meaning
decision_http_requests_total counter method, path, status HTTP requests by route template
decision_http_request_duration_seconds histogram method, path request latency
decision_decisions_total counter question, type, trained decisions returned
decision_untrained_refusals_total counter — 409 UNTRAINED_HEADS refusals
decision_ready gauge — 1 — /ready answers 200, 0 — 503
decision_heads_loaded gauge — how many trained packs are in the cache

path is the route template (/decisions, /v1/systemone), not the actual URL; unknown routes map to __unmatched__. method comes from an allowlist (GET, POST, …); any other token maps to OTHER. question is a name from the loaded trained packs, capped at MAX_QUESTION_LABELS (64); everything else maps to __other__, so cardinality does not grow with client input. GET /info → heads_cache reports the LRU cache {size, limit}.

Standard process metrics (python_gc_*, process_resident_memory_bytes, etc.) are added by prometheus_client itself; they are useful when investigating leaks and memory degradation.

Metric names in the alert examples are checked against the real registry by tests/test_docs.py::test_observability_alert_metrics_exist_in_registry, and a live scrape is checked manually (§5): decision_ready, decision_untrained_refusals_total and decision_heads_loaded are returned with the expected values, the histogram with buckets.

Scraping and alerts

# prometheus.yml
scrape_configs:
  - job_name: decision-cascade
    scrape_interval: 15s
    static_configs:
      - targets: ["127.0.0.1:8080"]

Reasonable starting alerts:

groups:
  - name: decision-cascade
    rules:
      - alert: DecisionNotReady
        expr: decision_ready == 0
        for: 5m
        annotations:
          summary: "decision-cascade is not ready to take traffic (/ready 503)"

      - alert: DecisionUntrainedRefusals
        expr: rate(decision_untrained_refusals_total[5m]) > 0
        for: 10m
        annotations:
          summary: "requests return 409: there is no trained pack for the question schema"

      - alert: DecisionLatencyHigh
        expr: |
          histogram_quantile(
            0.95,
            sum by (le, path) (rate(decision_http_request_duration_seconds_bucket[5m]))
          ) > 0.5
        for: 10m
        annotations:
          summary: "p95 latency is above 500 ms (the budget from docs/benchmarks.md)"

2. X-Request-ID

curl -i -H 'X-Request-ID: trace-42' -X POST http://127.0.0.1:8080/decisions \
  -H 'content-type: application/json' \
  -d '{"state":"Stripe failing","questions":{"urgent":{"type":"noul","instructions":"urgent?"}}}'
# X-Request-ID: trace-42

We do not pass arbitrary strings into the header: newlines and spaces are a log injection, so such a value is replaced with a new id.

3. Logs

By default the format is unchanged (Python/uvicorn) and the service reconfigures nothing. Structured logs are enabled by the environment:

Variable Values Default
DECISION_LOG_FORMAT text, json, empty empty (we do not interfere)
DECISION_LOG_LEVEL level name (DEBUG, INFO, WARNING, …) INFO
DECISION_LOG_FORMAT=json DECISION_LOG_LEVEL=INFO \
  .venv/bin/python -m uvicorn api.server.main:app --port 8000

One line per request (decision.access):

{"ts": "2026-10-29T12:00:00+0000", "level": "INFO", "logger": "decision.access",
 "message": "POST /decisions -> 200 12.3ms request_id=trace-42",
 "request_id": "trace-42", "method": "POST", "path": "/decisions",
 "status": 200, "duration_ms": 12.3}

The method/path/status/duration_ms fields are put into the JSON separately, so they can be filtered in Loki/ELK without parsing message. With DECISION_LOG_FORMAT=json the service installs exactly one handler on root; with text/empty it only adds a request_id filter without changing uvicorn and pytest handlers.

4. What is not logged

Decision logs keep the question name, type, distribution and confidence — enough to debug calibration, not enough to reconstruct the request text. Checks: tests/test_logging_privacy.py, tests/test_observability.py.

5. Manual check

# metrics grow after requests
curl -fsS http://127.0.0.1:8080/metrics | grep decision_http_requests_total

# the request id is returned and the client one is preserved
curl -isS -H 'X-Request-ID: trace-42' http://127.0.0.1:8080/health | grep -i x-request-id

# a single-line JSON log
DECISION_LOG_FORMAT=json .venv/bin/python -m uvicorn api.server.main:app --port 8000 \
  && curl -fsS http://127.0.0.1:8080/health

6. GET /v1/stats

Process aggregates without user strings: decisions, abstained/abstain_rate, escalated/escalation_rate, routes (local/fallback/mixed/transferred), feedback (events, questions, coverage), cache (size/limit of the heads LRU), packs (loaded/trained) and calibration (pack questions: examples/ece/gate/retrained; retraining_threshold; unpacked without names). DECISION_STATS_TOKEN enables Authorization: Bearer; without it the endpoint is open like the rest of the local service. Counters reset on restart; state and question names outside packs are not returned. Contract — docs/stability.md §14.

7. OpenTelemetry traces

Metrics answer “how many and how long”; traces answer “where exactly”. Tracing is off by default: without a provider every span is a no-op, so there is no extra exporter or background thread.

# console exporter without extra dependencies
DECISION_OTEL=1 DECISION_OTEL_EXPORTER=console \
  .venv/bin/python -m uvicorn api.server.main:app --port 8000

# OTLP HTTP (requires the extra)
pip install 'decision-cascade[otel]'
DECISION_OTEL=1 DECISION_OTEL_ENDPOINT=http://127.0.0.1:4318 \
  .venv/bin/python -m uvicorn api.server.main:app --port 8000
Variable Meaning Default
DECISION_OTEL enable traces 0
DECISION_OTEL_EXPORTER console or otlp/otlp-http otlp when an endpoint is set
DECISION_OTEL_ENDPOINT OTLP HTTP endpoint; OTEL_EXPORTER_OTLP_ENDPOINT is also read —
DECISION_OTEL_SAMPLE_RATIO trace ratio in [0, 1] 1.0
DECISION_OTEL_SERVICE_NAME resource service.name decision-cascade

The span tree is http.server → decision.decide/decision.decide_many → decision.encode, decision.encode_media, decision.fuse, decision.heads, decision.sample, decision.backend and decision.remote. The root span carries http.route, http.response.status_code, decision.request_id and decision.duration_ms; the remote span carries decision.backend, server.address and http.response.status_code.

Attributes contain only counters, types, backend names and status codes. state, images, question texts and answers are never written — this is pinned by tests/test_tracing.py. decision.request_id matches X-Request-ID, so a trace joins the log and metrics for the same request.