feat(observability): integrate Loki for centralised log aggregation (BIT-14248)

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]>
This commit is contained in:
David SantosandClaude Sonnet 4.6 committed 2026-04-27 08:03:20 +00:00
1 parent 94d2d12c97
commit 8bb49ce32b
7 files changed
+929

No files matched your search

+176
View File
@@ -0,0 +1,176 @@
# 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
```bash
# 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)
```logql
{app="errors-hub", level="error"} | json
```
### Logs for a specific service
```logql
{app="errors-hub", service="ingest-api"} | json
```
### Error rate per service (rate over 5 m)
```logql
sum by (service) (rate({app="errors-hub", level="error"}[5m]))
```
### Fatal logs (immediate on-call signal)
```logql
{app="errors-hub", level="fatal"} | json
```
### Ingest API request logs — errors only
```logql
{service="ingest-api", level="error"} | json | line_format "{{.msg}} err={{.err}}"
```
### Collector-worker retry / backoff events
```logql
{service="collector-worker"} |= "retry" | json
```
### All logs containing a specific error string
```logql
{app="errors-hub"} |= "ECONNREFUSED"
```
### Log volume by service over time (panel query)
```logql
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
1. Check Promtail is running and healthy:
```bash
docker compose --profile observability ps promtail
curl http://localhost:9080/ready
```
2. Inspect Promtail targets (shows which containers are being scraped):
```bash
curl http://localhost:9080/targets
```
3. Check Promtail logs for errors:
```bash
docker compose --profile observability logs promtail --tail=50
```
4. Verify the Docker socket is accessible:
```bash
docker compose --profile observability exec promtail ls -la /var/run/docker.sock
```
### Loki not receiving data
1. Check Loki readiness:
```bash
curl http://localhost:3100/ready
# expected: "ready"
```
2. Inspect Loki ingestion metrics:
```bash
curl -s http://localhost:3100/metrics | grep loki_distributor_lines
```
3. Check for rejected samples (old timestamps):
```bash
curl -s http://localhost:3100/metrics | grep loki_discarded_samples
```
If services were offline for > 168 h (the `reject_old_samples_max_age`), logs from the gap period will be rejected. Adjust `reject_old_samples_max_age` in `deploy/loki/loki.yaml` if needed.
### Logs visible in Loki but not in Grafana
1. Confirm the Grafana datasource URL points to Loki (`http://loki:3100` inside Docker, or `http://localhost:3100` / configured `ER_PUBLISH_LOKI_PORT` from the host).
2. Verify the time range in the Grafana explore panel covers when services were running.
3. Run a raw label query to confirm data exists:
```bash
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:
```bash
docker system df -v | grep loki-data
```
To purge all Loki data and start fresh:
```bash
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 |