version: '3.8'

services:
  #------------------------------------------------------------------------------
  # Prometheus - Metrics Collection
  #------------------------------------------------------------------------------
  prometheus:
    image: prom/prometheus:v2.48.0
    container_name: mockupaws-prometheus
    restart: unless-stopped
    command:
      - '--config.file=/etc/prometheus/prometheus.yml'
      - '--storage.tsdb.path=/prometheus'
      - '--storage.tsdb.retention.time=30d'
      - '--web.console.libraries=/usr/share/prometheus/console_libraries'
      - '--web.console.templates=/usr/share/prometheus/consoles'
      - '--web.enable-lifecycle'
    volumes:
      - ./infrastructure/monitoring/prometheus/prometheus.yml:/etc/prometheus/prometheus.yml:ro
      - ./infrastructure/monitoring/prometheus/alerts.yml:/etc/prometheus/alerts/alerts.yml:ro
      - prometheus_data:/prometheus
    ports:
      - "9090:9090"
    networks:
      - monitoring

  #------------------------------------------------------------------------------
  # Grafana - Visualization
  #------------------------------------------------------------------------------
  grafana:
    image: grafana/grafana:10.2.0
    container_name: mockupaws-grafana
    restart: unless-stopped
    environment:
      - GF_SECURITY_ADMIN_USER=admin
      - GF_SECURITY_ADMIN_PASSWORD=${GRAFANA_ADMIN_PASSWORD:-admin}
      - GF_USERS_ALLOW_SIGN_UP=false
      - GF_SERVER_ROOT_URL=https://grafana.mockupaws.com
      - GF_INSTALL_PLUGINS=grafana-clock-panel,grafana-simple-json-datasource
    volumes:
      - ./infrastructure/monitoring/grafana/dashboards:/etc/grafana/provisioning/dashboards:ro
      - ./infrastructure/monitoring/grafana/datasources.yml:/etc/grafana/provisioning/datasources/datasources.yml:ro
      - grafana_data:/var/lib/grafana
    ports:
      - "3000:3000"
    networks:
      - monitoring
    depends_on:
      - prometheus

  #------------------------------------------------------------------------------
  # Alertmanager - Alert Routing
  #------------------------------------------------------------------------------
  alertmanager:
    image: prom/alertmanager:v0.26.0
    container_name: mockupaws-alertmanager
    restart: unless-stopped
    command:
      - '--config.file=/etc/alertmanager/alertmanager.yml'
      - '--storage.path=/alertmanager'
    volumes:
      - ./infrastructure/monitoring/alerts/alertmanager.yml:/etc/alertmanager/alertmanager.yml:ro
      - alertmanager_data:/alertmanager
    ports:
      - "9093:9093"
    networks:
      - monitoring

  #------------------------------------------------------------------------------
  # Node Exporter - Host Metrics
  #------------------------------------------------------------------------------
  node-exporter:
    image: prom/node-exporter:v1.7.0
    container_name: mockupaws-node-exporter
    restart: unless-stopped
    command:
      - '--path.rootfs=/host'
      - '--path.procfs=/host/proc'
      - '--path.sysfs=/host/sys'
      - '--collector.filesystem.mount-points-exclude=^/(sys|proc|dev|host|etc)($$|/)'
    volumes:
      - /proc:/host/proc:ro
      - /sys:/host/sys:ro
      - /:/host:ro,rslave
    networks:
      - monitoring

  #------------------------------------------------------------------------------
  # cAdvisor - Container Metrics
  #------------------------------------------------------------------------------
  cadvisor:
    image: gcr.io/cadvisor/cadvisor:v0.47.2
    container_name: mockupaws-cadvisor
    restart: unless-stopped
    privileged: true
    devices:
      - /dev/kmsg:/dev/kmsg
    volumes:
      - /:/rootfs:ro
      - /var/run:/var/run:ro
      - /sys:/sys:ro
      - /var/lib/docker:/var/lib/docker:ro
      - /cgroup:/cgroup:ro
    networks:
      - monitoring

  #------------------------------------------------------------------------------
  # PostgreSQL Exporter
  #------------------------------------------------------------------------------
  postgres-exporter:
    image: prometheuscommunity/postgres-exporter:v0.15.0
    container_name: mockupaws-postgres-exporter
    restart: unless-stopped
    environment:
      DATA_SOURCE_NAME: ${DATABASE_URL:-postgresql://postgres:postgres@postgres:5432/mockupaws?sslmode=disable}
    networks:
      - monitoring
      - mockupaws
    depends_on:
      - postgres

  #------------------------------------------------------------------------------
  # Redis Exporter
  #------------------------------------------------------------------------------
  redis-exporter:
    image: oliver006/redis_exporter:v1.55.0
    container_name: mockupaws-redis-exporter
    restart: unless-stopped
    environment:
      REDIS_ADDR: ${REDIS_URL:-redis://redis:6379}
    networks:
      - monitoring
      - mockupaws
    depends_on:
      - redis

  #------------------------------------------------------------------------------
  # Loki - Log Aggregation
  #------------------------------------------------------------------------------
  loki:
    image: grafana/loki:2.9.0
    container_name: mockupaws-loki
    restart: unless-stopped
    command: -config.file=/etc/loki/local-config.yaml
    volumes:
      - ./infrastructure/monitoring/loki/loki.yml:/etc/loki/local-config.yaml:ro
      - loki_data:/loki
    ports:
      - "3100:3100"
    networks:
      - monitoring

  #------------------------------------------------------------------------------
  # Promtail - Log Shipper
  #------------------------------------------------------------------------------
  promtail:
    image: grafana/promtail:2.9.0
    container_name: mockupaws-promtail
    restart: unless-stopped
    command: -config.file=/etc/promtail/config.yml
    volumes:
      - ./infrastructure/monitoring/loki/promtail.yml:/etc/promtail/config.yml:ro
      - /var/log:/var/log:ro
      - /var/lib/docker/containers:/var/lib/docker/containers:ro
    networks:
      - monitoring
    depends_on:
      - loki

networks:
  monitoring:
    driver: bridge
  mockupaws:
    external: true

volumes:
  prometheus_data:
  grafana_data:
  alertmanager_data:
  loki_data:
