You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@bookkeeper.apache.org by GitBox <gi...@apache.org> on 2022/04/29 03:53:22 UTC

[GitHub] [bookkeeper] dlg99 commented on a diff in pull request #3252: [ISSUE 2637] Fix jvm_memory_direct_bytes_used metrics when using jdk11+

dlg99 commented on code in PR #3252:
URL: https://github.com/apache/bookkeeper/pull/3252#discussion_r861436192


##########
stats/bookkeeper-stats-providers/prometheus-metrics-provider/src/main/java/org/apache/bookkeeper/stats/prometheus/PrometheusMetricsProvider.java:
##########
@@ -223,9 +224,11 @@ private void registerMetrics(Collector collector) {
         AtomicLong tmpDirectMemoryUsage = null;
 
         try {
-            Field field = PlatformDependent.class.getDeclaredField("DIRECT_MEMORY_COUNTER");
-            field.setAccessible(true);
-            tmpDirectMemoryUsage = (AtomicLong) field.get(null);
+            Class<?> name = Class.forName("java.nio.Bits");
+            String fieldName = SystemUtils.IS_JAVA_1_8 ? "reservedMemory" : "RESERVED_MEMORY";
+            Field reservedMemory = name.getDeclaredField(fieldName);
+            reservedMemory.setAccessible(true);
+            tmpDirectMemoryUsage = (AtomicLong) reservedMemory.get(null);

Review Comment:
   fyi, you can use org.apache.commons.lang.reflect.FieldUtils [FieldUtils.readDeclaredField() ](https://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/reflect/FieldUtils.html#readField-java.lang.reflect.Field-java.lang.Object-boolean-)with forceAccess
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@bookkeeper.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org