cloudflare/pint
Publicmirrored from https://github.com/cloudflare/pintAvailable
docs/checks/promql/selector.md
181lines · modecode
| 1 | --- |
| 2 | layout: default |
| 3 | parent: Checks |
| 4 | grand_parent: Documentation |
| 5 | --- |
| 6 | |
| 7 | # promql/selector |
| 8 | |
| 9 | This check is used to enforce rules on how vector selectors must be structured. |
| 10 | |
| 11 | ## Configuration |
| 12 | |
| 13 | Syntax: |
| 14 | |
| 15 | ```js |
| 16 | selector "$pattern" { |
| 17 | comment = "..." |
| 18 | severity = "bug|warning|info" |
| 19 | requiredLabels = [ "...", ... ] |
| 20 | } |
| 21 | ``` |
| 22 | |
| 23 | - `$pattern` - regexp pattern to match time series name used in the vector selector, |
| 24 | this can be templated to reference checked rule fields, see [Configuration](../../configuration.md) |
| 25 | for details. |
| 26 | - `comment` - set a custom comment that will be added to reported problems. |
| 27 | - `severity` - set custom severity for reported issues, defaults to a warning. |
| 28 | - `requiredLabels` - list of label names that must be present on all matching vector selectors. |
| 29 | |
| 30 | The `selector` block can be optionally wrapped inside a `call` block to restrict this check |
| 31 | only to selectors used inside specific function, syntax: |
| 32 | |
| 33 | ```js |
| 34 | call "$pattern" { |
| 35 | selector { ... } |
| 36 | } |
| 37 | ``` |
| 38 | |
| 39 | - `$pattern` - regexp pattern to match the name of the function, |
| 40 | this can be templated to reference checked rule fields, see [Configuration](../../configuration.md) |
| 41 | for details. |
| 42 | |
| 43 | ## How to enable it |
| 44 | |
| 45 | This check is not enabled by default as it requires explicit configuration to work. |
| 46 | To enable it add one or more `rule {...}` blocks and specify all required rules there. |
| 47 | |
| 48 | Examples: |
| 49 | |
| 50 | Ensure that all alerting rules are scoped to specific scrape job we can enforce that they |
| 51 | all have a `job` label filter: |
| 52 | |
| 53 | ```js |
| 54 | rule { |
| 55 | match { |
| 56 | kind = "alerting" |
| 57 | } |
| 58 | selector ".+" { |
| 59 | requiredLabels = ["job"] |
| 60 | comment = "All alerts must be scoped to a specific scrape job via the `job` label." |
| 61 | } |
| 62 | } |
| 63 | ``` |
| 64 | |
| 65 | The above rule would flag this rule: |
| 66 | |
| 67 | ```yaml |
| 68 | - alert: TargetDown |
| 69 | expr: up == 0 |
| 70 | ``` |
| 71 | |
| 72 | but this one would pass: |
| 73 | |
| 74 | ```yaml |
| 75 | - alert: TargetDown |
| 76 | expr: up{job="myjob"} |
| 77 | ``` |
| 78 | |
| 79 | We can also restrict this check only to some metrics, for example only to the `up` metric, by |
| 80 | changing the regexp pattern in the `selector` block from `.+` to `up`: |
| 81 | |
| 82 | ```js |
| 83 | rule { |
| 84 | selector "up" { ... } |
| 85 | } |
| 86 | ``` |
| 87 | |
| 88 | The `selector` block can be nested under the `call` block if we want to apply it only to selectors |
| 89 | used inside some specific function. To ensure that all alerts using `absent()` and `absent_over_time()` |
| 90 | call have the `job` label: |
| 91 | |
| 92 | ```js |
| 93 | rule { |
| 94 | match { |
| 95 | kind = "alerting" |
| 96 | } |
| 97 | call "absent|absent_over_time" { |
| 98 | selector ".+" { |
| 99 | requiredLabels = ["job"] |
| 100 | comment = "All alerts using absent() must be specify the `job` label." |
| 101 | } |
| 102 | } |
| 103 | } |
| 104 | ``` |
| 105 | |
| 106 | ## How to disable it |
| 107 | |
| 108 | You can disable this check globally by adding this config block: |
| 109 | |
| 110 | ```js |
| 111 | checks { |
| 112 | disabled = ["promql/selector"] |
| 113 | } |
| 114 | ``` |
| 115 | |
| 116 | You can also disable it for all rules inside given file by adding |
| 117 | a comment anywhere in that file. Example: |
| 118 | |
| 119 | ```yaml |
| 120 | # pint file/disable promql/selector |
| 121 | ``` |
| 122 | |
| 123 | Or you can disable it per rule by adding a comment to it. Example: |
| 124 | |
| 125 | ```yaml |
| 126 | # pint disable promql/selector |
| 127 | ``` |
| 128 | |
| 129 | If you want to disable only individual instances of this check |
| 130 | you can add a more specific comment. |
| 131 | |
| 132 | ### If you only have the `selector` config block |
| 133 | |
| 134 | ```yaml |
| 135 | # pint disable promql/selector($pattern:$label) |
| 136 | ``` |
| 137 | |
| 138 | Example: |
| 139 | |
| 140 | ```js |
| 141 | selector "up" { |
| 142 | requiredLabels = ["job"] |
| 143 | } |
| 144 | ``` |
| 145 | |
| 146 | ```yaml |
| 147 | # pint disable promql/selector(^up$:job) |
| 148 | ``` |
| 149 | |
| 150 | ### If you have the `call` config block |
| 151 | |
| 152 | ```yaml |
| 153 | # pint disable promql/selector($callPattern:$selectorPattern:$label) |
| 154 | ``` |
| 155 | |
| 156 | Example: |
| 157 | |
| 158 | ```js |
| 159 | call "absent" { |
| 160 | selector "up" { |
| 161 | requiredLabels = ["job"] |
| 162 | } |
| 163 | } |
| 164 | ``` |
| 165 | |
| 166 | ```yaml |
| 167 | # pint disable promql/selector(^absent$:^up$:job) |
| 168 | ``` |
| 169 | |
| 170 | ## How to snooze it |
| 171 | |
| 172 | You can disable this check until given time by adding a comment to it. Example: |
| 173 | |
| 174 | ```yaml |
| 175 | # pint snooze $TIMESTAMP promql/selector |
| 176 | ``` |
| 177 | |
| 178 | Where `$TIMESTAMP` is either use [RFC3339](https://www.rfc-editor.org/rfc/rfc3339) |
| 179 | formatted or `YYYY-MM-DD`. |
| 180 | Adding this comment will disable `promql/selector` *until* `$TIMESTAMP`, after that |
| 181 | check will be re-enabled. |