您可以使用promtool来测试您的规则。

  1. # 单个测试文件
  2. ./promtool test rules test.yml
  3. # 多个测试文件
  4. ./promtool test rules test1.yml test2.yml test3.yml
一、测试文件格式
  1. # 这是要考虑进行测试的规则文件列表。
  2. rule_files:
  3. [ - <file_name> ]
  4. # 可选的, 默认 = 1m
  5. evaluation_interval: <duration>
  6. # 下面列出组名称的顺序将是规则组的评估顺序(在给定的评估时间)。 订单仅适用于下面提到的组。
  7. # 下面不需要提到所有组。
  8. group_eval_order:
  9. [ - <group_name> ]
  10. # 所有测试都列在这里。
  11. tests:
  12. [ - <test_group> ]
1.1 <test_group>
  1. # 系列数据
  2. interval: <duration>
  3. input_series:
  4. [ - <series> ]
  5. # 上述数据的单元测试
  6. # 警报规则的单元测试。 我们从输入文件中考虑警报规则。
  7. alert_rule_test:
  8. [ - <alert_test_case> ]
  9. # 单元测试PromQL表达式。
  10. promql_expr_test:
  11. [ - <promql_test_case> ]
1.2 <series>
  1. # 这遵循通常的系列符号 '<metric name>{<label name>=<label value>, ...}'
  2. # 例子:
  3. # series_name{label1="value1", label2="value2"}
  4. # go_goroutines{job="prometheus", instance="localhost:9090"}
  5. series: <string>
  6. # 这使用扩展表示法。
  7. # 扩展符号:
  8. # 'a+bxc' becomes 'a a+b a+(2*b) a+(3*b) … a+(c*b)'
  9. # 'a-bxc' becomes 'a a-b a-(2*b) a-(3*b) … a-(c*b)'
  10. # 例子:
  11. # 1. '-2+4x3' becomes '-2 2 6 10'
  12. # 2. ' 1-2x4' becomes '1 -1 -3 -5 -7'
  13. values: <string>
1.3 <alert_test_case>

Prometheus允许您为不同的警报规则使用相同的警报名称。 因此,在此单元测试中,您必须在单个<alert_test_case>下列出alertname的所有触发警报的并集。

  1. # 这是从必须检查警报的时间= 0开始经过的时间。
  2. eval_time: <duration>
  3. # 要测试的警报的名称。
  4. alertname: <string>
  5. # 在给定评估时间在给定警报名称下触发的预期警报列表。 如果您想测试是否不应该触发警报规则,那么您可以提及上述字段并将“exp_alerts”留空。
  6. exp_alerts:
  7. [ - <alert> ]
1.4 <alert>
  1. # 这些是预期警报的扩展标签和注释。
  2. # 注意:标签还包括与警报关联的样本标签(与您在`/alerts`中看到的标签相同,没有系列`__name__`和`alertname`)
  3. exp_labels:
  4. [ <labelname>: <string> ]
  5. exp_annotations:
  6. [ <labelname>: <string> ]
1.5 <promql_test_case>
  1. # 表达评估
  2. expr: <string>
  3. # 这是从必须检查警报的时间= 0开始经过的时间。
  4. eval_time: <duration>
  5. # 在给定评估时间的预期样品。
  6. exp_samples:
  7. [ - <sample> ]
1.6 <sample>
  1. # 通常系列表示法中的样本标签 '<metric name>{<label name>=<label value>, ...}'
  2. # 例子:
  3. # series_name{label1="value1", label2="value2"}
  4. # go_goroutines{job="prometheus", instance="localhost:9090"}
  5. labels: <string>
  6. # promql表达式的期望值。
  7. value: <number>
二、例子

这是用于通过测试的单元测试的示例输入文件。 test.yml是遵循上述语法的测试文件,alerts.yml包含警报规则。

alerts.yml放在同一目录中,运行./promtool test rules test.yml。

2.1 <test.yml>
  1. # This is the main input for unit testing.
  2. # Only this file is passed as command line argument.
  3. rule_files:
  4. - alerts.yml
  5. evaluation_interval: 1m
  6. tests:
  7. # Test 1.
  8. - interval: 1m
  9. # Series data.
  10. input_series:
  11. - series: 'up{job="prometheus", instance="localhost:9090"}'
  12. values: '0 0 0 0 0 0 0 0 0 0 0 0 0 0 0'
  13. - series: 'up{job="node_exporter", instance="localhost:9100"}'
  14. values: '1+0x6 0 0 0 0 0 0 0 0' # 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0
  15. - series: 'go_goroutines{job="prometheus", instance="localhost:9090"}'
  16. values: '10+10x2 30+20x5' # 10 20 30 30 50 70 90 110 130
  17. - series: 'go_goroutines{job="node_exporter", instance="localhost:9100"}'
  18. values: '10+10x7 10+30x4' # 10 20 30 40 50 60 70 80 10 40 70 100 130
  19. # Unit test for alerting rules.
  20. alert_rule_test:
  21. # Unit test 1.
  22. - eval_time: 10m
  23. alertname: InstanceDown
  24. exp_alerts:
  25. # Alert 1.
  26. - exp_labels:
  27. severity: page
  28. instance: localhost:9090
  29. job: prometheus
  30. exp_annotations:
  31. summary: "Instance localhost:9090 down"
  32. description: "localhost:9090 of job prometheus has been down for more than 5 minutes."
  33. # Unit tests for promql expressions.
  34. promql_expr_test:
  35. # Unit test 1.
  36. - expr: go_goroutines > 5
  37. eval_time: 4m
  38. exp_samples:
  39. # Sample 1.
  40. - labels: 'go_goroutines{job="prometheus",instance="localhost:9090"}'
  41. value: 50
  42. # Sample 2.
  43. - labels: 'go_goroutines{job="node_exporter",instance="localhost:9100"}'
  44. value: 50
2.2 <alerts.yml>
  1. # This is the rules file.
  2. groups:
  3. - name: example
  4. rules:
  5. - alert: InstanceDown
  6. expr: up == 0
  7. for: 5m
  8. labels:
  9. severity: page
  10. annotations:
  11. summary: "Instance {{ $labels.instance }} down"
  12. description: "{{ $labels.instance }} of job {{ $labels.job }} has been down for more than 5 minutes."
  13. - alert: AnotherInstanceDown
  14. expr: up == 0
  15. for: 10m
  16. labels:
  17. severity: page
  18. annotations:
  19. summary: "Instance {{ $labels.instance }} down"
  20. description: "{{ $labels.instance }} of job {{ $labels.job }} has been down for more than 5 minutes."