规则的单元测试

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

  1. # 单独测试一个文件
  2. ./promtool test rules test.yml
  3. # 如果你有多个测试文件, test1.yml,test2.yml,test2.yml
  4. ./promtool test rules test1.yml test2.yml test3.yml
' class="reference-link">

测试文件格式

  1. # 要进行测试的规则文件的列表。支持globs。
  2. rule_files:
  3. [ - <file_name> ]
  4. # 可选的, default = 1m
  5. evaluation_interval: <duration>
  6. # 下面列出的组名的顺序僵尸规则组的执行顺序(在给定的执行时间内)。仅针对以下提及的组保证顺序。不需要体积所有组
  7. group_eval_order:
  8. [ - <group_name> ]
  9. # 所有测试均在此处列出
  10. tests:
  11. [ - <test_group> ]
' class="reference-link">

<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> ]
  12. # 告警模版可访问的外部扩展标签
  13. external_labels:
  14. [ <labelname>: <string> ... ]
' class="reference-link">

<series>

  1. # 遵循常规的序列符号 '<metric name>{<label name>=<label value>, ...}'
  2. # Examples:
  3. # series_name{label1="value1", label2="value2"}
  4. # go_goroutines{job="prometheus", instance="localhost:9090"}
  5. series: <string>
  6. # 使用扩展符号
  7. # Expanding notation:
  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. # Examples:
  11. # 1. '-2+4x3' becomes '-2 2 6 10'
  12. # 2. ' 1-2x4' becomes '1 -1 -3 -5 -7'
  13. values: <string>
' class="reference-link">

<alert_test_case>

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

  1. # 检查告警的时间间隔
  2. eval_time: <duration>
  3. # 要测试的告警名称
  4. alertname: <string>
  5. # 给定执行时间以给定警报名称触发的预期告警列表。 如果要测试是否不应该触发警报规则,则可以提及上述字段,并将 "exp_alerts" 留空。
  6. exp_alerts:
  7. [ - <alert> ]
' class="reference-link">

<alert>

  1. # 预期告警的扩展标签和注解.
  2. # 注意: 标签包括与告警关联的样本的标签(与您在 `/alerts` 中看到的标签相同,但不包含 `__name__` 和 `alertname` 序列)
  3. exp_labels:
  4. [ <labelname>: <string> ]
  5. exp_annotations:
  6. [ <labelname>: <string> ]
  1. # 执行的表达式
  2. expr: <string>
  3. # 执行表达式的时间间隔
  4. eval_time: <duration>
  5. # 给定评估时间的预期样本
  6. exp_samples:
  7. [ - <sample> ]
' class="reference-link">

<sample>

  1. # 数据样本的标签采用常规系列标记 '<metric name>{<label name>=<label value>, ...}'
  2. # Examples:
  3. # series_name{label1="value1", label2="value2"}
  4. # go_goroutines{job="prometheus", instance="localhost:9090"}
  5. labels: <string>
  6. # PromQL 表达式的期望值.
  7. value: <number>
' class="reference-link">

示例

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

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

test.yml

  1. # 这是单元测试的入口
  2. # 仅将此文件作为命令行参数传递
  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

alert.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."