Humanize Unix timestamps in Prometheus alerts
Sending some timestamp along with a Prometheus alert text is sometimes very useful to gain better awareness. For example, when exactly will a TLS certificate expire, instdead of some generic text like "TLS certificate will expire in less than x days":
groups:
- name: Blackbox rules
rules:
- alert: SSLCertExpiringSoon
expr: probe_ssl_earliest_cert_expiry - time() < 86400 * 15
for: 30m
labels:
severity: warning
annotations:
description: "TLS certificate will expire in {{ $value }} (instance {{ $labels.instance }})"
However, it is in a Unix timestamp format (e.g. 1567485600
), which is not that convenient for humans to interpret. To make it more human-readable, native Prometheus template functions can be used.
In this particular case it would be useful to see, how much time is left, until the certificate will expire, so humanizeDuration
would seem to be best suited to achieve that. The amended alert would then look something like this:
groups:
- name: Blackbox rules
rules:
- alert: SSLCertExpiringSoon
expr: probe_ssl_earliest_cert_expiry - time() < 86400 * 15
for: 30m
labels:
severity: warning
annotations:
description: "TLS certificate will expire in {{ $value | humanizeDuration }} (instance {{ $labels.instance }})"
The resulting alert's description would then look similar to this: