Defined Type: prometheus::alert::rule

Defined in:
modules/prometheus/manifests/alert/rule.pp

Summary

resource to configure a Prometheus alerting rule from Puppet. Please note that using alerts.git repository is the preferred way to manage alerts.

Overview

SPDX-License-Identifier: Apache-2.0

Parameters:

  • alert_name (Prometheus::Alert::Name) —

    Name for the alert

  • summary (String[1]) —

    Short alert gist

  • description (String[1]) —

    Alert full description

  • expr (String[1]) —

    Alert condition

  • for (Prometheus::Alert::Duration) —

    The time interval required for an alert condition to trigger an alert

  • group (Prometheus::Alert::Group) —

    The group the alert belongs to

  • dashboard (String[1]) (defaults to: 'TODO') —

    Dashboard url

  • runbook (String[1]) (defaults to: 'TODO') —

    Runbook url

  • logs (String[1]) (defaults to: 'TODO') —

    Logs url

  • team (String[1]) (defaults to: 'sre') —

    the WMF team to alert

  • severity (Prometheus::Alert::Severity) (defaults to: 'warning') —

    The severity of the alert

  • site (Wmflib::Sites) (defaults to: $::site) —

    Deployment site

  • instance (String[1]) —

    The prometheus instance to deploy to

  • def_label_whitelst (Array[String[1]]) (defaults to: [ 'team', 'severity' ])
  • ensure (Wmflib::Ensure) (defaults to: present)


16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'modules/prometheus/manifests/alert/rule.pp', line 16

define prometheus::alert::rule (
    Prometheus::Alert::Name     $alert_name,
    String[1]                   $instance,
    String[1]                   $summary,
    String[1]                   $description,
    String[1]                   $expr,
    Prometheus::Alert::Duration $for,
    Prometheus::Alert::Group    $group,
    String[1]                   $dashboard          = 'TODO',
    String[1]                   $runbook            = 'TODO',
    String[1]                   $logs               = 'TODO',
    String[1]                   $team               = 'sre',
    Prometheus::Alert::Severity $severity           = 'warning',
    Wmflib::Sites               $site               = $::site,  # lint:ignore:top_scope_facts
    Array[String[1]]            $def_label_whitelst = [ 'team', 'severity' ],
    Wmflib::Ensure              $ensure             = present,
) {
    $safe_title = $title.regsubst('\W', '_', 'G')
    $alert_title = "alerts_${instance}_${safe_title}"

    if ($severity == 'page' and $summary !~ /#page/) {
        fail('Paging alerts must contain #page in the summary')
    }

    # Ideally we would validate instance via a type, unfortunately Puppet can
    # not build Enum types dynamically.
    $valid_instances = prometheus::instances().keys
    unless $instance in $valid_instances {
        fail("Invalid Prometheus instance '${instance}'. Must be one of: ${valid_instances.join(', ')}")
    }

    $_alert_config = {
        'alert'  => $alert_name,
        'expr'   => $expr,
        'for'    => $for,
        'annotations' => {
            'description' => $description,
            'summary'     => $summary,
            'dashboard'   => $dashboard,
            'logs'        => $logs,
            'runbook'     => $runbook,
        },
    }

    $def_labels = {
        'team'     => $team,
        'severity' => $severity
    }

    $labels = {
        'labels' => $def_labels.reduce({}) |$memo, $label| {
            if $label[0] in $def_label_whitelst {
                $memo + $label
            } else {
                $memo
            }
        }
    }

    $alert_config = length($labels['labels'].keys) > 0 ? {
        true  => $_alert_config + $labels,
        false => $_alert_config
    }


    $alert_rule_params  = {
        'instance' => $instance,
        'config'   => $alert_config,
        'group'    => $group,
        'tag'      => "prometheus::alert::rule::${site}::${instance}",
    }

    if $ensure == 'present' {
        wmflib::resource::export('prometheus::alert::placeholder', $alert_title, $title, $alert_rule_params)
    }
}