Skip to content

Commit a21e6f5

Browse files
committed
[FLINK-37709][metrics] add file descriptor jvm metrics
1 parent 932fd59 commit a21e6f5

File tree

5 files changed

+96
-0
lines changed

5 files changed

+96
-0
lines changed

docs/content.zh/docs/ops/metrics.md

+29
Original file line numberDiff line numberDiff line change
@@ -632,6 +632,35 @@ Some metrics might not be exposed when using other JVM implementations (e.g. IBM
632632
</tbody>
633633
</table>
634634

635+
### File Descriptors
636+
<table class="table table-bordered">
637+
<thead>
638+
<tr>
639+
<th class="text-left" style="width: 18%">Scope</th>
640+
<th class="text-left" style="width: 22%">Infix</th>
641+
<th class="text-left" style="width: 20%">Metrics</th>
642+
<th class="text-left" style="width: 32%">Description</th>
643+
<th class="text-left" style="width: 8%">Type</th>
644+
</tr>
645+
</thead>
646+
<tbody>
647+
<tr>
648+
<th rowspan="1"><strong>Job-/TaskManager</strong></th>
649+
<td rowspan="1">Status.FileDescriptor.Max</td>
650+
<td>Count</td>
651+
<td>The max number of file descriptors.</td>
652+
<td>Gauge</td>
653+
</tr>
654+
<tr>
655+
<th rowspan="1"><strong>Job-/TaskManager</strong></th>
656+
<td rowspan="1">Status.FileDescriptor.Open</td>
657+
<td>Count</td>
658+
<td>The total open of file descriptors.</td>
659+
<td>Gauge</td>
660+
</tr>
661+
</tbody>
662+
</table>
663+
635664
### Threads
636665
<table class="table table-bordered">
637666
<thead>

docs/content/docs/ops/metrics.md

+29
Original file line numberDiff line numberDiff line change
@@ -624,6 +624,35 @@ Some metrics might not be exposed when using other JVM implementations (e.g. IBM
624624
</tbody>
625625
</table>
626626

627+
### File Descriptors
628+
<table class="table table-bordered">
629+
<thead>
630+
<tr>
631+
<th class="text-left" style="width: 18%">Scope</th>
632+
<th class="text-left" style="width: 22%">Infix</th>
633+
<th class="text-left" style="width: 20%">Metrics</th>
634+
<th class="text-left" style="width: 32%">Description</th>
635+
<th class="text-left" style="width: 8%">Type</th>
636+
</tr>
637+
</thead>
638+
<tbody>
639+
<tr>
640+
<th rowspan="1"><strong>Job-/TaskManager</strong></th>
641+
<td rowspan="1">Status.FileDescriptor.Max</td>
642+
<td>Count</td>
643+
<td>The max number of file descriptors.</td>
644+
<td>Gauge</td>
645+
</tr>
646+
<tr>
647+
<th rowspan="1"><strong>Job-/TaskManager</strong></th>
648+
<td rowspan="1">Status.FileDescriptor.Open</td>
649+
<td>Count</td>
650+
<td>The total open of file descriptors.</td>
651+
<td>Gauge</td>
652+
</tr>
653+
</tbody>
654+
</table>
655+
627656
### Threads
628657
<table class="table table-bordered">
629658
<thead>

flink-runtime/src/main/java/org/apache/flink/runtime/metrics/MetricNames.java

+3
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ private MetricNames() {}
5858
public static final String MEMORY_COMMITTED = "Committed";
5959
public static final String MEMORY_MAX = "Max";
6060

61+
public static final String FILE_DESCRIPTOR_MAX = "Max";
62+
public static final String FILE_DESCRIPTOR_OPEN = "Open";
63+
6164
public static final String IS_BACK_PRESSURED = "isBackPressured";
6265

6366
public static final String CHECKPOINT_ALIGNMENT_TIME = "checkpointAlignmentTime";

flink-runtime/src/main/java/org/apache/flink/runtime/metrics/util/MetricUtils.java

+25
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ public static void instantiateStatusMetrics(MetricGroup metricGroup) {
139139
instantiateMemoryMetrics(jvm.addGroup(METRIC_GROUP_MEMORY));
140140
instantiateThreadMetrics(jvm.addGroup("Threads"));
141141
instantiateCPUMetrics(jvm.addGroup("CPU"));
142+
instantiateFileDescriptorMetrics(jvm.addGroup("FileDescriptor"));
142143
}
143144

144145
public static void instantiateFlinkMemoryMetricGroup(
@@ -338,6 +339,30 @@ static void instantiateMetaspaceMemoryMetrics(final MetricGroup parentMetricGrou
338339
}
339340
}
340341

342+
static void instantiateFileDescriptorMetrics(MetricGroup metrics) {
343+
try {
344+
final com.sun.management.OperatingSystemMXBean mxBean =
345+
(com.sun.management.OperatingSystemMXBean)
346+
ManagementFactory.getOperatingSystemMXBean();
347+
348+
if (mxBean instanceof com.sun.management.UnixOperatingSystemMXBean) {
349+
com.sun.management.UnixOperatingSystemMXBean unixMXBean =
350+
(com.sun.management.UnixOperatingSystemMXBean) mxBean;
351+
metrics.<Long, Gauge<Long>>gauge("Max", unixMXBean::getMaxFileDescriptorCount);
352+
metrics.<Long, Gauge<Long>>gauge("Open", unixMXBean::getOpenFileDescriptorCount);
353+
354+
} else {
355+
throw new UnsupportedOperationException(
356+
"Can't find com.sun.management.UnixOperatingSystemMXBean in JVM.");
357+
}
358+
} catch (Exception e) {
359+
LOG.warn(
360+
"Cannot access com.sun.management.UnixOperatingSystemMXBean.getOpenFileDescriptorCount()"
361+
+ " - FileDescriptor metrics will not be available.",
362+
e);
363+
}
364+
}
365+
341366
private static void instantiateMemoryUsageMetrics(
342367
final MetricGroup metricGroup, final Supplier<MemoryUsage> memoryUsageSupplier) {
343368
metricGroup.<Long, Gauge<Long>>gauge(

flink-runtime/src/test/java/org/apache/flink/runtime/metrics/util/MetricUtilsTest.java

+10
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,16 @@ void testHeapMetricsCompleteness() {
177177
assertThat(heapMetrics.get(MetricNames.MEMORY_MAX)).isNotNull();
178178
}
179179

180+
@Test
181+
void testFileDescriptorMetricsCompleteness() {
182+
final InterceptingOperatorMetricGroup heapMetrics = new InterceptingOperatorMetricGroup();
183+
184+
MetricUtils.instantiateFileDescriptorMetrics(heapMetrics);
185+
186+
assertThat(heapMetrics.get(MetricNames.FILE_DESCRIPTOR_MAX)).isNotNull();
187+
assertThat(heapMetrics.get(MetricNames.FILE_DESCRIPTOR_OPEN)).isNotNull();
188+
}
189+
180190
/**
181191
* Tests that heap/non-heap metrics do not rely on a static MemoryUsage instance.
182192
*

0 commit comments

Comments
 (0)