Adds Grafana Loki + Promtail to the Docker Compose observability profile, enabling structured-log collection from all errors.bitreport.pt services. - deploy/loki/loki.yaml: Loki server (filesystem storage, TSDB v13, 30-day retention) - deploy/loki/promtail.yaml: Docker SD auto-discovery, JSON parse, Pino level normalisation - deploy/grafana/loki-logs-dashboard.json: Grafana log dashboard (uid er-loki-logs) - docs/OBSERVABILITY_LOKI.md: setup, key LogQL queries, troubleshooting runbook - docker-compose.yml: loki + promtail services under --profile observability; LOG_LEVEL env - .env.example / ingest-api/.env.example: LOG_LEVEL + ER_PUBLISH_LOKI_PORT documented Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
5.9 KiB
Loki Log Aggregation — errors.bitreport.pt
Centralised log aggregation for all errors.bitreport.pt services using Grafana Loki + Promtail.
Architecture
ingest-api ──┐
collector-worker ─┤ stdout/stderr ┌──────────────┐ HTTP push ┌──────┐
admin-ui ──┤ ──────────────────▶│ Promtail │──────────────▶│ Loki │
api ──┤ (Docker socket) │ (Docker SD) │ └──────┘
mariadb ──┘ └──────────────┘ │
▼
Grafana (LogQL)
Promtail discovers containers via the Docker socket by matching the errors-bitreport-pt compose project label. All services emit structured JSON logs (Pino format); Promtail parses them and normalises Pino numeric levels to human-readable strings before shipping.
Starting the observability stack
# Start core services + Loki + Promtail
docker compose --profile observability up -d
# Core services only (no log aggregation)
docker compose up -d
Loki listens on port 3100 (configurable via ER_PUBLISH_LOKI_PORT in .env).
Loki labels
Every log line carries these labels:
| Label | Source | Example values |
|---|---|---|
service |
compose service name | ingest-api, collector-worker, admin-ui |
compose_project |
Docker label | errors-bitreport-pt |
container |
Docker container name | /errors-bitreport-pt-ingest-api-1 |
app |
static | errors-hub |
env |
static | production |
level |
normalised from Pino JSON level field |
info, warn, error, fatal |
trace and debug lines are dropped by Promtail in production.
Key LogQL queries
Error logs across all services (last 1 h)
{app="errors-hub", level="error"} | json
Logs for a specific service
{app="errors-hub", service="ingest-api"} | json
Error rate per service (rate over 5 m)
sum by (service) (rate({app="errors-hub", level="error"}[5m]))
Fatal logs (immediate on-call signal)
{app="errors-hub", level="fatal"} | json
Ingest API request logs — errors only
{service="ingest-api", level="error"} | json | line_format "{{.msg}} err={{.err}}"
Collector-worker retry / backoff events
{service="collector-worker"} |= "retry" | json
All logs containing a specific error string
{app="errors-hub"} |= "ECONNREFUSED"
Log volume by service over time (panel query)
sum by (service) (count_over_time({app="errors-hub"}[1m]))
Grafana integration
The Loki datasource must be added to Grafana with URL http://loki:3100 (or the host-mapped port if Grafana runs outside the compose network).
The pre-built log visualisation dashboard is at deploy/grafana/loki-logs-dashboard.json. Import it via Dashboards → Import → Upload JSON.
The dashboard requires a Loki datasource named Loki (or update the datasource field in the JSON to match your instance name).
Troubleshooting
Promtail not shipping logs
-
Check Promtail is running and healthy:
docker compose --profile observability ps promtail curl http://localhost:9080/ready -
Inspect Promtail targets (shows which containers are being scraped):
curl http://localhost:9080/targets -
Check Promtail logs for errors:
docker compose --profile observability logs promtail --tail=50 -
Verify the Docker socket is accessible:
docker compose --profile observability exec promtail ls -la /var/run/docker.sock
Loki not receiving data
-
Check Loki readiness:
curl http://localhost:3100/ready # expected: "ready" -
Inspect Loki ingestion metrics:
curl -s http://localhost:3100/metrics | grep loki_distributor_lines -
Check for rejected samples (old timestamps):
curl -s http://localhost:3100/metrics | grep loki_discarded_samplesIf services were offline for > 168 h (the
reject_old_samples_max_age), logs from the gap period will be rejected. Adjustreject_old_samples_max_ageindeploy/loki/loki.yamlif needed.
Logs visible in Loki but not in Grafana
- Confirm the Grafana datasource URL points to Loki (
http://loki:3100inside Docker, orhttp://localhost:3100/ configuredER_PUBLISH_LOKI_PORTfrom the host). - Verify the time range in the Grafana explore panel covers when services were running.
- Run a raw label query to confirm data exists:
curl 'http://localhost:3100/loki/api/v1/labels' curl 'http://localhost:3100/loki/api/v1/query?query=\{app="errors-hub"\}&limit=5'
JSON not being parsed (raw log lines in Grafana)
Promtail only runs the JSON pipeline stage; if a service emits plain-text lines (e.g. MariaDB), the json stage is silently skipped and the raw line is stored as-is. The level label will be absent for those lines.
Disk usage
Loki retains logs for 30 days (configurable via limits_config.retention_period in deploy/loki/loki.yaml). Data is stored in the loki-data Docker volume. To check current volume size:
docker system df -v | grep loki-data
To purge all Loki data and start fresh:
docker compose --profile observability down
docker volume rm errors-bitreport-pt_loki-data errors-bitreport-pt_promtail-positions
docker compose --profile observability up -d
Configuration files
| File | Purpose |
|---|---|
deploy/loki/loki.yaml |
Loki server — storage, schema, retention |
deploy/loki/promtail.yaml |
Promtail — Docker SD, JSON parsing, label extraction |
deploy/grafana/loki-logs-dashboard.json |
Grafana log visualisation dashboard |