How to keep last non-zero metric value in Prometheus/Grafana?

Hi!

  • What Grafana version and what operating system are you using: Graphana: Latest, Arch Linux

  • What are you trying to achieve:

    I have a service that sends metrics to Prometheus, and I would like to keep metric continuity in Grafana. My use case looks like this:

    1. If my service is down, I want Grafana to display the last value greater than 0

    2. If my service is up, but the metric value is 0, I also want Grafana to display the last value greater than 0

    3. If my service is up and the metric value is greater than 0, then display the actual value

Do you know how to achieve this behavior in Prometheus/Grafana? Maybe with some specific query functions or transformations?

Thanks in advance for any suggestions!