# ==============================================================================
# ADTLAS DAI MANAGEMENT SYSTEM - PROMETHEUS CONFIGURATION
# ==============================================================================
#
# This Prometheus configuration defines monitoring targets, scrape intervals,
# alerting rules, and service discovery for the Adtlas Django application
# and its infrastructure components.
#
# Features:
# - Django application metrics
# - Infrastructure monitoring (Node Exporter, cAdvisor)
# - Database monitoring
# - Redis monitoring
# - Nginx monitoring
# - Custom business metrics
# - Alerting rules integration
#
# Author: Adtlas Development Team
# Version: 2.0.0
# Last Updated: 2025-01-20
# ==============================================================================

# ==============================================================================
# GLOBAL CONFIGURATION
# ==============================================================================

global:
  # How frequently to scrape targets by default
  scrape_interval: 15s
  
  # How long until a scrape request times out
  scrape_timeout: 10s
  
  # How frequently to evaluate rules
  evaluation_interval: 15s
  
  # External labels to add to any time series or alerts
  external_labels:
    monitor: 'adtlas-monitor'
    environment: 'production'
    cluster: 'adtlas-cluster'
    datacenter: 'primary'

# ==============================================================================
# ALERTING CONFIGURATION
# ==============================================================================

alerting:
  alertmanagers:
    - static_configs:
        - targets:
          # Alertmanager service endpoint
          - alertmanager:9093
      # Timeout for sending alerts
      timeout: 10s
      # API version to use
      api_version: v2
      # Path prefix for alertmanager
      path_prefix: /

# ==============================================================================
# RULE FILES CONFIGURATION
# ==============================================================================

rule_files:
  # Django application specific rules
  - "rules/django_rules.yml"
  # Infrastructure monitoring rules
  - "rules/infrastructure_rules.yml"
  # Database monitoring rules
  - "rules/database_rules.yml"
  # Business logic rules
  - "rules/business_rules.yml"
  # General system rules
  - "rules/general_rules.yml"

# ==============================================================================
# SCRAPE CONFIGURATIONS
# ==============================================================================

scrape_configs:
  # ==============================================================================
  # PROMETHEUS SELF-MONITORING
  # ==============================================================================
  
  - job_name: 'prometheus'
    # Scrape Prometheus itself for monitoring
    static_configs:
      - targets: ['localhost:9090']
    # Scrape interval for Prometheus metrics
    scrape_interval: 30s
    # Metrics path
    metrics_path: /metrics
    # Additional labels (moved to metric_relabel_configs)
    metric_relabel_configs:
      - target_label: service
        replacement: 'prometheus'
      - target_label: component
        replacement: 'monitoring'

  # ==============================================================================
  # DJANGO APPLICATION MONITORING
  # ==============================================================================
  
  - job_name: 'django-app'
    # Django application metrics endpoint
    static_configs:
      - targets: ['web:8000']
    # Scrape interval for Django metrics
    scrape_interval: 15s
    # Metrics path (django-prometheus default)
    metrics_path: /metrics
    # Scrape timeout
    scrape_timeout: 10s
    # Honor labels from target
    honor_labels: true
    # Metric relabeling
    metric_relabel_configs:
      # Add instance label
      - source_labels: [__address__]
        target_label: instance
        replacement: 'django-app'
      # Add environment label
      - target_label: environment
        replacement: 'production'
      - target_label: service
        replacement: 'django'
      - target_label: component
        replacement: 'web-application'
      - target_label: application
        replacement: 'adtlas'

  # ==============================================================================
  # CELERY WORKER MONITORING
  # ==============================================================================
  
  - job_name: 'celery-worker'
    # Celery worker metrics (if exposed)
    static_configs:
      - targets: ['worker:8001']
    # Scrape interval for Celery metrics
    scrape_interval: 30s
    # Metrics path
    metrics_path: /metrics
    # Metric relabeling with labels
    metric_relabel_configs:
      - source_labels: [__address__]
        target_label: instance
        replacement: 'celery-worker'
      - target_label: service
        replacement: 'celery'
      - target_label: component
        replacement: 'worker'
      - target_label: application
        replacement: 'adtlas'

  # ==============================================================================
  # FLOWER MONITORING
  # ==============================================================================
  
  - job_name: 'flower'
    # Flower monitoring interface metrics
    static_configs:
      - targets: ['flower:5555']
    # Scrape interval for Flower metrics
    scrape_interval: 30s
    # Metrics path
    metrics_path: /metrics
    # Metric relabeling with labels
    metric_relabel_configs:
      - source_labels: [__address__]
        target_label: instance
        replacement: 'flower'
      - target_label: service
        replacement: 'flower'
      - target_label: component
        replacement: 'celery-monitoring'
      - target_label: application
        replacement: 'adtlas'

  # ==============================================================================
  # REDIS MONITORING
  # ==============================================================================
  
  - job_name: 'redis'
    # Redis metrics via redis_exporter
    static_configs:
      - targets: ['redis:6379']
    # Scrape interval for Redis metrics
    scrape_interval: 30s
    # Metrics path
    metrics_path: /metrics
    # Metric relabeling with labels
    metric_relabel_configs:
      - source_labels: [__address__]
        target_label: instance
        replacement: 'redis'
      - target_label: service
        replacement: 'redis'
      - target_label: component
        replacement: 'cache-broker'
      - target_label: application
        replacement: 'adtlas'

  # ==============================================================================
  # POSTGRESQL MONITORING
  # ==============================================================================
  
  - job_name: 'postgresql'
    # PostgreSQL metrics via postgres_exporter
    static_configs:
      - targets: ['db:5432']
    # Scrape interval for PostgreSQL metrics
    scrape_interval: 30s
    # Metrics path
    metrics_path: /metrics
    # Metric relabeling with labels
    metric_relabel_configs:
      - source_labels: [__address__]
        target_label: instance
        replacement: 'postgresql'
      - target_label: service
        replacement: 'postgresql'
      - target_label: component
        replacement: 'database'
      - target_label: application
        replacement: 'adtlas'

  # ==============================================================================
  # NGINX MONITORING
  # ==============================================================================
  
  - job_name: 'nginx'
    # Nginx metrics via nginx-prometheus-exporter
    static_configs:
      - targets: ['nginx:9113']
    # Scrape interval for Nginx metrics
    scrape_interval: 30s
    # Metrics path
    metrics_path: /metrics
    # Metric relabeling with labels
    metric_relabel_configs:
      - source_labels: [__address__]
        target_label: instance
        replacement: 'nginx'
      - target_label: service
        replacement: 'nginx'
      - target_label: component
        replacement: 'reverse-proxy'
      - target_label: application
        replacement: 'adtlas'

  # ==============================================================================
  # NODE EXPORTER (SYSTEM METRICS)
  # ==============================================================================
  
  - job_name: 'node-exporter'
    # System metrics via Node Exporter
    static_configs:
      - targets: ['node-exporter:9100']
    # Scrape interval for system metrics
    scrape_interval: 30s
    # Metrics path
    metrics_path: /metrics
    # Metric relabeling with labels
    metric_relabel_configs:
      - source_labels: [__address__]
        target_label: instance
        replacement: 'docker-host'
      - target_label: service
        replacement: 'node-exporter'
      - target_label: component
        replacement: 'system-monitoring'
      - target_label: application
        replacement: 'adtlas'

  # ==============================================================================
  # CADVISOR (CONTAINER METRICS)
  # ==============================================================================
  
  - job_name: 'cadvisor'
    # Container metrics via cAdvisor
    static_configs:
      - targets: ['cadvisor:8080']
    # Scrape interval for container metrics
    scrape_interval: 30s
    # Metrics path
    metrics_path: /metrics
    # Metric relabeling with labels
    metric_relabel_configs:
      - source_labels: [__address__]
        target_label: instance
        replacement: 'cadvisor'
      - target_label: service
        replacement: 'cadvisor'
      - target_label: component
        replacement: 'container-monitoring'
      - target_label: application
        replacement: 'adtlas'
      # Clean up container names
      - source_labels: [container_label_com_docker_compose_service]
        target_label: container_service
      - source_labels: [container_label_com_docker_compose_project]
        target_label: container_project

  # ==============================================================================
  # ALERTMANAGER MONITORING
  # ==============================================================================
  
  - job_name: 'alertmanager'
    # Alertmanager self-monitoring
    static_configs:
      - targets: ['alertmanager:9093']
    # Scrape interval for Alertmanager metrics
    scrape_interval: 30s
    # Metrics path
    metrics_path: /metrics
    # Metric relabeling
    metric_relabel_configs:
      - source_labels: [__address__]
        target_label: instance
        replacement: 'alertmanager'
      - target_label: service
        replacement: 'alertmanager'
      - target_label: component
        replacement: 'alerting'
      - target_label: application
        replacement: 'adtlas'

  # ==============================================================================
  # GRAFANA MONITORING
  # ==============================================================================
  
  - job_name: 'grafana'
    # Grafana self-monitoring
    static_configs:
      - targets: ['grafana:3000']
    # Scrape interval for Grafana metrics
    scrape_interval: 60s
    # Metrics path
    metrics_path: /metrics
    # Metric relabeling
    metric_relabel_configs:
      - source_labels: [__address__]
        target_label: instance
        replacement: 'grafana'
      - target_label: service
        replacement: 'grafana'
      - target_label: component
        replacement: 'visualization'
      - target_label: application
        replacement: 'adtlas'

  # ==============================================================================
  # BLACKBOX EXPORTER (ENDPOINT MONITORING)
  # ==============================================================================
  
  - job_name: 'blackbox-http'
    # HTTP endpoint monitoring
    metrics_path: /probe
    params:
      module: [http_2xx]  # Look for a HTTP 200 response
    static_configs:
      - targets:
        # Django application endpoints
        - http://nginx/health/
        - http://nginx/api/health/
        - http://nginx/admin/
        # External dependencies (if any)
        # - https://api.external-service.com/health
    relabel_configs:
      - source_labels: [__address__]
        target_label: __param_target
      - source_labels: [__param_target]
        target_label: instance
      - target_label: __address__
        replacement: blackbox-exporter:9115
    # Metric relabeling for labels
    metric_relabel_configs:
      - target_label: service
        replacement: 'blackbox-exporter'
      - target_label: component
        replacement: 'endpoint-monitoring'
      - target_label: probe_type
        replacement: 'http'

  # ==============================================================================
  # CUSTOM BUSINESS METRICS
  # ==============================================================================
  
  - job_name: 'adtlas-business-metrics'
    # Custom business metrics from Django application
    static_configs:
      - targets: ['web:8000']
    # Scrape interval for business metrics
    scrape_interval: 60s
    # Custom metrics path
    metrics_path: /metrics/business
    # Metric relabeling for business metrics
    metric_relabel_configs:
      - source_labels: [__address__]
        target_label: instance
        replacement: 'adtlas-business'
      - target_label: service
        replacement: 'django'
      - target_label: component
        replacement: 'business-metrics'
      - target_label: application
        replacement: 'adtlas'
      # Keep only business-specific metrics
      - source_labels: [__name__]
        regex: 'adtlas_.*'
        action: keep

# ==============================================================================
# REMOTE WRITE CONFIGURATION (for long-term storage)
# ==============================================================================

# Uncomment and configure for remote storage
# remote_write:
#   - url: "https://prometheus-remote-storage.example.com/api/v1/write"
#     basic_auth:
#       username: "prometheus"
#       password: "your-password"
#     write_relabel_configs:
#       - source_labels: [__name__]
#         regex: 'adtlas_.*|django_.*|http_.*'
#         action: keep

# ==============================================================================
# REMOTE READ CONFIGURATION (for querying remote storage)
# ==============================================================================

# Uncomment and configure for remote storage
# remote_read:
#   - url: "https://prometheus-remote-storage.example.com/api/v1/read"
#     basic_auth:
#       username: "prometheus"
#       password: "your-password"

# ==============================================================================
# STORAGE CONFIGURATION
# ==============================================================================

# Local storage configuration
# Note: These are command-line flags, not YAML configuration
# They should be set in docker-compose.yml or startup script
# --storage.tsdb.retention.time=30d
# --storage.tsdb.retention.size=10GB
# --storage.tsdb.wal-compression
# --storage.tsdb.min-block-duration=2h
# --storage.tsdb.max-block-duration=25h

# ==============================================================================
# CONFIGURATION NOTES
# ==============================================================================
#
# This Prometheus configuration provides comprehensive monitoring for:
# 1. Django application metrics (requests, responses, database queries)
# 2. Infrastructure metrics (CPU, memory, disk, network)
# 3. Container metrics (Docker containers resource usage)
# 4. Database metrics (PostgreSQL performance)
# 5. Cache metrics (Redis performance)
# 6. Web server metrics (Nginx performance)
# 7. Business metrics (custom application metrics)
# 8. Endpoint availability (HTTP probes)
# 9. Alert management (Alertmanager integration)
# 10. Visualization (Grafana integration)
#
# Key features:
# - Automatic service discovery
# - Metric relabeling for consistency
# - Multiple scrape intervals based on metric importance
# - Comprehensive labeling for filtering and grouping
# - Integration with alerting rules
# - Support for remote storage
# - Configurable retention policies
#
# To extend monitoring:
# 1. Add new scrape configs for additional services
# 2. Create custom metrics in Django application
# 3. Add new alerting rules
# 4. Configure remote storage for long-term retention
# 5. Set up federation for multi-cluster monitoring
# ==============================================================================