Why ever use CachedGauage in Dropwizard Metrics?

1.3k views Asked by At

I see there is the CachedGauge in the DropWizard Metrics (formerly Coda Hale Metrics) lib. I'm wondering why anyone would ever use this?!?

My understanding of the value of a metric is that it is real-time, accurate and relevant. If a metric is reporting a stale/cached value, of what purpose could it possibly serve?

2

There are 2 answers

2
André On BEST ANSWER

Quoting the same link you added:

Cached Gauges

A cached gauge allows for a more efficient reporting of values which are expensive to calculate

What if your metric takes around two seconds seconds to calculate, or even minutes? Would you calculate every time the user request the data? Makes sense to store on a cache.

Such metric isn't what I would call real time because updating itself takes more time than rendering the information for the user. The metric is outdated already when it finishes it's calculation.

0
Tiago L. Alves On

Just a note of warning when using CachedGauge. You need to take into account the computation time when specifying the time for which you're caching that value. This is because the cache expiry period starts counting before the computation is done and it's not the cache period after the value is computed.

For example, in the example in the link which we're caching a value for 10 minutes, if loadValue() takes 5m, we're only caching it for 10m - 5m = 5 minutes.