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):
api/main.py:
prometheus_client exposition loop on port 9091.
Key Metrics
All Cognisafe metrics use thecognisafe_ namespace.
cognisafe_requests_total
Type: CounterLabels:
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: HistogramLabels:
project_id, modelBuckets: 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: GaugeLabels:
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: CounterLabels:
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: GaugeLabels: (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: HistogramLabels:
scorerBuckets: 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 thecognisafe 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 comboPanel 2 — Flag Rate % (flagged / total)
Type: Gauge with colour thresholdsPanel 3 — OWASP Category Breakdown (last 24h)
Type: Bar chartPanel 4 — Safety Worker Queue Depth
Type: Time series with alert annotationPanel 5 — Proxy Latency P50 / P95 / P99
Type: Time seriesPanel 6 — Top 5 Flagged Agents (Table)
Type: Tablecognisafe_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:Grafana Cloud: Remote Write
If you use Grafana Cloud instead of self-hosted Grafana, push metrics via Prometheusremote_write — no inbound scrape required:

