# Prometheus alerting rules for HyperCloud. See docs/metrics.md.
#
# Load with `rule_files: [hypercloud-alerts.yml]` in prometheus.yml. The job name
# in the first rule assumes the scrape config from the same page; adjust it if
# yours differs. Thresholds are starting points, not gospel.
groups:
  - name: HyperCloud
    rules:
      - alert: HypercloudDown
        expr: up{job="hypercloud"} == 0
        for: 5m
        labels:
          severity: critical
        annotations:
          summary: HyperCloud is not answering scrapes
          description: Prometheus has not reached {{ $labels.instance }} for 5 minutes.

      - alert: HypercloudReminderSchedulerStalled
        # The scheduler ticks every 30 s. Absent entirely when VAPID keys are
        # not set, since then there is no scheduler to stall.
        expr: time() - hypercloud_reminder_scheduler_last_tick_timestamp_seconds > 300
        for: 2m
        labels:
          severity: critical
        annotations:
          summary: Reminder scheduler has not completed a pass in 5 minutes
          description: >-
            No reminders are being sent. Check the log for "reminder tick
            failed".

      - alert: HypercloudReminderLagHigh
        expr: |
          histogram_quantile(0.95,
            sum by (le) (rate(hypercloud_reminder_scheduler_lag_seconds_bucket[30m]))
          ) > 120
        for: 10m
        labels:
          severity: warning
        annotations:
          summary: Reminders are going out more than 2 minutes late
          description: 95th percentile reminder lag is {{ $value | humanizeDuration }}.

      - alert: HypercloudReminderDeliveryFailing
        expr: |
          sum(rate(hypercloud_reminders_sent_total{status="error"}[30m]))
            / sum(rate(hypercloud_reminders_sent_total[30m])) > 0.2
        for: 30m
        labels:
          severity: warning
        annotations:
          summary: More than 20% of reminder pushes are failing
          description: >-
            The push service is refusing deliveries. Check the log for "push
            send failed".

      - alert: HypercloudSyncLinkFailing
        expr: hypercloud_sync_links{state="failing"} > 0
        for: 6h
        labels:
          severity: warning
        annotations:
          summary: "{{ $value }} {{ $labels.kind }} link(s) failing"
          description: >-
            Their last sync ended in an error for 6 hours. The account's
            calendar list shows which one and why.

      - alert: HypercloudSyncStale
        expr: time() - hypercloud_sync_oldest_success_timestamp_seconds > 3 * 86400
        for: 1h
        labels:
          severity: warning
        annotations:
          summary: A {{ $labels.kind }} link has not synced successfully in 3 days

      - alert: HypercloudBackupStale
        # Both backups are daily; 26 h allows for the schedule drifting.
        expr: time() - hypercloud_backup_last_success_timestamp_seconds > 26 * 3600
        for: 15m
        labels:
          severity: critical
        annotations:
          summary: No successful {{ $labels.kind }} backup in over a day

      - alert: HypercloudBackupFailed
        expr: increase(hypercloud_backup_failures_total[1d]) > 0
        labels:
          severity: warning
        annotations:
          summary: A {{ $labels.kind }} backup failed in the last day

      - alert: HypercloudHighErrorRate
        expr: |
          sum(rate(hypercloud_http_requests_total{status=~"5.."}[5m]))
            / sum(rate(hypercloud_http_requests_total[5m])) > 0.05
        for: 10m
        labels:
          severity: warning
        annotations:
          summary: More than 5% of requests are failing with a 5xx

      - alert: HypercloudMemoryNearLimit
        # Only where the server runs with a memory limit (a container).
        expr: process_resident_memory_bytes / hypercloud_memory_limit_bytes > 0.9
        for: 10m
        labels:
          severity: warning
        annotations:
          summary: The server uses over 90% of its memory limit
          description: >-
            Past the limit the container is killed. Raise mem_limit, or see
            what holds memory under "Memory and load" in the host docs.

      - alert: HypercloudRequestsRefused
        expr: sum by (reason) (rate(hypercloud_requests_refused_total[5m])) > 0.1
        for: 10m
        labels:
          severity: warning
        annotations:
          summary: Requests are being turned away ({{ $labels.reason }})
          description: >-
            busy: more than MAX_REQUESTS at once. memory: bodies waiting for
            the memory budget. timeout: requests past REQUEST_TIMEOUT. Raise
            the limit in question, or the container's memory.

      - alert: HypercloudRestarting
        expr: changes(process_start_time_seconds[1h]) > 2
        labels:
          severity: critical
        annotations:
          summary: The server restarted more than twice in an hour
          description: >-
            Most often killed for memory: docker inspect shows OOMKilled.
