Skip to content

Commit

Permalink
gh-116 Use Hazelcast 2.4 for compilation, include Hazelcast2CacheMetr…
Browse files Browse the repository at this point in the history
…icsTest
  • Loading branch information
Daan Kerkhofs committed Jul 12, 2021
1 parent 8151a67 commit 1397456
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 38 deletions.
5 changes: 4 additions & 1 deletion alfred-telemetry-platform/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ dependencies {

// 'alfresco-enterprise-repository' not transitive because it will try to download a bunch of unreachable artifacts
alfrescoProvided('org.alfresco:alfresco-enterprise-repository') { transitive = false }
alfrescoProvided('com.hazelcast:hazelcast') { transitive = false }
alfrescoProvided('com.hazelcast:hazelcast:2.4') {
force = true
transitive = false
}

implementation("io.micrometer:micrometer-core:${micrometerVersion}") {
exclude group: "org.slf4j", module: "*"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,15 @@
import io.micrometer.core.instrument.binder.cache.CacheMeterBinder;
import io.micrometer.core.instrument.binder.cache.HazelcastCacheMetrics;
import io.micrometer.core.lang.Nullable;
import java.lang.reflect.Method;
import java.util.concurrent.TimeUnit;
import javax.annotation.Nonnull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* {@link HazelcastCacheMetrics} equivalent which is compatible with Hazelcast 2 (Alfresco 5.x)
*/
public class Hazelcast2CacheMetrics extends CacheMeterBinder {

private static final Logger logger = LoggerFactory.getLogger(Hazelcast2CacheMetrics.class);

private static final String TAG_OWNERSHIP = "ownership";
private static final String METHOD_GET_OPERATION_STATS = "getOperationStats";
private static final String METHOD_GET_PUT_OPERATION_COUNT = "getPutOperationCount";

private final IMap<?, ?> cache;

Expand Down Expand Up @@ -98,7 +91,7 @@ protected Long evictionCount() {

@Override
protected long putCount() {
return extractMetricWithReflection(cache.getLocalMapStats(), METHOD_GET_PUT_OPERATION_COUNT);
return cache.getLocalMapStats().getOperationStats().getNumberOfPuts();
}

@Override
Expand Down Expand Up @@ -126,8 +119,7 @@ protected void bindImplementationSpecificMetrics(@Nonnull MeterRegistry registry
.register(registry);

FunctionCounter.builder("cache.partition.gets", cache,
cache -> extractMetricWithReflection(cache.getLocalMapStats(), METHOD_GET_OPERATION_STATS,
"getNumberOfGets"))
c -> c.getLocalMapStats().getOperationStats().getNumberOfGets())
.tags(getTagsWithCacheName())
.description("The total number of get operations executed against this partition")
.register(registry);
Expand All @@ -138,49 +130,28 @@ protected void bindImplementationSpecificMetrics(@Nonnull MeterRegistry registry

private void timings(MeterRegistry registry) {
FunctionTimer.builder("cache.gets.latency", cache,
cache -> extractMetricWithReflection(cache.getLocalMapStats(), METHOD_GET_OPERATION_STATS,
"getNumberOfGets"),
cache -> extractMetricWithReflection(cache.getLocalMapStats(), METHOD_GET_OPERATION_STATS,
"getTotalGetLatency"),
c -> c.getLocalMapStats().getOperationStats().getNumberOfGets(),
c -> c.getLocalMapStats().getOperationStats().getTotalGetLatency(),
TimeUnit.NANOSECONDS)
.tags(getTagsWithCacheName())
.description("Cache gets")
.register(registry);

FunctionTimer.builder("cache.puts.latency", cache,
cache -> extractMetricWithReflection(cache.getLocalMapStats(), METHOD_GET_OPERATION_STATS,
"getNumberOfPuts"),
cache -> extractMetricWithReflection(cache.getLocalMapStats(), METHOD_GET_OPERATION_STATS,
"getTotalPutLatency"),
c -> c.getLocalMapStats().getOperationStats().getNumberOfPuts(),
c -> c.getLocalMapStats().getOperationStats().getTotalPutLatency(),
TimeUnit.NANOSECONDS)
.tags(getTagsWithCacheName())
.description("Cache puts")
.register(registry);

FunctionTimer.builder("cache.removals.latency", cache,
cache -> extractMetricWithReflection(cache.getLocalMapStats(), METHOD_GET_OPERATION_STATS,
"getNumberOfRemoves"),
cache -> extractMetricWithReflection(cache.getLocalMapStats(), METHOD_GET_OPERATION_STATS,
"getTotalRemoveLatency"),
c -> c.getLocalMapStats().getOperationStats().getNumberOfRemoves(),
c -> c.getLocalMapStats().getOperationStats().getTotalRemoveLatency(),
TimeUnit.NANOSECONDS)
.tags(getTagsWithCacheName())
.description("Cache removals")
.register(registry);
}

public static long extractMetricWithReflection(final Object object, final String... methods) {
try {
Object currentObject = object;
for (String methodToExecute : methods) {
final Method method = currentObject.getClass().getMethod(methodToExecute);
method.setAccessible(true);
currentObject = method.invoke(currentObject);
}
return (long) currentObject;
} catch (Throwable e) {
logger.warn("Unable to extract metric using reflection", e);
return -1;
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package eu.xenit.alfred.telemetry.binder.cache;

import static org.awaitility.Awaitility.await;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.nullValue;

import com.hazelcast.config.Config;
import com.hazelcast.core.Hazelcast;
import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.core.IMap;
import io.micrometer.core.instrument.simple.SimpleMeterRegistry;
import java.time.Duration;
import org.junit.jupiter.api.Test;

class Hazelcast2CacheMetricsTest {

@Test
void testBasicPutAndGetMetrics() {
SimpleMeterRegistry meterRegistry = new SimpleMeterRegistry();
HazelcastInstance hazelcastInstance = Hazelcast.newHazelcastInstance(new Config());
IMap<String, String> cache = hazelcastInstance.getMap(this.getClass().getSimpleName());

Hazelcast2CacheMetrics.monitor(meterRegistry, cache);
cache.put("foo", "bar");

assertThat(cache.get("foo"), is("bar"));
assertThat(cache.get("baz"), is(nullValue()));

assertThat(meterRegistry.get("cache.gets").tag("result", "hit").functionCounter().count(), is(1.0));
await().atMost(Duration.ofSeconds(5))
.until(() -> meterRegistry.get("cache.puts").functionCounter().count(), is(1.0));

}

}

0 comments on commit 1397456

Please sign in to comment.