Skip to content

Commit ce46d39

Browse files
authored
Merge pull request #1613 from cloudflare/issue-1572
Add a note about yaml/parse strictness
2 parents 6f43db4 + 12666b3 commit ce46d39

File tree

1 file changed

+44
-2
lines changed

1 file changed

+44
-2
lines changed

docs/checks/yaml/parse.md

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,59 @@ grand_parent: Documentation
88

99
You will only ever see this check reporting problems if a file containing
1010
Prometheus rules to check doesn't parse as valid [YAML](https://yaml.org/),
11-
meaning that pint is unable to read any rules from that file.
11+
meaning that pint is unable to read any rules from that file or when
12+
some fields are using the wrong type.
1213

1314
This includes basic YAML parser checks but will also fail if a rule
14-
block contains duplicated keys, example:
15+
block contains duplicate keys, example:
1516

1617
```yaml
1718
- record: foo
1819
expr: sum(my_metric)
1920
expr: sum(my_metric) without(instance)
2021
```
2122
23+
Syntax checks enforced by pint are more strict than what Prometheus uses,
24+
so a rule definition that fails pint checks might still be parsed by
25+
Prometheus. This is because pint enforces that all fields have the correct type.
26+
For example all annotations are expected to be strings, but the YAML parser
27+
will load any value that can be represented as a string, for example a number:
28+
29+
```yaml
30+
- alert: Foo
31+
expr: up == 0
32+
annotations:
33+
priotity: 1
34+
```
35+
36+
The above rule will work in Prometheus but, for example, if you try to parse
37+
such file using Python to find all rules where `priority` is `"1"` it will skip it,
38+
because Python doesn't know the schema of rule file, so it returns whatever types
39+
it finds:
40+
41+
```python
42+
import yaml
43+
44+
with open("rules.yaml") as f:
45+
for rule in yaml.safe_load(f):
46+
if rule["annotations"]["priority"] == "1":
47+
...
48+
```
49+
50+
This kind of type confusion can be even more problematic because YAML will
51+
automatically convert certain string values to boolean, for example:
52+
53+
```yaml
54+
- alert: Foo
55+
expr: up == 0
56+
annotations:
57+
critical: no
58+
```
59+
60+
In the above YAML will parse `no` as `false`.
61+
There are other well known gotchas that are caused by YAML complex parsing rules
62+
and the best way to avoid these is to always use explicit types for string.
63+
2264
## Configuration
2365

2466
This check doesn't have any configuration options.

0 commit comments

Comments
 (0)