# Prometheus configuration — Generated by Ansible (Bunker Ops)
# Instance: {{ cml_domain }}

global:
  scrape_interval: {{ prometheus_scrape_interval }}
  evaluation_interval: {{ prometheus_evaluation_interval }}
  external_labels:
    monitor: 'changemaker-lite'
    instance: '{{ bunker_ops_instance_label | default(cml_domain) }}'
    domain: '{{ cml_domain }}'
    environment: '{{ cml_node_env }}'

# Alertmanager configuration
alerting:
  alertmanagers:
    - static_configs:
        - targets: ['alertmanager:9093']

# Load rules once and periodically evaluate them
rule_files:
  - "alerts.yml"

# Scrape configurations
scrape_configs:
  # V2 Unified API Metrics
  - job_name: 'changemaker-v2-api'
    static_configs:
      - targets: ['changemaker-v2-api:{{ cml_api_port }}']
    metrics_path: '/api/metrics'
    scrape_interval: {{ prometheus_api_scrape_interval }}
    scrape_timeout: 5s

  # N8N Metrics (if available)
  - job_name: 'n8n'
    static_configs:
      - targets: ['n8n-changemaker:5678']
    metrics_path: '/metrics'
    scrape_interval: 30s

  # Redis Metrics
  - job_name: 'redis'
    static_configs:
      - targets: ['redis-exporter-changemaker:9121']
    scrape_interval: 15s

  # cAdvisor - Docker container metrics
  - job_name: 'cadvisor'
    static_configs:
      - targets: ['cadvisor:8080']
    scrape_interval: 15s

  # Node Exporter - System metrics
  - job_name: 'node'
    static_configs:
      - targets: ['node-exporter:9100']
    scrape_interval: 15s

  # Prometheus self-monitoring
  - job_name: 'prometheus'
    static_configs:
      - targets: ['localhost:9090']

  # Alertmanager monitoring
  - job_name: 'alertmanager'
    static_configs:
      - targets: ['alertmanager:9093']
    scrape_interval: 30s

{% if prometheus_remote_write_enabled | bool %}
# Remote write to Bunker Ops central (VictoriaMetrics)
remote_write:
  - url: "{{ prometheus_remote_write_url }}"
{% if prometheus_remote_write_token %}
    authorization:
      credentials: "{{ prometheus_remote_write_token }}"
{% endif %}
    queue_config:
      max_samples_per_send: 1000
      batch_send_deadline: 5s
      max_shards: 3
    write_relabel_configs:
      # Only send cm_* and node_* metrics to central (not raw container metrics)
      - source_labels: [__name__]
        regex: '(cm_.*|node_.*|http_request.*|up)'
        action: keep
{% endif %}
