Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add file descriptor metrics #11876

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import io.opentelemetry.instrumentation.runtimemetrics.java8.Threads;
import io.opentelemetry.instrumentation.runtimemetrics.java8.internal.ExperimentalBufferPools;
import io.opentelemetry.instrumentation.runtimemetrics.java8.internal.ExperimentalCpu;
import io.opentelemetry.instrumentation.runtimemetrics.java8.internal.ExperimentalFileDescriptor;
import io.opentelemetry.instrumentation.runtimemetrics.java8.internal.ExperimentalMemoryPools;
import java.util.ArrayList;
import java.util.Arrays;
Expand Down Expand Up @@ -114,6 +115,7 @@ private List<AutoCloseable> buildObservables() {
observables.addAll(ExperimentalBufferPools.registerObservers(openTelemetry));
observables.addAll(ExperimentalCpu.registerObservers(openTelemetry));
observables.addAll(ExperimentalMemoryPools.registerObservers(openTelemetry));
observables.addAll(ExperimentalFileDescriptor.registerObservers(openTelemetry));
}
return observables;
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import io.opentelemetry.instrumentation.runtimemetrics.java8.Threads;
import io.opentelemetry.instrumentation.runtimemetrics.java8.internal.ExperimentalBufferPools;
import io.opentelemetry.instrumentation.runtimemetrics.java8.internal.ExperimentalCpu;
import io.opentelemetry.instrumentation.runtimemetrics.java8.internal.ExperimentalFileDescriptor;
import io.opentelemetry.instrumentation.runtimemetrics.java8.internal.ExperimentalMemoryPools;
import io.opentelemetry.instrumentation.runtimemetrics.java8.internal.JmxRuntimeMetricsUtil;
import io.opentelemetry.javaagent.extension.AgentListener;
Expand Down Expand Up @@ -51,6 +52,7 @@ public void afterAgent(AutoConfiguredOpenTelemetrySdk autoConfiguredSdk) {
observables.addAll(ExperimentalBufferPools.registerObservers(openTelemetry));
observables.addAll(ExperimentalCpu.registerObservers(openTelemetry));
observables.addAll(ExperimentalMemoryPools.registerObservers(openTelemetry));
observables.addAll(ExperimentalFileDescriptor.registerObservers(openTelemetry));
}

Thread cleanupTelemetry = new Thread(() -> JmxRuntimeMetricsUtil.closeObservers(observables));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.instrumentation.runtimemetrics.java8.internal;

import io.opentelemetry.api.OpenTelemetry;
import io.opentelemetry.api.metrics.Meter;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Supplier;

/**
* Registers measurements that generate experimental metrics about file descriptor.
*
* <p>This class is internal and is hence not for public use. Its APIs are unstable and can change
* at any time.
*/
public class ExperimentalFileDescriptor {
/** Register observers for java runtime experimental file descriptor metrics. */
public static List<AutoCloseable> registerObservers(OpenTelemetry openTelemetry) {
return registerObservers(
openTelemetry,
FileDescriptorMethods.openFileDescriptorCount(),
FileDescriptorMethods.maxFileDescriptorCount());
}

// Visible for testing
static List<AutoCloseable> registerObservers(
OpenTelemetry openTelemetry,
Supplier<Long> openFileDescriptorCount,
Supplier<Long> maxFileDescriptorCount) {
Meter meter = JmxRuntimeMetricsUtil.getMeter(openTelemetry);
List<AutoCloseable> observables = new ArrayList<>();

if (openFileDescriptorCount != null) {
observables.add(
meter
.gaugeBuilder("os.file.descriptor.open")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://github.com/open-telemetry/opentelemetry-collector-contrib/blob/main/receiver/hostmetricsreceiver/internal/scraper/processscraper/documentation.md#processopen_file_descriptors
defines open file descriptor count as an up down counter. os.file.descriptor.open does not align with existing metric names, perhaps jvm.process.open_file_descriptors.

.setDescription("number of open file descriptors")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

all other descriptions start with a capital letter and end with dot. Host metric receiver uses the following description Number of file descriptors in use by the process.

.setUnit("{file}")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd use file_descriptor though hostmerics receiver uses {count} @trask any suggestions?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.buildWithCallback(
observableMeasurement -> {
Long openCount = openFileDescriptorCount.get();
if (openCount != null && openCount >= 0) {
observableMeasurement.record(openCount);
}
}));
}

if (maxFileDescriptorCount != null) {
observables.add(
meter
.gaugeBuilder("os.file.descriptor.max")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Host metrics receiver does not have this. It is similar to jvm.memory.limit and jvm.buffer.memory.limit that we already have. I'd use limit instead of max in the metric name. The descriptions of the existing limit metrics are Measure of max obtainable memory. and Measure of total memory capacity of buffers. The description of this metric should follow similar style.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 on .limit

@xiangtianyu are these process metrics, or os-wide metrics? can you open an issue in https://github.com/open-telemetry/semantic-conventions to propose any new metrics, and add a comment in the code pointing to that issue?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.setDescription("maximum number of file descriptors")
.setUnit("{file}")
.buildWithCallback(
observableMeasurement -> {
Long maxCount = maxFileDescriptorCount.get();
if (maxCount != null && maxCount >= 0) {
observableMeasurement.record(maxCount);
}
}));
}

return observables;
}

private ExperimentalFileDescriptor() {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.instrumentation.runtimemetrics.java8.internal;

import java.lang.management.ManagementFactory;
import java.lang.management.OperatingSystemMXBean;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.function.Supplier;
import javax.annotation.Nullable;

/**
* This class is internal and is hence not for public use. Its APIs are unstable and can change at
* any time.
*/
public class FileDescriptorMethods {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


private static final String OS_BEAN_J9 = "com.ibm.lang.management.UnixOperatingSystemMXBean";
private static final String OS_BEAN_HOTSPOT = "com.sun.management.UnixOperatingSystemMXBean";
private static final String METHOD_OPEN_FILE_DESCRIPTOR_COUNT = "getOpenFileDescriptorCount";
private static final String METHOD_MAX_FILE_DESCRIPTOR_COUNT = "getMaxFileDescriptorCount";

@Nullable private static final Supplier<Long> openFileDescriptorCount;
@Nullable private static final Supplier<Long> maxFileDescriptorCount;

static {
OperatingSystemMXBean osBean = ManagementFactory.getOperatingSystemMXBean();

Supplier<Long> openFileDescriptorCountSupplier =
methodInvoker(osBean, OS_BEAN_HOTSPOT, METHOD_OPEN_FILE_DESCRIPTOR_COUNT);
if (openFileDescriptorCountSupplier == null) {
// More users will be on hotspot than j9, so check for j9 second
openFileDescriptorCountSupplier =
methodInvoker(osBean, OS_BEAN_J9, METHOD_OPEN_FILE_DESCRIPTOR_COUNT);
}

openFileDescriptorCount = openFileDescriptorCountSupplier;

Supplier<Long> maxFileDescriptorCountSupplier =
methodInvoker(osBean, OS_BEAN_HOTSPOT, METHOD_MAX_FILE_DESCRIPTOR_COUNT);
if (maxFileDescriptorCountSupplier == null) {
// More users will be on hotspot than j9, so check for j9 second
maxFileDescriptorCountSupplier =
methodInvoker(osBean, OS_BEAN_J9, METHOD_MAX_FILE_DESCRIPTOR_COUNT);
}

maxFileDescriptorCount = maxFileDescriptorCountSupplier;
}

@Nullable
@SuppressWarnings("ReturnValueIgnored")
private static Supplier<Long> methodInvoker(
OperatingSystemMXBean osBean, String osBeanClassName, String methodName) {
try {
Class<?> osBeanClass = Class.forName(osBeanClassName);
osBeanClass.cast(osBean);
Method method = osBeanClass.getDeclaredMethod(methodName);
return () -> {
try {
return (Long) method.invoke(osBean);
} catch (IllegalAccessException | InvocationTargetException e) {
return null;
}
};
} catch (ClassNotFoundException | ClassCastException | NoSuchMethodException e) {
return null;
}
}

public static Supplier<Long> openFileDescriptorCount() {
return openFileDescriptorCount;
}

public static Supplier<Long> maxFileDescriptorCount() {
return maxFileDescriptorCount;
}

private FileDescriptorMethods() {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.instrumentation.runtimemetrics.java8.internal;

import static io.opentelemetry.instrumentation.runtimemetrics.java8.ScopeUtil.EXPECTED_SCOPE;
import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.assertThat;

import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension;
import io.opentelemetry.instrumentation.testing.junit.LibraryInstrumentationExtension;
import java.util.function.Supplier;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.mockito.junit.jupiter.MockitoExtension;

@ExtendWith(MockitoExtension.class)
xiangtianyu marked this conversation as resolved.
Show resolved Hide resolved
public class ExperimentalFileDescriptorTest {
xiangtianyu marked this conversation as resolved.
Show resolved Hide resolved
@RegisterExtension
static final InstrumentationExtension testing = LibraryInstrumentationExtension.create();

@Test
void registerObservers() {
Supplier<Long> openFileDescriptor = () -> 10L;
Supplier<Long> maxFileDescriptor = () -> 10000L;

ExperimentalFileDescriptor.registerObservers(
testing.getOpenTelemetry(), openFileDescriptor, maxFileDescriptor);

testing.waitAndAssertMetrics(
"io.opentelemetry.runtime-telemetry-java8",
"os.file.descriptor.open",
metrics ->
metrics.anySatisfy(
metricData ->
assertThat(metricData)
.hasInstrumentationScope(EXPECTED_SCOPE)
.hasDescription("number of open file descriptors")
.hasUnit("{file}")
.hasDoubleGaugeSatisfying(
gauge -> gauge.hasPointsSatisfying(point -> point.hasValue(10L)))));
testing.waitAndAssertMetrics(
"io.opentelemetry.runtime-telemetry-java8",
"os.file.descriptor.max",
metrics ->
metrics.anySatisfy(
metricData ->
assertThat(metricData)
.hasInstrumentationScope(EXPECTED_SCOPE)
.hasDescription("maximum number of file descriptors")
.hasUnit("{file}")
.hasDoubleGaugeSatisfying(
gauge -> gauge.hasPointsSatisfying(point -> point.hasValue(10000L)))));
}
}
Loading