Files
ERP/monitoring/docker-compose.monitoring.yml
T
whqreza 0adadd2cf8 feat(monitoring): bot observability dashboards — Grafana + Persian RTL ERP page
Grafana (full use of the existing monitoring/ stack, per OSS policy):
- datasources.yml: BotPostgres (uid bot-postgres) over the shared network;
  grafana service joins 'shared' + carries the datasource env
- db_init.py: idempotent read-only role obs_reader (USAGE on schema public +
  SELECT on bot_traces/bot_trace_events; skipped when
  OBS_READER_PASSWORD empty; ALTER ROLE for rotation) + composite index
  ix_bot_trace_events_stage_time (stage, created_at DESC) — senior-engineer
  review applied with the column-name fix (event_type -> stage); (trace_id)
  already covered by the existing (trace_id, created_at) index
- dashboards/bot-observability.json: KPI row (real traffic, events, Jev/
  fallback errors, Jev p95, tokens, commits, archive guard), events/day,
  per-stage p50/p95, mention statuses (arbiter+fallback aggregates + legacy
  rows), DeepSeek arbiter actions, tokens/day, model-drift vs jev-1.13.0,
  user-choice A/B, STT + audio.enhance health, archive size guard
- dashboards/bot-trace-explorer.json: trace table + full event drill-down
  via the trace_id variable

ERP page (admin-only, Persian RTL, recharts@^3 for React 19):
- backend bot_metrics.py: read-only require_role('admin') endpoints
  (summary/routing/quality/jev/stt/traces + trace detail); aggregates count
  real traffic only (calibration/demo/chat_id=0 excluded)
- frontend BotMonitorPage at /bot-monitor (KPI cards, daily events,
  status donut, arbiter actions, user-choice A/B, STT/enhance tables,
  stage latency, tokens chart, traces table + detail drawer with payload
  viewer); entry link from the admin panel; MobileTabBar hidden there
- api.ts: typed botMetrics client section

Tooling/docs:
- tools/seed_observability_demo.py: deterministic demo traces (source='demo',
  every current pipeline stage incl. audio.enhance/jev.failure/arbitration/
  fallback/user choices) — excluded from real-traffic aggregates
- monitoring/README.md + bot/docs/DEPLOY-AND-MONITORING.md: setup, seed and
  rollback notes; alerts deliberately deferred (owner decision 2026-10-24)

Verified locally: obs_reader SELECT ok / INSERT denied; panels' SQL run as
obs_reader with data; datasource health OK; both dashboards survive a grafana
restart; endpoints 200 as admin and 403 without role; tsc + vite build clean;
page renders in-browser (admin session); bot startup provisions role+index.
2026-10-24 15:09:01 +03:30

207 lines
7.2 KiB
YAML

# ============================================================================
# Monitoring stack — SEPARATE compose project from the ERP app on purpose:
# app rebuilds (push-to-deploy) must never restart monitoring containers.
#
# cd /srv/erp # repo root, so --env-file picks up the app's .env
# docker compose -f monitoring/docker-compose.monitoring.yml --env-file .env up -d
#
# Included (≈2.2 GB RAM budgeted):
# GlitchTip (errors, Sentry-compatible) + its own small Postgres
# Prometheus (metrics) + Grafana (dashboards) + Loki/Promtail (logs)
# node-exporter (host) + postgres-exporter (app DB) + cAdvisor (containers)
#
# Admin UIs (Grafana :3001, GlitchTip :8001) bind to 127.0.0.1 only —
# reach them via SSH tunnel exactly like the MinIO console. GlitchTip ALSO
# needs a public origin for the browser SDK: set GLITCHTIP_HOST and enable
# the "public" profile (Traefik route + TLS) — see monitoring/README.md.
# ============================================================================
services:
# ---------------------------------------------------------------- errors --
glitchtip:
image: glitchtip/glitchtip:latest # pinned upstream: version tags are dated, keep an eye on updates
container_name: monitoring-glitchtip
mem_limit: 640m
environment:
SECRET_KEY: ${GLITCHTIP_SECRET_KEY:?set GLITCHTIP_SECRET_KEY in .env}
GLITCHTIP_DOMAIN: ${GLITCHTIP_DOMAIN:?set GLITCHTIP_DOMAIN in .env}
DATABASE_URL: postgres://glitchtip:${GLITCHTIP_DB_PASSWORD:-glitchtip}@glitchtip-postgres:5432/glitchtip
# Single-container mode (embedded worker) — no redis needed at this scale.
GLITCHTIP_EMBED_WORKER: "true"
VALKEY_URL: ""
ENABLE_USER_REGISTRATION: "false"
ENABLE_ORGANIZATION_CREATION: "false"
DEFAULT_FROM_EMAIL: ${GLITCHTIP_FROM_EMAIL:-noreply@bennu-api-bahrestan.ir}
EMAIL_URL: ${GLITCHTIP_EMAIL_URL:-consolemail://}
ports:
- "127.0.0.1:8001:8000"
depends_on:
glitchtip-postgres:
condition: service_healthy
networks:
- monitoring
- shared
- traefik # public DSN route (profile "public")
labels:
- traefik.enable=true
- traefik.http.routers.glitchtip.rule=Host(`${GLITCHTIP_HOST:-errors.localhost}`)
- traefik.http.routers.glitchtip.entrypoints=web
- traefik.http.services.glitchtip.loadbalancer.server.port=8000
restart: unless-stopped
glitchtip-postgres:
image: postgres:16-alpine
container_name: monitoring-glitchtip-postgres
mem_limit: 128m
environment:
POSTGRES_DB: glitchtip
POSTGRES_USER: glitchtip
POSTGRES_PASSWORD: ${GLITCHTIP_DB_PASSWORD:-glitchtip}
volumes:
- glitchtip_postgres_data:/var/lib/postgresql/data
healthcheck:
test: ["CMD-SHELL", "pg_isready -U glitchtip -d glitchtip"]
interval: 10s
timeout: 5s
retries: 10
networks:
- monitoring
restart: unless-stopped
# --------------------------------------------------------------- metrics --
prometheus:
image: prom/prometheus:v3.4.1
container_name: monitoring-prometheus
mem_limit: 512m
command:
- --config.file=/etc/prometheus/prometheus.yml
- --storage.tsdb.retention.time=14d
- --storage.tsdb.retention.size=2GB
volumes:
- ./prometheus:/etc/prometheus:ro
- prometheus_data:/prometheus
networks:
- monitoring
- shared
restart: unless-stopped
grafana:
image: grafana/grafana:11.6.3
container_name: monitoring-grafana
mem_limit: 256m
environment:
GF_SECURITY_ADMIN_USER: admin
GF_SECURITY_ADMIN_PASSWORD: ${GRAFANA_ADMIN_PASSWORD:?set GRAFANA_ADMIN_PASSWORD in .env}
GF_USERS_ALLOW_SIGN_UP: "false"
GF_SERVER_ROOT_URL: ${GRAFANA_ROOT_URL:-http://localhost:3001/}
# Bot-observability Postgres datasource (provisioned from datasources.yml)
OBS_DB_HOST: postgres
OBS_DB_PORT: "5432"
OBS_DB_NAME: procurement
OBS_DB_USER: ${OBS_READER_USER:-obs_reader}
OBS_DB_PASSWORD: ${PROCUREMENT_BOT_OBS_READER_PASSWORD:-}
volumes:
- grafana_data:/var/lib/grafana
- ./grafana/provisioning:/etc/grafana/provisioning:ro
- ./grafana/dashboards:/var/lib/grafana/dashboards:ro
ports:
- "127.0.0.1:3001:3000"
networks:
- monitoring
- shared # reaches the app postgres for the bot_traces/bot_trace_events datasource
restart: unless-stopped
# ------------------------------------------------------------------ logs --
loki:
image: grafana/loki:3.5.0
container_name: monitoring-loki
mem_limit: 384m
command: -config.file=/etc/loki/loki-config.yml
volumes:
- ./loki/loki-config.yml:/etc/loki/loki-config.yml:ro
- loki_data:/loki
networks:
- monitoring
restart: unless-stopped
promtail:
image: grafana/promtail:3.5.0
container_name: monitoring-promtail
mem_limit: 64m
command: -config.file=/etc/promtail/promtail-config.yml
volumes:
- ./promtail/promtail-config.yml:/etc/promtail/promtail-config.yml:ro
- /var/lib/docker/containers:/var/lib/docker/containers:ro
- /var/run/docker.sock:/var/run/docker.sock:ro
networks:
- monitoring
restart: unless-stopped
# -------------------------------------------------------------- exporters --
# Linux-host only (see cadvisor note): local runs skip it via the profile.
node-exporter:
image: prom/node-exporter:v1.9.1
container_name: monitoring-node-exporter
mem_limit: 32m
profiles: ["host-metrics"]
command:
- --path.rootfs=/host
volumes:
- /:/host:ro,rslave
pid: host
networks:
- monitoring
restart: unless-stopped
postgres-exporter:
image: prometheuscommunity/postgres-exporter:v0.17.1
container_name: monitoring-postgres-exporter
mem_limit: 48m
environment:
# App database on the main stack's postgres (reaches it via erp_shared).
DATA_SOURCE_NAME: postgres://postgres:${POSTGRES_PASSWORD:?set POSTGRES_PASSWORD in .env}@postgres:5432/procurement?sslmode=disable
networks:
- monitoring
- shared
restart: unless-stopped
# Linux-host only: the rootfs bind mounts fail on Docker Desktop (Windows/macOS).
# VPS: docker compose -f monitoring/docker-compose.monitoring.yml --env-file .env \
# --profile host-metrics up -d
# Local: started WITHOUT the profile — per-container metrics are simply absent.
cadvisor:
image: gcr.io/cadvisor/cadvisor:v0.49.1
container_name: monitoring-cadvisor
mem_limit: 128m
profiles: ["host-metrics"]
privileged: true
devices:
- /dev/kmsg
volumes:
- /:/rootfs:ro
- /var/run:/var/run:ro
- /sys:/sys:ro
- /var/lib/docker/:/var/lib/docker:ro
networks:
- monitoring
restart: unless-stopped
networks:
monitoring:
driver: bridge
# Reaches the ERP containers (api:8000/metrics, postgres:5432) — the main
# compose declares the same fixed-name network as "shared".
shared:
name: erp_shared
external: true
# Created by the main compose; required only for the public GlitchTip route.
traefik:
name: traefik_network
external: true
volumes:
glitchtip_postgres_data:
prometheus_data:
grafana_data:
loki_data: