Calculate cache hits

I’d like to measure the cache hit rate of our services. We’re using SpringBoot with NewRelic. In the code we have to following:

For a cache hit

        NewRelic.incrementCounter("cacheHit", 1);

For a cache miss

        NewRelic.incrementCounter("cacheMiss", 1);

The Java metric API does not allow any additional attributes. Now I’d like to calculate the cache hit rate in NewRelic but I don’t know how I can combine the two values.

The following query allows me to query one of the values. Since it’s a metric I have to use this “strange” format newrelic.timeslice.value.

SELECT count(newrelic.timeslice.value) as Miss FROM Metric WHERE metricTimesliceName = 'cacheHit' SINCE 24 HOURS AGO TIMESERIES
SELECT count(newrelic.timeslice.value) as Hit FROM Metric WHERE metricTimesliceName = 'cacheMiss' SINCE 24 HOURS AGO TIMESERIES

We can’t use the NewRelic default integration for this cache. I could easily solve this by using events but that would create a lot of unnecessary events.

Hi, @aom.nova.ti:

SELECT 
  filter(count(newrelic.timeslice.value), WHERE metricTimesliceName = 'cacheHit') / 
  filter(count(newrelic.timeslice.value), WHERE metricTimesliceName IN ('cacheHit', 'cacheMiss')
FROM Metric 
SINCE 24 hours ago 
TIMESERIES

By the way, you may use the metric API to add additional attributes to a metric.