# Grafana Alerting Configuration for Adtlas
# This file defines alert rules for monitoring the Adtlas application

apiVersion: 1

groups:
  - name: adtlas_infrastructure
    title: Adtlas Infrastructure Alerts
    folder: Infrastructure
    interval: 1m
    rules:
      # High CPU Usage Alert
      - uid: cpu_usage_high
        title: High CPU Usage
        condition: A
        data:
          - refId: A
            queryType: ''
            relativeTimeRange:
              from: 300
              to: 0
            model:
              expr: 'rate(container_cpu_usage_seconds_total{name=~"adtlas_.*"}[5m]) * 100 > 80'
              intervalMs: 1000
              maxDataPoints: 43200
        noDataState: NoData
        execErrState: Alerting
        for: 2m
        annotations:
          description: 'Container {{ $labels.name }} CPU usage is above 80% for more than 2 minutes'
          summary: 'High CPU usage detected'
        labels:
          severity: warning
          team: infrastructure

      # High Memory Usage Alert
      - uid: memory_usage_high
        title: High Memory Usage
        condition: A
        data:
          - refId: A
            queryType: ''
            relativeTimeRange:
              from: 300
              to: 0
            model:
              expr: '(container_memory_usage_bytes{name=~"adtlas_.*"} / container_spec_memory_limit_bytes{name=~"adtlas_.*"}) * 100 > 85'
              intervalMs: 1000
              maxDataPoints: 43200
        noDataState: NoData
        execErrState: Alerting
        for: 2m
        annotations:
          description: 'Container {{ $labels.name }} memory usage is above 85% for more than 2 minutes'
          summary: 'High memory usage detected'
        labels:
          severity: warning
          team: infrastructure

      # Container Down Alert
      - uid: container_down
        title: Container Down
        condition: A
        data:
          - refId: A
            queryType: ''
            relativeTimeRange:
              from: 300
              to: 0
            model:
              expr: 'up{job="docker"} == 0'
              intervalMs: 1000
              maxDataPoints: 43200
        noDataState: Alerting
        execErrState: Alerting
        for: 1m
        annotations:
          description: 'Container {{ $labels.instance }} is down'
          summary: 'Container is not responding'
        labels:
          severity: critical
          team: infrastructure

  - name: adtlas_application
    title: Adtlas Application Alerts
    folder: Application
    interval: 1m
    rules:
      # High HTTP Error Rate
      - uid: http_error_rate_high
        title: High HTTP Error Rate
        condition: A
        data:
          - refId: A
            queryType: ''
            relativeTimeRange:
              from: 300
              to: 0
            model:
              expr: 'rate(django_http_responses_total_by_status_total{status=~"5.."}[5m]) / rate(django_http_responses_total_by_status_total[5m]) * 100 > 5'
              intervalMs: 1000
              maxDataPoints: 43200
        noDataState: NoData
        execErrState: Alerting
        for: 2m
        annotations:
          description: 'HTTP 5xx error rate is above 5% for more than 2 minutes'
          summary: 'High application error rate detected'
        labels:
          severity: critical
          team: application

      # Slow Response Time
      - uid: response_time_slow
        title: Slow Response Time
        condition: A
        data:
          - refId: A
            queryType: ''
            relativeTimeRange:
              from: 300
              to: 0
            model:
              expr: 'histogram_quantile(0.95, rate(django_http_request_duration_seconds_bucket[5m])) > 2'
              intervalMs: 1000
              maxDataPoints: 43200
        noDataState: NoData
        execErrState: Alerting
        for: 3m
        annotations:
          description: '95th percentile response time is above 2 seconds for more than 3 minutes'
          summary: 'Application response time is slow'
        labels:
          severity: warning
          team: application

      # Database Connection Issues
      - uid: database_connection_issues
        title: Database Connection Issues
        condition: A
        data:
          - refId: A
            queryType: ''
            relativeTimeRange:
              from: 300
              to: 0
            model:
              expr: 'django_db_errors_total > 0'
              intervalMs: 1000
              maxDataPoints: 43200
        noDataState: NoData
        execErrState: Alerting
        for: 1m
        annotations:
          description: 'Database connection errors detected'
          summary: 'Database connectivity issues'
        labels:
          severity: critical
          team: database

  - name: adtlas_celery
    title: Adtlas Celery Alerts
    folder: Celery
    interval: 1m
    rules:
      # Celery Worker Down
      - uid: celery_worker_down
        title: Celery Worker Down
        condition: A
        data:
          - refId: A
            queryType: ''
            relativeTimeRange:
              from: 300
              to: 0
            model:
              expr: 'up{job="celery"} == 0'
              intervalMs: 1000
              maxDataPoints: 43200
        noDataState: Alerting
        execErrState: Alerting
        for: 1m
        annotations:
          description: 'Celery worker is not responding'
          summary: 'Celery worker is down'
        labels:
          severity: critical
          team: application

      # High Task Failure Rate
      - uid: celery_task_failure_rate_high
        title: High Celery Task Failure Rate
        condition: A
        data:
          - refId: A
            queryType: ''
            relativeTimeRange:
              from: 300
              to: 0
            model:
              expr: 'rate(celery_task_failed_total[5m]) / rate(celery_task_total[5m]) * 100 > 10'
              intervalMs: 1000
              maxDataPoints: 43200
        noDataState: NoData
        execErrState: Alerting
        for: 2m
        annotations:
          description: 'Celery task failure rate is above 10% for more than 2 minutes'
          summary: 'High Celery task failure rate'
        labels:
          severity: warning
          team: application

      # Long Running Tasks
      - uid: celery_long_running_tasks
        title: Long Running Celery Tasks
        condition: A
        data:
          - refId: A
            queryType: ''
            relativeTimeRange:
              from: 300
              to: 0
            model:
              expr: 'celery_task_runtime_seconds > 300'
              intervalMs: 1000
              maxDataPoints: 43200
        noDataState: NoData
        execErrState: Alerting
        for: 1m
        annotations:
          description: 'Celery task {{ $labels.task }} has been running for more than 5 minutes'
          summary: 'Long running Celery task detected'
        labels:
          severity: warning
          team: application

  - name: adtlas_storage
    title: Adtlas Storage Alerts
    folder: Storage
    interval: 5m
    rules:
      # Low Disk Space
      - uid: disk_space_low
        title: Low Disk Space
        condition: A
        data:
          - refId: A
            queryType: ''
            relativeTimeRange:
              from: 300
              to: 0
            model:
              expr: '(node_filesystem_avail_bytes{mountpoint="/"} / node_filesystem_size_bytes{mountpoint="/"}) * 100 < 15'
              intervalMs: 1000
              maxDataPoints: 43200
        noDataState: NoData
        execErrState: Alerting
        for: 5m
        annotations:
          description: 'Disk space on {{ $labels.instance }} is below 15%'
          summary: 'Low disk space warning'
        labels:
          severity: warning
          team: infrastructure

      # Critical Disk Space
      - uid: disk_space_critical
        title: Critical Disk Space
        condition: A
        data:
          - refId: A
            queryType: ''
            relativeTimeRange:
              from: 300
              to: 0
            model:
              expr: '(node_filesystem_avail_bytes{mountpoint="/"} / node_filesystem_size_bytes{mountpoint="/"}) * 100 < 5'
              intervalMs: 1000
              maxDataPoints: 43200
        noDataState: Alerting
        execErrState: Alerting
        for: 1m
        annotations:
          description: 'Disk space on {{ $labels.instance }} is critically low (below 5%)'
          summary: 'Critical disk space alert'
        labels:
          severity: critical
          team: infrastructure