我希望使用promtool
来针对我设置的警报运行单元测试。
promtool test rules alert-test.yaml
字符串
下面是一个示例测试文件:
# alert-test.yaml
rule_files:
- 'my-alert.yaml'
tests:
- name: 'Fire ManyRequests Alert'
interval: 1s # every second
input_series:
- series: rate(http_requests_total[5m])
values: '0+1x30' # starting at 0 requests per second increase to 30 requests per second
alert_rule_test:
- alertname: ManyRequests
eval_time: 10s
exp_alerts: # no alert
- alertname: ManyRequests
eval_time: 25s
exp_alerts:
- exp_labels:
severity: p2
exp_annotations:
description: Request increase too high
型
这里没什么新鲜事。如果my-alert.yaml
看起来像这样,这将是没有问题的:
groups:
- name: alert_group_name
rules:
- alert: ManyRequests
expr: rate(http_requests_total[5m]) > 20
for: 5s
annotations:
description: Request increase too high
labels:
severity: P2
型
但是我有一个正在部署的Kubernetes清单,即
my-alert.yaml
apiVersion: monitoring.coreos.com/v1
kind: PrometheusRule
metadata:
labels:
prometheus: prometheus-core-rules
role: alert-rules
name: tesla-maps-cert-alert-rules
spec:
groups:
- name: alert_group_name
rules:
- alert: ManyRequests
expr: rate(http_requests_total[5m]) > 20
for: 5s
annotations:
description: Request increase too high
labels:
severity: P2
型
所以运行promtool test rules alert-test.yaml
会抛出这个错误:
Unit Testing: alerts-test.yaml
FAILED:
my-alert.yaml: yaml: unmarshal errors:
line 1: field apiVersion not found in type rulefmt.RuleGroups
line 2: field kind not found in type rulefmt.RuleGroups
line 3: field metadata not found in type rulefmt.RuleGroups
line 8: field spec not found in type rulefmt.RuleGroups
型
要怎么做才能使这个工作?
1条答案
按热度按时间piv4azn71#
要怎么做才能使这个工作?
只需提取YAML清单的相关部分:
字符串
并修改测试文件以引用提取的文件:
型
我使用的是this yq,但the other one应该也能正常工作。