prometheus config

Guide for prometheus config

Prometheus Configuration

This configuration file sets up Prometheus to scrape metrics from the Legends of Hastinapur game client and server.

Configuration File

File: prometheus.yml
global:
  scrape_interval: 2s      # How often to scrape targets
  evaluation_interval: 2s  # How often to evaluate rules

scrape_configs:
  # Game Client Metrics
  - job_name: 'legends_client'
    static_configs:
      - targets: 
          - 'host.docker.internal:9000'  # Docker Desktop on Mac/Windows
          - 'localhost:9000'              # Native Linux or direct access
        labels:
          env: 'dev'
          component: 'client'

  # Game Server Metrics
  - job_name: 'legends_server'
    static_configs:
      - targets:
          - 'host.docker.internal:9001'
          - 'localhost:9001'
        labels:
          env: 'dev'
          component: 'server'

Usage

Local Development (Docker)

  1. Create prometheus.yml in your project root with the above content
  2. Run Prometheus:
    docker run -d \
    --name prometheus \
    -p 9090:9090 \
    -v $(pwd)/prometheus.yml:/etc/prometheus/prometheus.yml \
    prom/prometheus:latest
  3. Access Prometheus UI: http://localhost:9090
  4. Run your game:
    cargo run --bin legends_client
  5. Verify metrics:
    • Visit http://localhost:9090/targets to see scrape status
    • Query metrics in the Prometheus UI (e.g., tick_duration_seconds)

Local Development (Native)

If running Prometheus natively (not in Docker):
# Download Prometheus
wget https://github.com/prometheus/prometheus/releases/download/v2.45.0/prometheus-2.45.0.linux-amd64.tar.gz
tar xvfz prometheus-2.45.0.linux-amd64.tar.gz
cd prometheus-2.45.0.linux-amd64

# Copy your config
cp /path/to/your/prometheus.yml ./prometheus.yml

# Run Prometheus
./prometheus --config.file=prometheus.yml

Metrics Endpoints

Client Metrics (localhost:9000/metrics)

Exposed by metrics-exporter-prometheus in src/systems/telemetry.rs:
let builder = PrometheusBuilder::new();
builder
    .with_http_listener(([0, 0, 0, 0], 9000))
    .install()
    .expect("failed to install Prometheus recorder");
Example metrics:
# HELP tick_duration_seconds Game loop tick duration
# TYPE tick_duration_seconds histogram
tick_duration_seconds_bucket{le="0.005"} 120
tick_duration_seconds_bucket{le="0.01"} 450
tick_duration_seconds_sum 5.234
tick_duration_seconds_count 500

# HELP entities_count Number of active entities
# TYPE entities_count gauge
entities_count 1523

# HELP fps Frames per second
# TYPE fps gauge
fps 60.2

Server Metrics (localhost:9001/metrics)

Similar setup in the game server binary.

Querying Metrics

Prometheus Query Language (PromQL)

Average tick duration over 5 minutes:
rate(tick_duration_seconds_sum[5m]) / rate(tick_duration_seconds_count[5m])
95th percentile tick duration:
histogram_quantile(0.95, rate(tick_duration_seconds_bucket[5m]))
Entity count by component:
entities_count{component="client"}
Quest completion rate:
rate(quest_completions_total[1m])

Grafana Integration

Add Prometheus as Data Source

  1. Access Grafana: http://localhost:3000 (if running Grafana)
  2. Configuration → Data Sources → Add data source
  3. Select Prometheus
  4. URL: http://prometheus:9090 (if in Docker network) or http://localhost:9090
  5. Save & Test

Sample Dashboard Panels

Game Performance Panel:
  • Metric: histogram_quantile(0.95, rate(tick_duration_seconds_bucket[5m]))
  • Visualization: Time series graph
  • Alert: Trigger if P95 > 50ms
Active Players Panel:
  • Metric: active_connections{component="server"}
  • Visualization: Gauge
  • Threshold: Green < 100, Yellow < 500, Red >= 500

Production Configuration

For production, use service discovery instead of static targets:
scrape_configs:
  - job_name: 'legends_client'
    kubernetes_sd_configs:
      - role: pod
        namespaces:
          names:
            - legends-game
    relabel_configs:
      - source_labels: [__meta_kubernetes_pod_label_app]
        regex: legends-client
        action: keep

Troubleshooting

Metrics endpoint not accessible

Check if the game is running:
curl http://localhost:9000/metrics
Expected output: Prometheus metrics in text format

Prometheus not scraping

Check Prometheus logs:
docker logs prometheus
Check targets page: http://localhost:9090/targets
  • Should show legends_client and legends_server as UP

No metrics appearing

Verify metrics are being recorded in code:
use metrics::counter;
counter!("test_metric").increment(1);
Check metrics endpoint:
curl http://localhost:9000/metrics | grep test_metric