rule_files:
  - alerts.yml

evaluation_interval: 1m

tests:
  - name: memory budget critical after sustained pressure
    interval: 1m
    input_series:
      - series: 'pg_local_cache_estimated_memory_bytes{instance="postgres-exporter:9187",job="pg-local-cache"}'
        values: '96+0x10'
      - series: 'pg_local_cache_memory_budget_bytes{instance="postgres-exporter:9187",job="pg-local-cache"}'
        values: '100+0x10'
    alert_rule_test:
      - eval_time: 6m
        alertname: PgLocalCacheMemoryBudgetCritical
        exp_alerts:
          - exp_labels:
              component: pg_local_cache
              instance: postgres-exporter:9187
              job: pg-local-cache
              severity: critical
            exp_annotations:
              summary: pg_local_cache memory budget is almost exhausted
              description: The deterministic extension memory estimate exceeds 95% of its startup budget.

  - name: missing workers become critical
    interval: 1m
    input_series:
      - series: 'pg_local_cache_workers_running{instance="postgres-exporter:9187",job="pg-local-cache"}'
        values: '7+0x10'
      - series: 'pg_local_cache_workers_configured{instance="postgres-exporter:9187",job="pg-local-cache"}'
        values: '8+0x10'
    alert_rule_test:
      - eval_time: 3m
        alertname: PgLocalCacheWorkersMissing
        exp_alerts:
          - exp_labels:
              component: pg_local_cache
              instance: postgres-exporter:9187
              job: pg-local-cache
              severity: critical
            exp_annotations:
              summary: One or more pg_local_cache workers are missing
              description: The number of running RESP workers is below the configured worker count.

  - name: client limit rejection is observable
    interval: 1m
    input_series:
      - series: 'pg_local_cache_client_limit_rejections_total{instance="postgres-exporter:9187",job="pg-local-cache"}'
        values: '0 0 1+0x10'
    alert_rule_test:
      - eval_time: 4m
        alertname: PgLocalCacheClientLimitRejections
        exp_alerts:
          - exp_labels:
              component: pg_local_cache
              instance: postgres-exporter:9187
              job: pg-local-cache
              severity: warning
            exp_annotations:
              summary: pg_local_cache rejected clients at the global limit
              description: At least one RESP connection was rejected by the OOM safety limit in five minutes.

  - name: incomplete mappings fail readiness monitoring
    interval: 1m
    input_series:
      - series: 'pg_local_cache_workers_with_incomplete_mappings{instance="postgres-exporter:9187",job="pg-local-cache"}'
        values: '1+0x10'
    alert_rule_test:
      - eval_time: 2m
        alertname: PgLocalCacheMappingsIncomplete
        exp_alerts:
          - exp_labels:
              component: pg_local_cache
              instance: postgres-exporter:9187
              job: pg-local-cache
              severity: critical
            exp_annotations:
              summary: pg_local_cache mappings are not current on every worker
              description: At least one RESP worker has not loaded the current complete table-mapping generation.

  - name: cAdvisor OOM counter pages immediately
    interval: 1m
    input_series:
      - series: 'container_oom_events_total{instance="cadvisor:8080",job="cadvisor",name="pg_local_cache-postgres-1"}'
        values: '0 0 1+0x12'
    alert_rule_test:
      - eval_time: 5m
        alertname: PgLocalCacheContainerOOMKilled
        exp_alerts:
          - exp_labels:
              component: pg_local_cache
              instance: cadvisor:8080
              job: cadvisor
              name: pg_local_cache-postgres-1
              severity: critical
            exp_annotations:
              summary: pg_local_cache PostgreSQL container had an OOM event
              description: cAdvisor reported an OOM event for the PostgreSQL container in the last ten minutes.
