google / guava

Google core libraries for Java
Apache License 2.0
50.03k stars 10.86k forks source link

Provide API to see if a cache records statistics #7381

Open izeye opened 2 weeks ago

izeye commented 2 weeks ago

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

hiddenHunter291 commented 2 weeks ago

Can you assign me this task

lowasser commented 3 days ago

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

izeye commented 3 days ago

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