Skip to content

Recipe Loki Grafana Stack

Avinash Gupta edited this page Oct 18, 2026 · 1 revision

Recipe: search, dashboards and alerts with Loki + Grafana

For teams with no log platform. One docker compose up on any host that can reach the RDS API gives you:

  • full-text search over every RDS log line (Grafana Explore + a search box)
  • an RDS logs dashboard: security counters, errors by database, slow queries, security events
  • alerts for destructive DDL, privilege changes, failed-login bursts, DELETE/UPDATE without WHERE, connection exhaustion, FATAL/PANIC lines and repeated deadlocks, routed to Slack, email or any webhook

Everything lives in examples/stack-loki-grafana/.

rdstail ──http──▶ vector ──▶ Loki ──▶ Grafana (dashboard, Explore)
                  disk         │
                  buffer       └─ ruler ──▶ Alertmanager ──▶ Slack / email / webhook

Run it

cd examples/stack-loki-grafana
export AWS_REGION=us-east-1
export AWS_PROFILE=my-profile            # or AWS_ACCESS_KEY_ID / AWS_SECRET_ACCESS_KEY
export GRAFANA_ADMIN_PASSWORD='choose-one'
docker compose up -d

By default rdstail discovers every supported instance in AWS_REGION and starts at the end of each log (only new lines). To narrow it, edit rdstail.yaml: list instances:, tag databases rdstail=off to exclude them, or add regions:. The IAM rdstail needs:

rdstail iam-policy -c rdstail.yaml

Running on EC2 with an instance role? Containers reach IMDSv2 only when the instance's metadata hop limit is ≥ 2 (aws ec2 modify-instance-metadata-options --http-put-response-hop-limit 2).

The dashboard

Panel Shows
Destructive DDL / Privilege changes / Failed logins / ERROR+ Counts over the selected time range; anything non-zero turns red/orange
Log lines by severity, Errors by instance Volume over time. A sudden ERROR wall is visible at a glance
Security events The lines behind the counters, prefixed with the database
Errors ERROR / FATAL / PANIC lines
Slow queries MySQL slow-query entries, PostgreSQL duration: lines
Search Every line matching the Search box (case-insensitive regex)

Filter by Instance and Engine at the top. For ad-hoc digging use Explore with LogQL:

{job="rdstail", instance="orders-db"} |~ `(?i)deadlock|lock wait`
{job="rdstail", log_type="slowquery"} |~ `Rows_examined: \d{7,}`
{job="rdstail", severity=~"ERROR|FATAL"} | log_file=~"error/.*" != "canceling statement"

Labels (cheap to filter on): instance, engine, severity (NONE for continuation lines), log_type (error, slowquery, general, audit). log_file and batch_id are structured metadata. Filter them with | log_file=~"…" after the selector.

Alerts

Rules live in loki-rules/fake/rds-security.yaml and are evaluated every minute by Loki's ruler:

Alert Fires when Severity
RDSDestructiveDDL DROP TABLE/DATABASE/SCHEMA/VIEW/INDEX/FUNCTION/…, TRUNCATE critical
RDSPrivilegeChange GRANT, REVOKE, CREATE/ALTER/DROP/RENAME USER/ROLE, SET PASSWORD warning
RDSFailedLoginBurst ≥ 10 failed authentications in 5 min warning
RDSUnboundedWrite DELETE FROM / UPDATE … SET with no WHERE warning
RDSConnectionExhaustion "too many connections" / "remaining connection slots are reserved" critical
RDSFatalErrors FATAL/PANIC lines (auth failures excluded) warning
RDSDeadlocks ≥ 3 deadlocks in 10 min info

These only fire if the engine logs the statements. DDL, GRANTs and DML are not in RDS logs by default. Security detections lists the exact parameter-group / option-group settings per engine.

Send them somewhere: in alertmanager.yml, uncomment the Slack, email or webhook receiver, set route.receiver to it, and docker compose restart alertmanager. For Slack, mount a file holding the webhook URL at the api_url_file path. Out of the box nothing leaves the host: alerts show in Alertmanager and Grafana → Alerting.

Tune false positives by adding a negative filter to a rule, e.g. ETL jobs that drop scratch tables:

{job="rdstail"} |~ `(?i)\bdrop\s+table` !~ `(?i)drop\s+table\s+(if\s+exists\s+)?tmp_`

Latency: a statement reaches Loki one rdstail poll after RDS writes it (p50 ≈ 3.5 s at poll_interval: 5s; this stack uses 10 s and backs quiet databases off to 2 min). The ruler evaluates every minute, and Alertmanager waits group_wait (30 s). Expect alerts 1–3 minutes after the statement. That's fast enough for detection, but these alerts are not a prevention control.

Delivery guarantees

  • rdstail's http sink gets a 2xx from vector only after the batch is in vector's disk buffer (acknowledgements on the Loki sink). rdstail checkpoints after that 2xx, so nothing is lost between them.

  • vector's 1 GiB disk buffer rides out a Loki restart; when full it blocks, which back-pressures rdstail (its checkpoints simply stop advancing).

  • If vector is down, rdstail retries with backoff and then parks batches in its DLQ (rdstail_dlq_depth on :9090). Once vector is back, replay them:

    docker compose stop rdstail
    docker compose run --rm rdstail dlq replay -c /etc/rdstail/config.yaml
    docker compose start rdstail
  • Loki dedupes identical (timestamp, line) entries in a stream, so an at-least-once re-send after a crash does not double-count.

Security of the stack itself

RDS logs contain SQL text, user names and client IPs. That's why every port is bound to 127.0.0.1. Before letting others in:

  • put Grafana behind your SSO / reverse proxy, or at least change the admin password and create viewer accounts
  • keep Loki (3100) and Alertmanager (9093) private: neither has auth here
  • consider dropping noisy or sensitive lines in the vector remap (e.g. abort on log_type == "general")

Sizing and retention

  • Loki keeps 30 days (retention_period: 720h in loki.yaml); keep S3 as the long-term archive by adding an S3 sink to rdstail.yaml. Every sink receives every record.
  • A single-node Loki on local disk is right for this stack's audience: a handful to a few dozen databases with error/slow/audit logs. If you're shipping general_log from busy databases, read Performance and throughput first. The RDS API, not Loki, is usually the limit.
  • Keep labels low-cardinality. Never turn log_file, batch_id or anything from the message into a label.

Removing it

docker compose down        # keep data volumes
docker compose down -v     # also delete logs, state and dashboards

Clone this wiki locally