Alerts are the types of conditions that will trigger Monika to send notifications. It is an array defined in the config file monika.yml
like so.
probes:- id: '1'name: Name of the proberequests:- method: GETurl: https://github.comalerts:- assertion: response.size >= 10000message: Response size is {{ response.size }}, expecting less than 10000
You can define two types of alerts: request alerts and probe alerts
Request alerts are alerts
configurations that are scoped under the requests
key. Alerts defined under a specific request will run for that specific request only. Take a look at the example below:
probes:- id: '1'name: Name of the proberequests:- method: GETurl: https://github.comalerts:- assertion: response.status != 200message: Status not 2xx # (This alert is only triggered for the github.com request)- method: GETurl: https://gitlab.comalerts:- assertion: response.status != 200message: Status not 2xx # (This alert is only triggered for the gitlab.com request)
Probe alerts are alerts
configurations that are defined under the probe
key. Alerts scoped under the probe key will run for all requests for that probe. Take a look at the example below:
probes:- id: '1'name: Name of the proberequests:- method: GETurl: https://github.comalerts:- assertion: response.status != 200message: Status not 2xx # (This alert is only triggered for the github.com request)- method: GETurl: https://gitlab.comalerts:- assertion: response.status != 200message: Status not 2xx # (This alert is only triggered for the gitlab.com request)alerts:- assertion: response.time > 10000 # in millisecondsmessage: Response time is longer than 10 seconds # (This alert is triggered for all request)
Probes are performed after every interval, and alerts are generated after a specified threshold. Monika can perform probes once a second, therefore a theoretical maximum rate of one alert a second. Please keep in mind that there may be some delays to your network, notification channels (slack, email, etc), so your result will vary.
In general it will be something like:
Alert resolution = interval period (s) x threshold + network_and_channel_latencies
From above, the theoretical maximum resolution is one second.
Assertion contains any arbitrary expression that will trigger alert when it returns a truthy value
alerts:- assertion: response.status == 500
Inside the assertion expression you can get the response object.
These are values that are available:
The response.headers
and response.body
can be queried further with object access syntax.
For example, to trigger alert when content-type is not json you may use
alerts:- assertion: response.headers['content-type'] != "application/json"
Or to assertion value inside the body
alerts:- assertion: response.body.data.todos[0].title != "Drink water"
Additionally you can have processing done in your queries. For instance, to ensure case insensitivity, you might want to convert to lowercase. It might look something like this:
alerts:- assertion: has(lowerCase(response.body.status), "success")
These operators are available:
Numeric arithmetic | Description |
---|---|
x + y | Add |
x - y | Subtract |
x * y | Multiply |
x / y | Divide |
x % y | Modulo |
x ^ y | Power |
Comparisons | Description |
---|---|
x == y | Equals |
x != y | Does not equal |
x < y | Less than |
x <= y | Less than or equal to |
x > y | Greater than |
x >= y | Greater than or equal to |
x ~= y | Regular expression match |
x in (a, b, c) | Equivalent to (x == a or x == b or x == c) |
x not in (a, b, c) | Equivalent to (x != a and x != b and x != c) |
Boolean logic | Description |
---|---|
x or y | Boolean or |
x and y | Boolean and |
not x | Boolean not |
x ? y : z | If boolean x, value y, else z |
( x ) | Explicit operator precedence |
There are also several helper functions available:
has(object, property): Checks whether an object has searched property.
example: has(response.body, "data")
checks if there is "data" property inside response.body
lowerCase(string): Converts string to lowercase
example: lowerCase(response.body.message)
converts message string value to lowercase
upperCase(string): Converts string to uppercase
example: upperCase(response.body.message)
converts message string value to uppercase
startsWith(string, target): Checks if string starts with the given target string
example: startsWith(response.body.message, "Hello")
checks if message string value starts with "Hello"
endsWith(string, target): Checks if string ends with the given target string
example: startsWith(response.body.message, "world!")
checks if message string value ends with "world!"
includes(collection, value): Checks if value is in collection. If collection is a string, it's checked for a substring of value
example 1: includes(response.body.prizes, "gold")
checks if "gold" exists in the prizes array.
example 2: includes(response.body.message, "ello")
checks if "ello" is a substring of the message string.
size(collection): Gets length of array or string values.
example: size(response.body.data.items)
gets the count of items.
isEmpty(value): Checks if value is an empty object, empty array, empty string, null, or undefined.
example: isEmpty(response.body.data)
checks whether the data property is empty or not
alerts:- assertion: response.status != 200message: HTTP Status code is different, expecting 200
This is the message that is used in the sent notification.
Inside the message string, you can also get the response object similar to assertion by surrounding the expression with double curly braces like the example above.
Monika will automatically issue an alert when it detects a probe is inaccessible. An incident and recovery alert will be sent to all configured notification channels. Note that explicit connection assertion and customized message strings are not yet supported.
PT Artha Rajamas Mandiri (Hyperjump) is an open-source-first company providing engineering excellence service. We aim to build and commercialize open-source tools to help companies streamline, simplify, and secure the most important aspects of its modern DevOps practices.
Copyright © 2024 Hyperjump Tech. All Rights Reserved.