services:
  postgres:
    image: postgres:17
    environment:
      POSTGRES_DB: app
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: postgres-lab
    command:
      - postgres
      - -c
      - shared_preload_libraries=pg_stat_statements
      - -c
      - track_io_timing=on
      - -c
      - log_min_duration_statement=500
    volumes:
      - postgres-data:/var/lib/postgresql/data
      - ./init-monitor.sql:/docker-entrypoint-initdb.d/10-monitor.sql:ro
    ports:
      - "5432:5432"
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U postgres -d app"]
      interval: 5s
      timeout: 3s
      retries: 20

  postgres-exporter:
    image: quay.io/prometheuscommunity/postgres-exporter:latest
    environment:
      DATA_SOURCE_URI: postgres:5432/app?sslmode=disable
      DATA_SOURCE_USER: postgres_exporter
      DATA_SOURCE_PASS: exporter-lab
    command:
      - --collector.long_running_transactions
      - --collector.stat_statements
      - --collector.stat_checkpointer
    depends_on:
      postgres:
        condition: service_healthy
    ports:
      - "9187:9187"

  prometheus:
    image: prom/prometheus:latest
    command:
      - --config.file=/etc/prometheus/prometheus.yml
      - --storage.tsdb.retention.time=7d
    volumes:
      - ./prometheus/prometheus.yml:/etc/prometheus/prometheus.yml:ro
      - ./prometheus/postgres.rules.yml:/etc/prometheus/postgres.rules.yml:ro
      - prometheus-data:/prometheus
    ports:
      - "9090:9090"

  grafana:
    image: grafana/grafana:latest
    environment:
      GF_SECURITY_ADMIN_USER: admin
      GF_SECURITY_ADMIN_PASSWORD: admin-lab
      GF_USERS_ALLOW_SIGN_UP: "false"
    volumes:
      - ./grafana/provisioning:/etc/grafana/provisioning:ro
      - grafana-data:/var/lib/grafana
    ports:
      - "3000:3000"

volumes:
  postgres-data:
  prometheus-data:
  grafana-data:
