Skip to content

Monitoring Setup

The project includes a Docker Compose stack for centralized log monitoring using Grafana, Loki, and Alloy.

Architecture

flowchart LR
    Bot["Grid Trading Bot"] -->|writes logs| Logs["logs/*.log"]
    Logs -->|tails files| Alloy["Alloy v1.13.2"]
    Alloy -->|pushes logs| Loki["Loki 3.6.7"]
    Loki -->|queries| Grafana["Grafana 12.4.0"]

Prerequisites

Start the Stack

docker-compose up -d

This starts three services:

Service Image Port Purpose
Loki grafana/loki:3.6.7 none published Log aggregation and storage
Grafana grafana/grafana:12.4.0 127.0.0.1:3000 Visualization and dashboards
Alloy grafana/alloy:v1.13.2 127.0.0.1:12345 Log collection agent

Loki is reachable only on the compose network. Grafana and Alloy listen on 127.0.0.1, so they are not reachable from another machine.

Run the Bot with Docker Compose

The bot itself is available as an optional Compose service behind the bot profile, so docker-compose up -d keeps starting the monitoring stack only.

# Build the bot image and start everything (monitoring + bot)
docker-compose --profile bot up -d --build

The service is defined for 24/7 operation on a VPS:

  • restart: on-failure: a halted bot exits with 0 and stays down; a crash, a lost feed or a reboot relaunches it. See Going Live
  • ./data volume — the SQLite state survives restarts, and the recovery service reconciles it with the exchange on startup
  • ./logs volume — the bot writes its logs where Alloy tails them, so they show up in Grafana automatically
  • Default SIGTERM and a 60 second grace period: docker compose stop keeps the open orders, writes a final checkpoint and exits with 143

By default the bot runs with config/config.json, which is a backtest configuration: it runs once and exits with 0, so the restart policy does not run it again. For a long-running deployment, point the service at a live configuration via the BOT_CONFIG variable in your .env file:

# .env
BOT_CONFIG=config/config.live.json
EXCHANGE_API_KEY=your_api_key
EXCHANGE_SECRET_KEY=your_secret_key

See config/config.live.example.json for a live configuration template.

To follow the bot output, stop it, or deploy a new version:

docker-compose logs -f bot

# Suspend: SIGTERM, open orders kept, final checkpoint written
docker-compose --profile bot stop bot

# Rebuild and restart after a git pull
docker-compose --profile bot up -d --build bot

Protect Grafana on a public VPS

Grafana is bound to 127.0.0.1. Reach it through an SSH tunnel (ssh -L 3000:127.0.0.1:3000 user@vps), or publish it through a reverse proxy such as Caddy with its own password.

Access Grafana

  1. Navigate to http://localhost:3000
  2. Log in with the credentials from your .env file:
    • Username: value of GRAFANA_ADMIN_USER (default: admin)
    • Password: value of GRAFANA_ADMIN_PASSWORD

Loki is pre-configured as the default datasource.

Log Pipeline

How Alloy Processes Logs

Alloy tails all .log files from the logs/ directory and extracts structured labels from the log filenames.

Labels extracted from filename:

Label Example Description
base_currency SOL Base currency of the trading pair
quote_currency USDT Quote currency of the trading pair
trading_mode backtest Trading mode (backtest/paper_trading/live)
strategy_type simple_grid Grid strategy type
spacing_type geometric Grid spacing type
grid_size 8 Number of grid levels
grid_range 120-200 Grid price range, bottom-top; whole bounds carry no decimals, fractional ones such as 0.25-0.5 do

A derived trading_pair label is created by combining base_currency/quote_currency (e.g., SOL/USDT).

Log Format Parsing

Alloy parses each log line to extract:

  • Timestamp — Used for time-series indexing in Loki
  • Service — The component that emitted the log
  • Level — Log severity (DEBUG, INFO, WARNING, ERROR)
  • Message — The log content

The service is the logger name of the class that wrote the line. Limit order placement, the initial purchase and the liquidation log as Placement, InitialPurchase and Liquidation (they were OrderManager); the strategies as BacktestGridStrategy and LiveGridStrategy (GridTradingStrategy); the recovery steps as ExchangeResolution and BalanceRestore (StateRecoveryService); the ticker stream as LiveTickerFeed (LiveExchangeService). The dashboard queries follow these names; an external Loki query that filters on the old logger name needs the same change.

Alerts

Grafana and Loki only display data: they send no alert. Alerts go through Apprise (APPRISE_NOTIFICATION_URLS): order failures, errors, health check issues (CPU, memory, disk, stale ticker feed) and TP/SL triggers. See Notifications.

Configuration Files

File Purpose
docker-compose.yml Service definitions and volumes
monitoring/configs/loki/loki.yaml Loki storage, limits, and cache settings
monitoring/configs/alloy/config.alloy Log tailing and label extraction
monitoring/configs/grafana/provisioning/datasources.yml Loki datasource auto-provisioning
monitoring/configs/grafana/provisioning/dashboards.yml Dashboard auto-provisioning

Stop the Stack

docker-compose down

To also remove stored data:

docker-compose down -v