DEV Community

Discussion on: Annotating Deployments in Grafana Using the Process Start Time Metric

Collapse
 
ultrafez profile image
Alex Silcock

There's another useful metric that you can use to create annotations when you deploy, if you have kube-state-metrics configured on your cluster: kube_replicaset_created.

This metric exists for every ReplicaSet that has been created recently, with a label identifying which RS it refers to, and a value referencing the timestamp when the RS was created. If you're using a Deployment to manage your app's pod lifecycle, every time you release a new image or change the config, it will result in a new ReplicaSet being created. We can use this to write a Prometheus query which will create an annotation every time a RS is created:

changes(max(kube_replicaset_created{namespace="<YourNamespaceName>"})[1m:])
Enter fullscreen mode Exit fullscreen mode

To break down what this query is doing:

  • The kube_replicaset_created metric value is a timestamp measured in seconds, so we can use the max aggregation function to get the timestamp of the most-recently created RS.
  • Since we only want our Grafana annotation to appear every time a new RS is created, we need the query value to be 0 unless a replicaset has just been created. Using the changes function, combined with the subquery [1m:], gives us this.