This is an automated email from the ASF dual-hosted git repository.

leonard pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/flink.git


The following commit(s) were added to refs/heads/master by this push:
     new f8b3c4b9a8c [FLINK-37709][metrics] Add file descriptor JVM metrics
f8b3c4b9a8c is described below

commit f8b3c4b9a8ce1c6a094fcc0f292faea4bad8806c
Author: Peter Huang <[email protected]>
AuthorDate: Sat Apr 26 18:59:41 2025 -0700

    [FLINK-37709][metrics] Add file descriptor JVM metrics
    
    This closes  #26492
---
 docs/content.zh/docs/ops/metrics.md                | 29 ++++++++++++++++++++++
 docs/content/docs/ops/metrics.md                   | 29 ++++++++++++++++++++++
 .../apache/flink/runtime/metrics/MetricNames.java  |  3 +++
 .../flink/runtime/metrics/util/MetricUtils.java    | 25 +++++++++++++++++++
 .../runtime/metrics/util/MetricUtilsTest.java      | 10 ++++++++
 5 files changed, 96 insertions(+)

diff --git a/docs/content.zh/docs/ops/metrics.md 
b/docs/content.zh/docs/ops/metrics.md
index dd9649a4a96..a4fc89215dd 100644
--- a/docs/content.zh/docs/ops/metrics.md
+++ b/docs/content.zh/docs/ops/metrics.md
@@ -632,6 +632,35 @@ Some metrics might not be exposed when using other JVM 
implementations (e.g. IBM
   </tbody>                                                         
 </table>
 
+### File Descriptors
+<table class="table table-bordered">
+  <thead>
+    <tr>
+      <th class="text-left" style="width: 18%">Scope</th>
+      <th class="text-left" style="width: 22%">Infix</th>
+      <th class="text-left" style="width: 20%">Metrics</th>
+      <th class="text-left" style="width: 32%">Description</th>
+      <th class="text-left" style="width: 8%">Type</th>
+    </tr>
+  </thead>
+  <tbody>
+    <tr>
+      <th rowspan="1"><strong>Job-/TaskManager</strong></th>
+      <td rowspan="1">Status.FileDescriptor.Max</td>
+      <td>Count</td>
+      <td>The max number of file descriptors.</td>
+      <td>Gauge</td>
+    </tr>
+    <tr>
+      <th rowspan="1"><strong>Job-/TaskManager</strong></th>
+      <td rowspan="1">Status.FileDescriptor.Open</td>
+      <td>Count</td>
+      <td>The total open of file descriptors.</td>
+      <td>Gauge</td>
+    </tr>
+  </tbody>
+</table>
+
 ### Threads
 <table class="table table-bordered">
   <thead>
diff --git a/docs/content/docs/ops/metrics.md b/docs/content/docs/ops/metrics.md
index 73d0c1ac968..9b5003755da 100644
--- a/docs/content/docs/ops/metrics.md
+++ b/docs/content/docs/ops/metrics.md
@@ -624,6 +624,35 @@ Some metrics might not be exposed when using other JVM 
implementations (e.g. IBM
   </tbody>                                                         
 </table>
 
+### File Descriptors
+<table class="table table-bordered">
+  <thead>
+    <tr>
+      <th class="text-left" style="width: 18%">Scope</th>
+      <th class="text-left" style="width: 22%">Infix</th>
+      <th class="text-left" style="width: 20%">Metrics</th>
+      <th class="text-left" style="width: 32%">Description</th>
+      <th class="text-left" style="width: 8%">Type</th>
+    </tr>
+  </thead>
+  <tbody>
+    <tr>
+      <th rowspan="1"><strong>Job-/TaskManager</strong></th>
+      <td rowspan="1">Status.FileDescriptor.Max</td>
+      <td>Count</td>
+      <td>The max number of file descriptors.</td>
+      <td>Gauge</td>
+    </tr>
+    <tr>
+      <th rowspan="1"><strong>Job-/TaskManager</strong></th>
+      <td rowspan="1">Status.FileDescriptor.Open</td>
+      <td>Count</td>
+      <td>The total open of file descriptors.</td>
+      <td>Gauge</td>
+    </tr>
+  </tbody>
+</table>
+
 ### Threads
 <table class="table table-bordered">
   <thead>
diff --git 
a/flink-runtime/src/main/java/org/apache/flink/runtime/metrics/MetricNames.java 
b/flink-runtime/src/main/java/org/apache/flink/runtime/metrics/MetricNames.java
index 22f59ee4e9b..4301808e587 100644
--- 
a/flink-runtime/src/main/java/org/apache/flink/runtime/metrics/MetricNames.java
+++ 
b/flink-runtime/src/main/java/org/apache/flink/runtime/metrics/MetricNames.java
@@ -58,6 +58,9 @@ public class MetricNames {
     public static final String MEMORY_COMMITTED = "Committed";
     public static final String MEMORY_MAX = "Max";
 
+    public static final String FILE_DESCRIPTOR_MAX = "Max";
+    public static final String FILE_DESCRIPTOR_OPEN = "Open";
+
     public static final String IS_BACK_PRESSURED = "isBackPressured";
 
     public static final String CHECKPOINT_ALIGNMENT_TIME = 
"checkpointAlignmentTime";
diff --git 
a/flink-runtime/src/main/java/org/apache/flink/runtime/metrics/util/MetricUtils.java
 
b/flink-runtime/src/main/java/org/apache/flink/runtime/metrics/util/MetricUtils.java
index 6b0c818fad6..915548be41a 100644
--- 
a/flink-runtime/src/main/java/org/apache/flink/runtime/metrics/util/MetricUtils.java
+++ 
b/flink-runtime/src/main/java/org/apache/flink/runtime/metrics/util/MetricUtils.java
@@ -139,6 +139,7 @@ public class MetricUtils {
         instantiateMemoryMetrics(jvm.addGroup(METRIC_GROUP_MEMORY));
         instantiateThreadMetrics(jvm.addGroup("Threads"));
         instantiateCPUMetrics(jvm.addGroup("CPU"));
+        instantiateFileDescriptorMetrics(jvm.addGroup("FileDescriptor"));
     }
 
     public static void instantiateFlinkMemoryMetricGroup(
@@ -338,6 +339,30 @@ public class MetricUtils {
         }
     }
 
+    static void instantiateFileDescriptorMetrics(MetricGroup metrics) {
+        try {
+            final com.sun.management.OperatingSystemMXBean mxBean =
+                    (com.sun.management.OperatingSystemMXBean)
+                            ManagementFactory.getOperatingSystemMXBean();
+
+            if (mxBean instanceof 
com.sun.management.UnixOperatingSystemMXBean) {
+                com.sun.management.UnixOperatingSystemMXBean unixMXBean =
+                        (com.sun.management.UnixOperatingSystemMXBean) mxBean;
+                metrics.<Long, Gauge<Long>>gauge("Max", 
unixMXBean::getMaxFileDescriptorCount);
+                metrics.<Long, Gauge<Long>>gauge("Open", 
unixMXBean::getOpenFileDescriptorCount);
+
+            } else {
+                throw new UnsupportedOperationException(
+                        "Can't find 
com.sun.management.UnixOperatingSystemMXBean in JVM.");
+            }
+        } catch (Exception e) {
+            LOG.warn(
+                    "Cannot access 
com.sun.management.UnixOperatingSystemMXBean.getOpenFileDescriptorCount()"
+                            + " - FileDescriptor metrics will not be 
available.",
+                    e);
+        }
+    }
+
     private static void instantiateMemoryUsageMetrics(
             final MetricGroup metricGroup, final Supplier<MemoryUsage> 
memoryUsageSupplier) {
         metricGroup.<Long, Gauge<Long>>gauge(
diff --git 
a/flink-runtime/src/test/java/org/apache/flink/runtime/metrics/util/MetricUtilsTest.java
 
b/flink-runtime/src/test/java/org/apache/flink/runtime/metrics/util/MetricUtilsTest.java
index b8f791c7418..2195e8c4b26 100644
--- 
a/flink-runtime/src/test/java/org/apache/flink/runtime/metrics/util/MetricUtilsTest.java
+++ 
b/flink-runtime/src/test/java/org/apache/flink/runtime/metrics/util/MetricUtilsTest.java
@@ -177,6 +177,16 @@ class MetricUtilsTest {
         assertThat(heapMetrics.get(MetricNames.MEMORY_MAX)).isNotNull();
     }
 
+    @Test
+    void testFileDescriptorMetricsCompleteness() {
+        final InterceptingOperatorMetricGroup heapMetrics = new 
InterceptingOperatorMetricGroup();
+
+        MetricUtils.instantiateFileDescriptorMetrics(heapMetrics);
+
+        
assertThat(heapMetrics.get(MetricNames.FILE_DESCRIPTOR_MAX)).isNotNull();
+        
assertThat(heapMetrics.get(MetricNames.FILE_DESCRIPTOR_OPEN)).isNotNull();
+    }
+
     /**
      * Tests that heap/non-heap metrics do not rely on a static MemoryUsage 
instance.
      *

Reply via email to