• Stars
    star
    156
  • Rank 239,589 (Top 5 %)
  • Language
    Go
  • License
    Apache License 2.0
  • Created over 6 years ago
  • Updated almost 6 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Prometheus Alert Testing utility
 ____   _  _____ 
|  _ \ / \|_   _|
| |_) / _ \ | |  
|  __/ ___ \| |  
|_| /_/   \_\_|  
                 

Prometheus Alert Testing tool

CircleCI

You may also be interested in PromCLI

Build & Install

go get github.com/kevinjqiu/pat

You must have golang 1.9+ and dep installed.

Build from source

Check out this repo to $GOPATH/src/github.com/kevinjqiu/pat

and then:

cd $GOPATH/src/github.com/kevinjqiu/pat && make build

Usage

pat [options] <test_yaml_file_glob>

e.g.,

pat test/*.yaml

Test File Format

Test files are written in yaml format. For a complete schema definition (in jsonschema format), see here.

Top level attributes

  • name - The name of the test case
  • rules - The rule definitions that are under test
  • fixtures - The fixture setup for the tests
  • assertions - The test assertions

Rules

The rules section defines how the rules-under-test should be loaded. Currently, two rules loading strategies are supported:

  • fromFile - load the rules from a .rules yaml file. If the path specified is not an absolute path, the rule file path will be relative to the test file.
  • fromLiteral - embed the rules under test right inside the test file.

Example

rules:
  fromFile: http-rules.yaml

or

rules:
  fromLiteral: |-
    groups:
      - name: prometheus.rules
        rules:
          - alert: HTTPRequestRateLow
            expr: http_requests{group="canary", job="app-server"} < 100
            for: 1m
            labels:
              severity: critical

Fixtures

The fixtures section defines a list of metrics fixtures that the tests will be using. Each item in the list has the following attributes:

  • duration - How long these metrics will be set to the specified value. The duration must be acceptable by Golang's time.ParseDuration(), e.g., 5m (5 minutes), 1h (1 hour), etc.
  • metrics - The metrics and their values

Example

fixtures:
  5m:
    - http_requests{job="app-server", instance="0", group="blue"}	75
    - http_requests{job="app-server", instance="1", group="blue"}	120

This will create these two metrics, with the values last for 5 minutes.

You are also able to specify multiple metrics values:

  5m:
    - http_requests{job="app-server", instance="0", group="blue"}	75 100 200

In this case, the metric http_requests{job="app-server", instance="0", group="blue"} will be set to 75 for the first 5 minutes, 100 for the next 5 minutes and 200 for the next 5 minutes. You can use this form to easily setup long running time series.

Assertions

The assertions section contains a list of expectations when the alert rules are evaluated at certain time.

  • at - The instant when the rules are being evaluated
  • expected - The list of expected alert properties

Example

assertions:
  - at: 0m
    expected:
      - alertname: HTTPRequestRateLow
        alertstate: pending
        job: app-server
        severity: critical
  - at: 5m
    expected:
      - alertname: HTTPRequestRateLow
        alertstate: firing
        job: app-server
        severity: critical
  - at: 10m
    expected: []

In this example, we're asserting that when the alert rules are evaluated at 0m, with the given fixtures, we should get HTTPRequestRateLow alert in pending state, and when evaluated at 5m, the alert should be in firing state. When evaluated at 10m, we shouldn't get any alert.

A Complete Example

Suppose you have the following rule file that you want to be tested:

groups:
  - name: prometheus.rules
    rules:
      - alert: HTTPRequestRateLow
        expr: http_requests{group="canary", job="app-server"} < 100
        for: 1m
        labels:
          severity: critical

Write a yaml file with your test cases:

name: Test HTTP Requests too low alert
rules:
  fromFile: rules.yaml
fixtures:
  - duration: 5m
    metrics:
      - http_requests{job="app-server", instance="0", group="canary", severity="overwrite-me"}	75 85  95 105 105  95  85
      - http_requests{job="app-server", instance="1", group="canary", severity="overwrite-me"}	80 90 100 110 120 130 140
assertions:
  - at: 0m
    expected:
      - alertname: HTTPRequestRateLow
        alertstate: pending
        group: canary
        instance: "0"
        job: app-server
        severity: critical
      - alertname: HTTPRequestRateLow
        alertstate: pending
        group: canary
        instance: "1"
        job: app-server
        severity: critical
    comment: |-
      At 0m, the alerts met the threshold but has not met the duration requirement. Expect the alert to be pending
  - at: 5m
    expected:
      - alertname: HTTPRequestRateLow
        alertstate: firing
        group: canary
        instance: "0"
        job: app-server
        severity: critical
      - alertname: HTTPRequestRateLow
        alertstate: firing
        group: canary
        instance: "1"
        job: app-server
        severity: critical
    comment: |-
      At 5m, the alerts should be firing because the duration requirement is met.
  - at: 10m
    expected:
      - alertname: HTTPRequestRateLow
        alertstate: firing
        group: canary
        instance: "0"
        job: app-server
        severity: critical
    comment: |-
      At 10m, the alert should be firing only for instance 0 because instance 1 is >= 100.
  - at: 15m
    expected: []
    comment: |-
      At 15m, both instances are back to normal, therefore we expect no alert.

Run the test:

$ ./pat examples/test.yaml
=== RUN   Test_HTTP_Requests_too_low_alert_at_0m
--- PASS: Test_HTTP_Requests_too_low_alert_at_0m (0.00s)
=== RUN   Test_HTTP_Requests_too_low_alert_at_5m
--- PASS: Test_HTTP_Requests_too_low_alert_at_5m (0.00s)
PASS

More Repositories

1

tangerine

Tangerine Bank scraper
Python
76
star
2

coredns-dockerdiscovery

Docker Discovery Plugin for CoreDNS
Go
61
star
3

promcli

A shell for loading Prometheus metrics fixtures and evaluating Prometheus expressions
Go
39
star
4

btsync.py

A Python API client for BitTorrent Sync
Python
39
star
5

home.idempotent.io

Ansible scripts for my home servers
Makefile
23
star
6

mailchute

A mailinator-like service providing self-hosted disposable email addresses
Python
23
star
7

cdbcli

Interactive CouchDB CLI written using the prompt-kit framework
Python
20
star
8

flask-passwordless

A flask extension for implementing passwordless logins
Python
15
star
9

mastermind

A Golang implementation of the solution to the MasterMind game
Go
13
star
10

florinapp

Offline-capable Personal Finance Management Single Page React App
JavaScript
9
star
11

pci-clj

Programming Collective Intelligence - Clojure
Clojure
9
star
12

ratechecker

A sample GWT/Google App Engine application that uses the best practices such as dependency injection and MVP
Java
8
star
13

artillery-operator

A kubernetes operator for managing artillery (artillery.io) jobs
Go
7
star
14

gxt-widget-expander

A more flexible RowExpander enables embedding GWT/GXT widgets in GXT's grids
Java
5
star
15

lumiere

A Clojure library to output stylized text in console using ANSI escape sequences
Clojure
5
star
16

nostrils

A nose plugin that builds up a map between code and their covering tests
Python
5
star
17

mailchute-ui

A web front-end for mailchute disposable email service
JavaScript
4
star
18

shardgather

A script that runs a SQL query against a collection of sharded databases and gathers the results
Python
4
star
19

greader

A Google Chrome plugin to enhance Google reader experience
JavaScript
3
star
20

chezmoi

My home directory
Perl
2
star
21

soosle

Scala search engine adapted from programming collective intelligence
Scala
2
star
22

spectrum.vim

Vim colorscheme roulette
Python
2
star
23

sudoku-clj

Sudoku solver algorithm in Clojure
Clojure
2
star
24

py-mountebank

Mountebank for honest Python developers
Python
2
star
25

overmind

A toy microservice intended to demonstrate deployment with docker on kubernetes
Go
2
star
26

sync-engine-docker

Dockerfile and docker-compose file for running your own N1 sync-engine (https://github.com/nylas/sync-engine)
Shell
2
star
27

watchlater

Generate atom/rss feed based on a folder of audio/video files so you can use pod catcher to watch/listen to them later
Go
2
star
28

aniketos

Git server hook that rejects bad pushes
Python
1
star
29

helm-plugin-asset

A helm plugin to manage chart assets
Go
1
star
30

blog_old

JavaScript
1
star
31

bifid-clj

A clojure implementation of Bifid cipher
Clojure
1
star
32

tfs-commit-notification

A python program that notifies user when there's a new commit in the path he or she is watching
Python
1
star
33

terraform-aws-kubernetes

HCL
1
star
34

tree_array

An implementation of growable arrays in Erlang
Erlang
1
star
35

antares

A "proxy" server written in Scala
Scala
1
star
36

goal

A RESTful service to manage/simulate soccer competitions
Python
1
star
37

vimmy

A place to store and version control my personalized vim config/tweak
Vim Script
1
star
38

mockingjay

A simple library to build mock http services based on HTTPretty
Python
1
star
39

pet

A utility for testing Prometheus rules
Go
1
star
40

failure_detector

A toy failure detector to help me understand Gossip/SWIM
Python
1
star
41

til

More or less "Today I Learned"
SCSS
1
star
42

lazyboy

A remote control for my TV-connecting laptop so I don't have to leave my couch.
Clojure
1
star
43

lnl

My lunch and learn presentations
JavaScript
1
star
44

lib-ramona

Library to interface with the Ramona Bot (http://www.kurzweilai.net/ramona/ramona.html)
Clojure
1
star
45

phantomail

A SMTP server implemented as a Caddy server type plugin
Go
1
star
46

zoom

A GeoLocation web service using maxmind.com geoip database
Go
1
star