From events to Grafana annotation
At some point you are frustrated in monitoring. You have to correlate facts to metrics, and it can become really difficult with monitoring systems. You have dozens of metrics and dashboards but finding a cause to a spike is often a needle in a haystack.
The annotation API at a glance
Grafana comes with a native annotation API, that can store and retrieve annotations for dashboards. Adding annotation is very easy and convenient since Grafana 4.6. Just Hold CMD + Click, this will add an annotation at the selected timestamp that will be stored in Grafana database. But, this is clearly a manual step that could be used to explain events afterwards.
Annotations can also be retrieved from other data sources such as Elasticsearch, MySQL/PostgreSQL, Prometheus, but let’s focus on the annotation API ans its in two formats, the native Grafana format and the Graphite format.
The Graphite format
Quick takeaways on choosing the Graphite format:
- dashboard has to be configured to display annotations
- four simple fields “what”, “when”, “data”, and “tags ”
Grafana Graphite annotation format
The Grafana native Format:
Quick takeaways on choosing the native format:
- can be linked to a dashboard and panel id (optional).
- can span a time range (with
timeEnd
) - millisecond epoch
Grafana native annotation format
Manual annotation posting
Posting annotation is fairly easy, just call the right endpoint with the right payload. We’ll use here the graphite API as this annotation is really simple to both implement and understand.
We must be authenticated to use the Grafana APIs. First, create a token. Once done, here you go:
Should I really curl Grafana every time ?
Of course no, there are plenty of ways to achieve this. At Contentsquare we often build simple tool to achieve simple actions. https://github.com/Contentsquare/grafana-annotation is one example. We deploy this tool via Ansible on our systems, and can refer to it when needed.
A simple use case is to track systemd services events as follows:
- step one, deploy the Grafana annotation tool
- step two, create a notifier systemd service
- step three, update the target systemd service to track
The Notifier systemd Service:
The Target systemd Service to monitor:
This service will trigger the notifier when started, stopped or failed. OnFailure=notify-grafana-failure@%i.service %i refers to the failing service name. See systemd specifiers for more information.
Grafana annotations on dashboard:
By adding the corrects tags to filter annotations, we will display on the whole dashboard all the corresponding annotations.
To go further
There are plenty of use cases, but let’s imagine a world where all sorts of events could be traced to make sense at some point. For automated workflows like CI/CD pipelines or scaling operations, it is a must!