groups:
  # Sample alert rules. Every one needs a lasting condition, so a single missed push,
  # retry or pause never fires them. Thresholds are starting points.
  - name: chronos-customer
    rules:
      # Customer-facing: waits for 3 hours so a locked screen, a long meeting or a laptop that is
      # awake but idle never shows as a problem. Two limits: it also fires on a device that runs with
      # vision disabled (no video is saved, so it never clears; filter those devices out), and encoded
      # seconds are summed across monitors, so a device with N monitors needs about N times less
      # coverage per screen to stay silent.
      - alert: ChronosNoRecentRecording
        expr: |
          (sum by (instance) (increase(chronos_recording_expected_duration_seconds_total[3h])) > 9000)
          unless on (instance)
          (sum by (instance) (increase(chronos_video_chunk_encoded_duration_seconds_total[3h]))
             >= 0.2 * sum by (instance) (increase(chronos_recording_expected_duration_seconds_total[3h])))
        for: 15m
        labels: { severity: warning, audience: customer }
        annotations:
          summary: "recording was expected on {{ $labels.instance }} for the last 3 hours but almost no video was saved"
          description: "Check the device status: it may be paused or the recorder may be failing. Restart Chronos on the device, and contact support if it continues."

      # The last-upload time starts at the recorder's start time, so a device that restarts more
      # often than every 6 hours never fires this, however long its uploads have been failing.
      - alert: ChronosUploadsDelayed
        expr: |
          ((time() - max by (instance) (chronos_upload_last_success_seconds{kind="video"})) > 21600)
          and on (instance) ((time() - max by (instance) (chronos_video_chunk_last_success_seconds)) < 3600)
          and on (instance) (count_over_time(chronos_up[2h]) >= 90)
        for: 15m
        labels: { severity: warning, audience: customer }
        annotations:
          summary: "{{ $labels.instance }} is online but no recording has been delivered to storage for over 6 hours"
          description: "Check the device network access and the storage credentials. Delivery is retried automatically once it works again."

      - alert: ChronosDiskLow
        expr: |
          (max_over_time(chronos_disk_free_bytes[30m]) < 5e9)
          and (count_over_time(chronos_disk_free_bytes[30m]) >= 25)
        for: 10m
        labels: { severity: warning, audience: customer }
        annotations:
          summary: "{{ $labels.instance }} has had under 5 GB of free disk space for over 30 minutes"
          description: "Free up disk space on the device. Recording stops when the disk is nearly full."

      - alert: ChronosDiskVeryLow
        expr: |
          (max_over_time(chronos_disk_free_bytes[15m]) < 1e9)
          and (count_over_time(chronos_disk_free_bytes[15m]) >= 12)
        for: 10m
        labels: { severity: critical, audience: customer }
        annotations:
          summary: "{{ $labels.instance }} has under 1 GB of free disk space, recording is about to stop"
          description: "Free up disk space on the device now, or recording stops."

      - alert: ChronosRecorderUnresponsive
        expr: min_over_time(chronos_runtime_stalled[10m]) == 1
        for: 5m
        labels: { severity: critical, audience: customer }
        annotations:
          summary: "the recorder on {{ $labels.instance }} has not responded for 15 minutes"
          description: "Restart Chronos on the device, and contact support if it happens again."

      - alert: ChronosRepeatedRestarts
        expr: changes(chronos_process_start_time_seconds[1h]) > 4
        for: 5m
        labels: { severity: warning, audience: customer }
        annotations:
          summary: "the recorder on {{ $labels.instance }} restarted more than 4 times in the last hour"
          description: "Look for crashes or something repeatedly restarting Chronos on the device, and contact support with its logs if it continues."
