# TipSharks API Alert Rules
# Import these rules into Grafana via the Alerting UI or provisioning.
# See https://grafana.com/docs/grafana/latest/alerting/set-up/provision-alerts/

apiVersion: 1

groups:
  - name: tipsharks-api-critical
    interval: 30s
    rules:
      - uid: tipsharks_api_5xx_rate
        title: "API 5xx Error Rate > 1%"
        condition: |
          avg by (service) (
            rate(tipsharks_api_requests_total{status=~"5.."}[5m])
          )
          /
          avg by (service) (
            rate(tipsharks_api_requests_total[5m])
          )
          * 100 > 1
        for: 5m
        labels:
          severity: critical
          team: backend
        annotations:
          summary: "API 5xx error rate exceeds 1% for 5 minutes"
          description: "Current 5xx rate: {{ $value | humanizePercent }}. Threshold: >1% for 5m."
          runbook_url: "https://github.com/tipsharks/tipsharks-elo-api/docs/monitoring.md#api-5xx-error-rate--1"
          dashboard_url: "https://grafana.tipsharks.local/d/tipsharks-api"

      - uid: tipsharks_api_p95_latency
        title: "API p95 Latency > 500ms"
        condition: |
          histogram_quantile(
            0.95,
            rate(tipsharks_api_request_duration_seconds_bucket[5m])
          )
          * 1000 > 500
        for: 5m
        labels:
          severity: critical
          team: backend
        annotations:
          summary: "API p95 latency exceeds 500ms for 5 minutes"
          description: "Current p95 latency: {{ $value | humanizeDuration }}. Threshold: >500ms for 5m."
          runbook_url: "https://github.com/tipsharks/tipsharks-elo-api/docs/monitoring.md#api-p95-latency--500ms"
          dashboard_url: "https://grafana.tipsharks.local/d/tipsharks-api"

  - name: tipsharks-api-warning
    interval: 30s
    rules:
      - uid: tipsharks_ingestion_failure_rate
        title: "Ingestion Failure Rate > 5%"
        condition: |
          (
            rate(tipsharks_ingestion_failure_total[10m])
            /
            (rate(tipsharks_ingestion_success_total[10m]) + rate(tipsharks_ingestion_failure_total[10m]))
          )
          * 100 > 5
        for: 10m
        labels:
          severity: warning
          team: backend
        annotations:
          summary: "Ingestion failure rate exceeds 5% for 10 minutes"
          description: "Current failure rate: {{ $value | humanizePercent }}. Threshold: >5% for 10m."
          runbook_url: "https://github.com/tipsharks/tipsharks-elo-api/docs/monitoring.md#ingestion-failure-rate--5"
          dashboard_url: "https://grafana.tipsharks.local/d/tipsharks-api"

      - uid: tipsharks_db_disk_usage
        title: "Database Disk Usage > 80%"
        condition: |
          (
            tipsharks_db_disk_used_bytes
            /
            tipsharks_db_disk_total_bytes
          )
          * 100 > 80
        for: 5m
        labels:
          severity: warning
          team: backend
        annotations:
          summary: "Database disk usage exceeds 80%"
          description: "Current usage: {{ $value | humanizePercent }}. Threshold: >80%. Action: review retention or increase volume."
          runbook_url: "https://github.com/tipsharks/tipsharks-elo-api/docs/monitoring.md#database-disk-usage--80"
          dashboard_url: "https://grafana.tipsharks.local/d/tipsharks-api"

      - uid: tipsharks_api_request_rate_zero
        title: "API Request Rate Dropped to Zero (Service Down)"
        condition: |
          rate(tipsharks_api_requests_total[5m]) == 0
        for: 5m
        labels:
          severity: critical
          team: backend
        annotations:
          summary: "API request rate is zero — service may be down"
          description: "No requests received in the last 5 minutes. Check service health and infrastructure."
          runbook_url: "https://github.com/tipsharks/tipsharks-elo-api/docs/monitoring.md#api-request-rate-drops-to-zero"
          dashboard_url: "https://grafana.tipsharks.local/d/tipsharks-api"

# ---------------------------------------------------------------------------
# Notification Channels (placeholders)
# Configure these in Grafana Alerting UI or via provisioning:
#
#   - Slack:   #tipsharks-alerts
#   - Email:   ops@tipsharks.local
#   - PagerDuty: TipSharks API Integration
#
# Reference:
#   https://grafana.com/docs/grafana/latest/alerting/fundamentals/notifications/
# ---------------------------------------------------------------------------
