Skip to main content

Metrics Endpoint

Cognisafe’s FastAPI backend (api/) exposes a /metrics endpoint in Prometheus text format using prometheus-fastapi-instrumentator. Installation (already included in the official Docker image — this is for self-builders):
Wiring in api/main.py:
The safety worker additionally exports Redis queue depth and processing duration metrics via a lightweight prometheus_client exposition loop on port 9091.

Key Metrics

All Cognisafe metrics use the cognisafe_ namespace.

cognisafe_requests_total

Type: Counter
Labels: project_id, model, score_label
Incremented once per LLM request logged by the proxy. score_label is pass, fail, or unscored (the scorer did not run).

cognisafe_request_latency_seconds

Type: Histogram
Labels: project_id, model
Buckets: 0.05, 0.1, 0.25, 0.5, 1.0, 2.5, 5.0, 10.0
End-to-end proxy latency — from first byte received to last byte forwarded to the upstream LLM. Does not include async safety scoring time.

cognisafe_safety_score

Type: Gauge
Labels: project_id, scorer, severity
Last-known score value per scorer per project per severity band. Useful for spotting persistent elevated risk.

cognisafe_flagged_requests_total

Type: Counter
Labels: project_id, owasp_category
Incremented each time a scorer produces score_label=fail. One request can produce multiple increments if multiple scorers flag it.

cognisafe_worker_queue_depth

Type: Gauge
Labels: (none)
Current length of the safety_score_jobs Redis list. Exported by the safety worker on port 9091. A sustained value above 100 indicates the worker fleet is under-provisioned.

cognisafe_worker_processing_seconds

Type: Histogram
Labels: scorer
Buckets: 0.1, 0.5, 1.0, 2.5, 5.0, 10.0, 30.0
Time taken to run a single scorer evaluation, including LLM API call latency inside the scorer.

Prometheus Scrape Configuration

Standalone Prometheus

Kubernetes ServiceMonitor (kube-prometheus-stack)

Apply after installing the cognisafe Helm chart with serviceMonitor.enabled: true:
The Helm chart creates the Service with the http-metrics and worker-metrics named ports automatically when serviceMonitor.enabled: true.

Grafana Dashboard

Set up a dashboard with these six panels. Import the JSON at the end of this section, or build manually.

Panel 1 — Request Rate (req/s, 5 min avg)

Type: Stat + Time series combo

Panel 2 — Flag Rate % (flagged / total)

Type: Gauge with colour thresholds
Thresholds: 0–1% green, 1–5% amber, >5% red.

Panel 3 — OWASP Category Breakdown (last 24h)

Type: Bar chart

Panel 4 — Safety Worker Queue Depth

Type: Time series with alert annotation
Draw a horizontal alert threshold line at 100.

Panel 5 — Proxy Latency P50 / P95 / P99

Type: Time series

Panel 6 — Top 5 Flagged Agents (Table)

Type: Table
Add a Last Seen column by joining against cognisafe_safety_score max timestamp. In Grafana, add a second query with cognisafe_safety_score and use a Transform → Merge to combine.

Dashboard JSON

Abbreviated but fully functional — import via Grafana → Dashboards → Import → Paste JSON.

AlertManager Rules


Grafana Alerting: PagerDuty + Slack

Configure contact points in Grafana → Alerting → Contact points:
Notification policy:

Grafana Cloud: Remote Write

If you use Grafana Cloud instead of self-hosted Grafana, push metrics via Prometheus remote_write — no inbound scrape required:
Set write_relabel_configs to keep only cognisafe_.* metrics to avoid ingesting the full Kubernetes metrics corpus into Grafana Cloud, which reduces your monthly active series bill significantly.