Skip to content

Commit

Permalink
Do not increment metrics on CaffeineCache#getIfPresent call
Browse files Browse the repository at this point in the history
  • Loading branch information
gwenneg committed Apr 18, 2024
1 parent 8fd116d commit 336eea4
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -127,13 +127,10 @@ public <V> CompletableFuture<V> getIfPresent(Object key) {
Objects.requireNonNull(key, NULL_KEYS_NOT_SUPPORTED_MSG);
CompletableFuture<Object> existingCacheValue = cache.getIfPresent(key);

// record metrics, if not null apply casting
if (existingCacheValue == null) {
statsCounter.recordMisses(1);
return null;
} else {
LOGGER.tracef("Key [%s] found in cache [%s]", key, cacheInfo.name);
statsCounter.recordHits(1);

// cast, but still throw the CacheException in case it fails
return unwrapCacheValueOrThrowable(existingCacheValue)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ quarkus.cache.caffeine."forest".expire-after-write=10M

quarkus.cache.caffeine."expensiveResourceCache".expire-after-write=10M
quarkus.cache.caffeine."expensiveResourceCache".metrics-enabled=true
quarkus.cache.caffeine."getIfPresentCache".metrics-enabled=true

io.quarkus.it.cache.SunriseRestClient/mp-rest/url=${test.url}
Original file line number Diff line number Diff line change
@@ -1,33 +1,82 @@
package io.quarkus.it.cache;

import static io.restassured.RestAssured.when;
import static java.util.concurrent.CompletableFuture.completedFuture;
import static org.hamcrest.Matchers.is;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.util.concurrent.ExecutionException;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import io.quarkus.cache.Cache;
import io.quarkus.cache.CacheName;
import io.quarkus.cache.CaffeineCache;
import io.quarkus.test.junit.QuarkusTest;

@QuarkusTest
@DisplayName("Tests the cache extension")
public class CacheTestCase {

private static final String EXPENSIVE_RESOURCE_CACHE_NAME = "expensiveResourceCache";
private static final String GET_IF_PRESENT_CACHE_NAME = "getIfPresentCache";
private static final String CACHE_KEY = "foo";

@CacheName(GET_IF_PRESENT_CACHE_NAME)
Cache cache;

@Test
public void testCache() {
void testCache() {
assertMetrics(EXPENSIVE_RESOURCE_CACHE_NAME, 0, 0, 0);

runExpensiveRequest();
assertMetrics(EXPENSIVE_RESOURCE_CACHE_NAME, 1, 1, 0);

runExpensiveRequest();
assertMetrics(EXPENSIVE_RESOURCE_CACHE_NAME, 1, 1, 1);

runExpensiveRequest();
assertMetrics(EXPENSIVE_RESOURCE_CACHE_NAME, 1, 1, 2);

when().get("/expensive-resource/invocations").then().statusCode(200).body(is("1"));
}

String metricsResponse = when().get("/q/metrics").then().extract().asString();
assertTrue(metricsResponse.contains("cache_puts_total{cache=\"expensiveResourceCache\"} 1.0"));
assertTrue(metricsResponse.contains("cache_gets_total{cache=\"expensiveResourceCache\",result=\"miss\"} 1.0"));
assertTrue(metricsResponse.contains("cache_gets_total{cache=\"expensiveResourceCache\",result=\"hit\"} 2.0"));
@Test
void testGetIfPresentMetrics() throws ExecutionException, InterruptedException {

CaffeineCache caffeineCache = cache.as(CaffeineCache.class);
assertMetrics(GET_IF_PRESENT_CACHE_NAME, 0, 0, 0);

assertNull(caffeineCache.getIfPresent(CACHE_KEY));
assertMetrics(GET_IF_PRESENT_CACHE_NAME, 0, 1, 0);

assertNull(caffeineCache.getIfPresent(CACHE_KEY));
assertMetrics(GET_IF_PRESENT_CACHE_NAME, 0, 2, 0);

caffeineCache.put(CACHE_KEY, completedFuture("bar"));
assertMetrics(GET_IF_PRESENT_CACHE_NAME, 1, 2, 0);

assertEquals("bar", caffeineCache.getIfPresent(CACHE_KEY).get());
assertMetrics(GET_IF_PRESENT_CACHE_NAME, 1, 2, 1);

assertEquals("bar", caffeineCache.getIfPresent(CACHE_KEY).get());
assertMetrics(GET_IF_PRESENT_CACHE_NAME, 1, 2, 2);
}

private void runExpensiveRequest() {
when().get("/expensive-resource/I/love/Quarkus?foo=bar").then().statusCode(200).body("result",
is("I love Quarkus too!"));
}

private void assertMetrics(String cacheName, double expectedPuts, double expectedMisses, double expectedHits) {
String metricsResponse = when().get("/q/metrics").then().extract().asString();
assertTrue(metricsResponse.contains(String.format("cache_puts_total{cache=\"%s\"} %.1f", cacheName, expectedPuts)));
assertTrue(metricsResponse
.contains(String.format("cache_gets_total{cache=\"%s\",result=\"miss\"} %.1f", cacheName, expectedMisses)));
assertTrue(metricsResponse
.contains(String.format("cache_gets_total{cache=\"%s\",result=\"hit\"} %.1f", cacheName, expectedHits)));
}
}

0 comments on commit 336eea4

Please sign in to comment.