4

I would like to use the conditional operator into the Prometheus alert.rules definition to set a specific severity. For instance, if the environment is production, I want to set the severity to critical else another value.

Something like:

- alert: CPU load
  expr: expression_used_to_check_load
  for: 15m
  labels:
     severity: if $labels.env == prd -> severity = critical else something else
  annotations:
     summary: Just a summary
     description: "Just a description"
rschirin
  • 1,939
  • 10
  • 34
  • 44

3 Answers3

0

You could divide the rule in two:

- alert: CPU load
  expr: expression_used_to_check_load and {env=="prd"}
  for: 15m
  labels:
    severity: critical
  annotations:
    summary: Just a summary
    description: "Just a description"
- alert: CPU load
  expr: expression_used_to_check_load and {env!="prd"}
  for: 15m
  labels:
    severity: warning
  annotations:
    summary: Just a summary
    description: "Just a description"
Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109
0

Rather than configuring this in the severity itself, you can configure the receiver of your pages differently based on environment.

- match: {owner: 'MYTEAM'}
    receiver: 'blackhole'
    routes:
    - match: {severity: 'critical', env: 'staging'}
      receiver: 'myteam-warn'
    - match: {severity: 'critical', env: 'prd'}}
      receiver: 'myteam-page'

The different receivers can be configured differently. myteam-warn might just send you an email whereas myteam-page might trigger a page.

Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109
0

You can customize the severity this way.

- alert: KubernetesStatefulsetReplicasMismatch
    expr: 'kube_statefulset_status_replicas_ready != kube_statefulset_status_replicas'
    for: 3s
    labels:
      severity: "{{ if eq .Labels.cluster \"prod\" }}critical{{ else if eq .Labels.cluster \"dev\" }}warning{{ end }}"
    annotations:
      summary: Kubernetes StatefulSet replicas mismatch (cluster {{ $labels.cluster }})
      description: "A StatefulSet does not match the expected number of replicas.\n  VALUE = {{ $value }}\n  LABELS = {{ $labels }}"