guava icon indicating copy to clipboard operation
guava copied to clipboard

Provide API to see if a cache records statistics

Open izeye opened this issue 1 year ago • 4 comments

API(s)

com.google.common.cache.Cache

How do you want it to be improved?

It would be nice to provide an API to see if recording statistics in a cache is activated similar to the Policy.isRecordingStats() from the Caffeine cache.

Why do we need it to be improved?

For libraries that collect metrics from a cache, it would be useful to warn a user that metrics won't be collected properly or not to register meters that produce only zero values by using the proposed API.

Example

if (!cache.isRecordingStats()) {
  // Warn a user that the cache doesn't provide statistics.
}
if (cache.isRecordingStats()) {
  // Register meters with its statistics.
}

Current Behavior

It's not possible to know if a cache records statistics.

Desired Behavior

It would be nice to know if a cache records statistics.

Concrete Use Cases

https://github.com/micrometer-metrics/micrometer/issues/5066 and https://github.com/micrometer-metrics/micrometer/pull/5402#issuecomment-2315785818 are examples that the proposed API is necessary.

Checklist

izeye avatar Aug 30 '24 14:08 izeye

Can you assign me this task

hiddenHunter291 avatar Sep 02 '24 14:09 hiddenHunter291

It looks like you're aware of Caffeine's cache -- is there a particular reason you're not using it?

lowasser avatar Sep 13 '24 21:09 lowasser

@lowasser As mentioned in the opening comment, I created this for libraries (ex. Micrometer), not for applications.

izeye avatar Sep 13 '24 22:09 izeye

Can you assign this to me?

PrimroseZhang avatar Oct 17 '24 07:10 PrimroseZhang

Does your library just accept an already-constructed Cache instance from your users? Could the library configure and construct the cache for the user instead? Then you can guarantee that the cache is recording stats.

chaoren avatar Nov 18 '24 21:11 chaoren

Does your library just accept an already-constructed Cache instance from your users?

@chaoren Yes, it's accepting prebuilt caches for metrics as follows:

https://github.com/micrometer-metrics/micrometer/blob/4d9efae6b7f3635be5e99fa089eb2851f9d9ca78/micrometer-core/src/main/java/io/micrometer/core/instrument/binder/cache/GuavaCacheMetrics.java#L54-L57

Could the library configure and construct the cache for the user instead?

It's just a library for observability, so considering its nature, it doesn't seem to be an option.

izeye avatar Nov 18 '24 23:11 izeye

We're not planning to add more features to the caching library at this point. Perhaps your library could include a recommendation for your users to migrate to Caffeine for the desired feature.

chaoren avatar Nov 20 '24 16:11 chaoren