Prometheus:How to run unit tests on PrometheusRule kubernetes manifest

oxf4rvwz  于 2023-08-03  发布在  Kubernetes
关注(0)|答案(1)|浏览(101)

我希望使用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


要怎么做才能使这个工作?

piv4azn7

piv4azn71#

要怎么做才能使这个工作?
只需提取YAML清单的相关部分:

yq .spec my-alert.yaml > my-alert-for-testing.yaml

字符串
并修改测试文件以引用提取的文件:

rules_files:
  - my-alert-for-testing.yaml


我使用的是this yq,但the other one应该也能正常工作。

相关问题