From d6fa977b8c68be4eb664017a64665137ae107be3 Mon Sep 17 00:00:00 2001 From: Johnny Lim Date: Mon, 11 Nov 2024 16:36:50 +0900 Subject: [PATCH] Add native image support for Hazelcast instrumentation (#5618) Closes gh-5604 --- .../binder/cache/HazelcastIMapAdapter.java | 26 +++++--- .../micrometer-core/reflect-config.json | 66 +++++++++++++++++++ 2 files changed, 82 insertions(+), 10 deletions(-) diff --git a/micrometer-core/src/main/java/io/micrometer/core/instrument/binder/cache/HazelcastIMapAdapter.java b/micrometer-core/src/main/java/io/micrometer/core/instrument/binder/cache/HazelcastIMapAdapter.java index 90aada7c50..9d2b31c320 100644 --- a/micrometer-core/src/main/java/io/micrometer/core/instrument/binder/cache/HazelcastIMapAdapter.java +++ b/micrometer-core/src/main/java/io/micrometer/core/instrument/binder/cache/HazelcastIMapAdapter.java @@ -37,6 +37,8 @@ class HazelcastIMapAdapter { private static final InternalLogger log = InternalLoggerFactory.getInstance(HazelcastIMapAdapter.class); + private static final Class CLASS_DISTRIBUTED_OBJECT = resolveClass("com.hazelcast.core.DistributedObject"); + private static final Class CLASS_I_MAP = resolveOneOf("com.hazelcast.map.IMap", "com.hazelcast.core.IMap"); private static final Class CLASS_LOCAL_MAP = resolveOneOf("com.hazelcast.map.LocalMapStats", @@ -50,8 +52,8 @@ class HazelcastIMapAdapter { private static final MethodHandle GET_LOCAL_MAP_STATS; static { - GET_NAME = resolveIMapMethod("getName", methodType(String.class)); - GET_LOCAL_MAP_STATS = resolveIMapMethod("getLocalMapStats", methodType(CLASS_LOCAL_MAP)); + GET_NAME = resolveMethod(CLASS_DISTRIBUTED_OBJECT, "getName", methodType(String.class)); + GET_LOCAL_MAP_STATS = resolveMethod(CLASS_I_MAP, "getLocalMapStats", methodType(CLASS_LOCAL_MAP)); } private final WeakReference cache; @@ -252,9 +254,9 @@ private static MethodHandle resolveMethod(String name, MethodType mt) { } - private static MethodHandle resolveIMapMethod(String name, MethodType mt) { + private static MethodHandle resolveMethod(Class clazz, String name, MethodType mt) { try { - return MethodHandles.publicLookup().findVirtual(CLASS_I_MAP, name, mt); + return MethodHandles.publicLookup().findVirtual(clazz, name, mt); } catch (NoSuchMethodException | IllegalAccessException e) { throw new IllegalStateException(e); @@ -266,12 +268,16 @@ private static Class resolveOneOf(String class1, String class2) { return Class.forName(class1); } catch (ClassNotFoundException e) { - try { - return Class.forName(class2); - } - catch (ClassNotFoundException ex) { - throw new IllegalStateException(ex); - } + return resolveClass(class2); + } + } + + private static Class resolveClass(String clazz) { + try { + return Class.forName(clazz); + } + catch (ClassNotFoundException e) { + throw new IllegalStateException(e); } } diff --git a/micrometer-core/src/main/resources/META-INF/native-image/io.micrometer/micrometer-core/reflect-config.json b/micrometer-core/src/main/resources/META-INF/native-image/io.micrometer/micrometer-core/reflect-config.json index 01c1060f64..9b3a95b4ac 100644 --- a/micrometer-core/src/main/resources/META-INF/native-image/io.micrometer/micrometer-core/reflect-config.json +++ b/micrometer-core/src/main/resources/META-INF/native-image/io.micrometer/micrometer-core/reflect-config.json @@ -27,5 +27,71 @@ { "name":"org.HdrHistogram.Histogram", "methods":[{"name":"","parameterTypes":["long","long","int"] }] + }, + { + "name":"com.hazelcast.core.DistributedObject", + "methods":[{"name":"getName","parameterTypes":[] }] + }, + { + "name":"com.hazelcast.map.IMap", + "methods":[{"name":"getLocalMapStats","parameterTypes":[] }] + }, + { + "name":"com.hazelcast.core.IMap", + "methods":[{"name":"getLocalMapStats","parameterTypes":[] }] + }, + { + "name":"com.hazelcast.map.LocalMapStats", + "methods":[ + {"name":"getNearCacheStats","parameterTypes":[] }, + {"name":"getOwnedEntryCount","parameterTypes":[] }, + {"name":"getHits","parameterTypes":[] }, + {"name":"getPutOperationCount","parameterTypes":[] }, + {"name":"getSetOperationCount","parameterTypes":[] }, + {"name":"getBackupEntryCount","parameterTypes":[] }, + {"name":"getBackupEntryMemoryCost","parameterTypes":[] }, + {"name":"getOwnedEntryMemoryCost","parameterTypes":[] }, + {"name":"getGetOperationCount","parameterTypes":[] }, + {"name":"getTotalGetLatency","parameterTypes":[] }, + {"name":"getTotalPutLatency","parameterTypes":[] }, + {"name":"getRemoveOperationCount","parameterTypes":[] }, + {"name":"getTotalRemoveLatency","parameterTypes":[] } + ] + }, + { + "name":"com.hazelcast.monitor.LocalMapStats", + "methods":[ + {"name":"getNearCacheStats","parameterTypes":[] }, + {"name":"getOwnedEntryCount","parameterTypes":[] }, + {"name":"getHits","parameterTypes":[] }, + {"name":"getPutOperationCount","parameterTypes":[] }, + {"name":"getSetOperationCount","parameterTypes":[] }, + {"name":"getBackupEntryCount","parameterTypes":[] }, + {"name":"getBackupEntryMemoryCost","parameterTypes":[] }, + {"name":"getOwnedEntryMemoryCost","parameterTypes":[] }, + {"name":"getGetOperationCount","parameterTypes":[] }, + {"name":"getTotalGetLatency","parameterTypes":[] }, + {"name":"getTotalPutLatency","parameterTypes":[] }, + {"name":"getRemoveOperationCount","parameterTypes":[] }, + {"name":"getTotalRemoveLatency","parameterTypes":[] } + ] + }, + { + "name":"com.hazelcast.nearcache.NearCacheStats", + "methods":[ + {"name":"getHits","parameterTypes":[] }, + {"name":"getMisses","parameterTypes":[] }, + {"name":"getEvictions","parameterTypes":[] }, + {"name":"getPersistenceCount","parameterTypes":[] } + ] + }, + { + "name":"com.hazelcast.monitor.NearCacheStats", + "methods":[ + {"name":"getHits","parameterTypes":[] }, + {"name":"getMisses","parameterTypes":[] }, + {"name":"getEvictions","parameterTypes":[] }, + {"name":"getPersistenceCount","parameterTypes":[] } + ] } ]