forked from micrometer-metrics/micrometer
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Revise cache metrics implementations + tests (micrometer-metrics#1107)
- Loading branch information
1 parent
a99d397
commit bb65f8a
Showing
12 changed files
with
898 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
...re/src/test/java/io/micrometer/core/instrument/binder/cache/AbstractCacheMetricsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/** | ||
* Copyright 2018 Pivotal Software, Inc. | ||
* <p> | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* <p> | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* <p> | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.micrometer.core.instrument.binder.cache; | ||
|
||
import io.micrometer.core.instrument.MeterRegistry; | ||
import io.micrometer.core.instrument.Tag; | ||
import io.micrometer.core.instrument.Tags; | ||
import io.micrometer.core.instrument.search.RequiredSearch; | ||
|
||
/** | ||
* @author Oleksii Bondar | ||
*/ | ||
abstract class AbstractCacheMetricsTest { | ||
|
||
protected Tags expectedTag = Tags.of("app", "test"); | ||
|
||
/** | ||
* Verifies base metrics presence | ||
*/ | ||
protected void verifyCommonCacheMetrics(MeterRegistry meterRegistry, CacheMeterBinder meterBinder) { | ||
meterRegistry.get("cache.puts").tags(expectedTag).functionCounter(); | ||
meterRegistry.get("cache.gets").tags(expectedTag).tag("result", "hit").functionCounter(); | ||
|
||
if (meterBinder.size() != null) { | ||
meterRegistry.get("cache.size").tags(expectedTag).gauge(); | ||
} | ||
if (meterBinder.missCount() != null) { | ||
meterRegistry.get("cache.gets").tags(expectedTag).tag("result", "miss").functionCounter(); | ||
} | ||
if (meterBinder.evictionCount() != null) { | ||
meterRegistry.get("cache.evictions").tags(expectedTag).functionCounter(); | ||
} | ||
} | ||
|
||
protected RequiredSearch fetch(MeterRegistry meterRegistry, String name) { | ||
return meterRegistry.get(name).tags(expectedTag); | ||
} | ||
|
||
protected RequiredSearch fetch(MeterRegistry meterRegistry, String name, Iterable<Tag> tags) { | ||
return fetch(meterRegistry, name).tags(tags); | ||
} | ||
} |
114 changes: 114 additions & 0 deletions
114
...re/src/test/java/io/micrometer/core/instrument/binder/cache/CaffeineCacheMetricsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
/** | ||
* Copyright 2018 Pivotal Software, Inc. | ||
* <p> | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* <p> | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* <p> | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.micrometer.core.instrument.binder.cache; | ||
|
||
import com.github.benmanes.caffeine.cache.Cache; | ||
import com.github.benmanes.caffeine.cache.CacheLoader; | ||
import com.github.benmanes.caffeine.cache.Caffeine; | ||
import com.github.benmanes.caffeine.cache.LoadingCache; | ||
import com.github.benmanes.caffeine.cache.stats.CacheStats; | ||
|
||
import io.micrometer.core.instrument.FunctionCounter; | ||
import io.micrometer.core.instrument.Gauge; | ||
import io.micrometer.core.instrument.MeterRegistry; | ||
import io.micrometer.core.instrument.Tags; | ||
import io.micrometer.core.instrument.TimeGauge; | ||
import io.micrometer.core.instrument.simple.SimpleMeterRegistry; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
/** | ||
* Tests for {@link CaffeineCacheMetrics}. | ||
* | ||
* @author Oleksii Bondar | ||
*/ | ||
class CaffeineCacheMetricsTest extends AbstractCacheMetricsTest { | ||
|
||
private LoadingCache<String, String> cache = Caffeine.newBuilder().build(new CacheLoader<String, String>() { | ||
public String load(String key) throws Exception { | ||
return ""; | ||
}; | ||
}); | ||
private CaffeineCacheMetrics metrics = new CaffeineCacheMetrics(cache, "testCache", expectedTag); | ||
|
||
@Test | ||
void reportExpectedGeneralMetrics() { | ||
MeterRegistry registry = new SimpleMeterRegistry(); | ||
metrics.bindTo(registry); | ||
|
||
verifyCommonCacheMetrics(registry, metrics); | ||
|
||
Gauge evictionWeight = fetch(registry, "cache.eviction.weight").gauge(); | ||
CacheStats stats = cache.stats(); | ||
assertThat(evictionWeight.value()).isEqualTo(stats.evictionCount()); | ||
|
||
// specific to LoadingCache instance | ||
TimeGauge loadDuration = fetch(registry, "cache.load.duration").timeGauge(); | ||
assertThat(loadDuration.value()).isEqualTo(stats.totalLoadTime()); | ||
|
||
FunctionCounter successfulLoad = fetch(registry, "cache.load", Tags.of("result", "success")).functionCounter(); | ||
assertThat(successfulLoad.count()).isEqualTo(stats.loadSuccessCount()); | ||
|
||
FunctionCounter failedLoad = fetch(registry, "cache.load", Tags.of("result", "failure")).functionCounter(); | ||
assertThat(failedLoad.count()).isEqualTo(stats.loadFailureCount()); | ||
} | ||
|
||
@Test | ||
void constructInstanceViaStaticMethodMonitor() { | ||
MeterRegistry meterRegistry = new SimpleMeterRegistry(); | ||
CaffeineCacheMetrics.monitor(meterRegistry, cache, "testCache", expectedTag); | ||
|
||
meterRegistry.get("cache.eviction.weight").tags(expectedTag).gauge(); | ||
} | ||
|
||
@Test | ||
void doNotReportMetricsForNonLoadingCache() { | ||
MeterRegistry meterRegistry = new SimpleMeterRegistry(); | ||
Cache<Object, Object> cache = Caffeine.newBuilder().build(); | ||
CaffeineCacheMetrics metrics = new CaffeineCacheMetrics(cache, "testCache", expectedTag); | ||
metrics.bindTo(meterRegistry); | ||
|
||
assertThat(meterRegistry.find("cache.load.duration").timeGauge()).isNull(); | ||
} | ||
|
||
@Test | ||
void returnCacheSize() { | ||
assertThat(metrics.size()).isEqualTo(cache.estimatedSize()); | ||
} | ||
|
||
@Test | ||
void returnHitCount() { | ||
assertThat(metrics.hitCount()).isEqualTo(cache.stats().hitCount()); | ||
} | ||
|
||
@Test | ||
void returnMissCount() { | ||
assertThat(metrics.missCount()).isEqualTo(cache.stats().missCount()); | ||
} | ||
|
||
@Test | ||
void returnEvictionCount() { | ||
assertThat(metrics.evictionCount()).isEqualTo(cache.stats().evictionCount()); | ||
} | ||
|
||
@Test | ||
void returnPutCount() { | ||
assertThat(metrics.putCount()).isEqualTo(cache.stats().loadCount()); | ||
} | ||
|
||
} |
Oops, something went wrong.