# Resource limit alert rules for Expanso

groups:
  - name: expanso_resource_limits
    interval: 30s
    rules:
      # Alert when file descriptors are running low
      - alert: HighFileDescriptorUsage
        expr: |
          (expanso_process_open_file_descriptors_ratio /
           expanso_process_max_file_descriptors_ratio) > 0.8
        for: 2m
        labels:
          severity: warning
          component: system
        annotations:
          summary: "High file descriptor usage on {{ $labels.service_name }}"
          description: "{{ $labels.service_name }} is using {{ $value | humanizePercentage }} of available file descriptors"

      # Alert on potential memory leak
      - alert: MemoryLeakDetected
        expr: |
          rate(expanso_process_memory_usage_bytes[1h]) > 10 * 1024 * 1024
        for: 30m
        labels:
          severity: warning
          component: performance
        annotations:
          summary: "Potential memory leak in {{ $labels.service_name }}"
          description: "{{ $labels.service_name }} memory is growing at {{ $value | humanize }}B/s ({{ $value * 3600 | humanize }}B/hour)"

      # Alert on goroutine leak
      - alert: GoroutineLeakDetected
        expr: |
          expanso_go_goroutines_count > 5000
        for: 10m
        labels:
          severity: critical
          component: performance
        annotations:
          summary: "Goroutine leak in {{ $labels.service_name }}"
          description: "{{ $labels.service_name }} has {{ $value }} goroutines running"

      # Alert on excessive GC activity
      - alert: ExcessiveGarbageCollection
        expr: |
          rate(expanso_go_gc_cycles_total[5m]) > 2
        for: 5m
        labels:
          severity: warning
          component: performance
        annotations:
          summary: "Excessive GC in {{ $labels.service_name }}"
          description: "{{ $labels.service_name }} is running GC {{ $value }} times per second"
