Search code examples
spring-bootgrafanaprometheuscaffeine

Query for a cache hit rate graph with prometheus


I'm using Caffeine cache with Spring Boot application. All metrics are enabled, so I have them on Prometheus and Grafana.

Based on cache_gets_total metric I want to build a HitRate graph.

I've tried to get a cache hits:

delta(cache_gets_total{result="hit",name="myCache"}[1m])

and all gets from cache:

sum(delta(cache_gets_total{name="myCache"}[1m]))

Both of the metrics works correctly and have values. But when I'm trying to get a hit ratio, I have no data points. Query I've tried:

delta(cache_gets_total{result="hit",name="myCache"}[1m]) / sum(delta(cache_gets_total{name="myCache"}[1m]))

Why this query doesn't work and how to get a HitRate graph based on information, I have from Spring Boot and Caffeine?


Solution

  • Run both ("cache hits" and "all gets") queries individually in prometheus and compare label sets you get with results. For "/" operation to work both sides have to have exactly the same labels (and values). Usually some aggregation is required to "drop" unwanted dimensions/labels (like: if you already have one value from both queries then just wrap them both in sum() - before dividing).