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 10 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 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
Expand Up @@ -7,8 +7,8 @@

import io.opentelemetry.api.OpenTelemetry;
import io.opentelemetry.api.metrics.Meter;
import io.opentelemetry.instrumentation.runtimemetrics.java8.internal.CpuMethods;
import io.opentelemetry.instrumentation.runtimemetrics.java8.internal.JmxRuntimeMetricsUtil;
import io.opentelemetry.instrumentation.runtimemetrics.java8.internal.OperatingSystemMethods;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -48,8 +48,8 @@ public static List<AutoCloseable> registerObservers(OpenTelemetry openTelemetry)
return INSTANCE.registerObservers(
openTelemetry,
Runtime.getRuntime()::availableProcessors,
CpuMethods.processCpuTime(),
CpuMethods.processCpuUtilization());
OperatingSystemMethods.processCpuTime(),
OperatingSystemMethods.processCpuUtilization());
}

// Visible for testing
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public static List<AutoCloseable> registerObservers(OpenTelemetry openTelemetry)
return registerObservers(
openTelemetry,
ManagementFactory.getOperatingSystemMXBean(),
CpuMethods.systemCpuUtilization());
OperatingSystemMethods.systemCpuUtilization());
}

// Visible for testing
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* 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. This metrics are
xiangtianyu marked this conversation as resolved.
Show resolved Hide resolved
* experimental, see <a
* href="https://github.com/open-telemetry/semantic-conventions/issues/1275">File Descriptor metrics
* semantic conventions</a>
xiangtianyu marked this conversation as resolved.
Show resolved Hide resolved
*
* <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,
OperatingSystemMethods.openFileDescriptorCount(),
OperatingSystemMethods.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
.upDownCounterBuilder("process.open_file_descriptor.count")
Copy link
Contributor

Choose a reason for hiding this comment

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

Since this name isn't exactly the same as process.open_file_descriptors that is used by the host metric receiver we might as well go with a different name. I think it would be best to discuss these names and descriptions at the SIG meeting.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok, how can i propose this as a topic?

Copy link
Member

Choose a reason for hiding this comment

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

based on https://github.com/open-telemetry/semantic-conventions/pull/1618/files

as a working group we believe it is important that process namespace and runtime namespace metrics remain separate, because process metrics are meant to represent an OS-level process as the instrumentation source, whereas runtime metrics represent the language runtime as the instrumentation source.

I'd suggest using jvm.open_file_descriptor.count

Copy link
Contributor

Choose a reason for hiding this comment

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

@trask do you also have suggestions for the metric descriptions and unit. Currently the unit is {count}.

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
Member

Choose a reason for hiding this comment

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

for description, I think we can use

Number of file descriptors in use by the process.

from https://github.com/open-telemetry/semantic-conventions/blob/373a69574ec9605895102205d561b2476972e20c/model/process/metrics.yaml#L82

.setDescription("Number of file descriptors in use by the process.")
.setUnit("{count}")
.buildWithCallback(
observableMeasurement -> {
Long openCount = openFileDescriptorCount.get();
if (openCount != null && openCount >= 0) {
xiangtianyu marked this conversation as resolved.
Show resolved Hide resolved
observableMeasurement.record(openCount);
}
}));
}

if (maxFileDescriptorCount != null) {
observables.add(
meter
.upDownCounterBuilder("process.open_file_descriptor.limit")
xiangtianyu marked this conversation as resolved.
Show resolved Hide resolved
.setDescription("Measure of max file descriptors.")
.setUnit("{count}")
.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
Expand Up @@ -16,52 +16,59 @@
* This class is internal and is hence not for public use. Its APIs are unstable and can change at
* any time.
*/
public final class CpuMethods {
public final class OperatingSystemMethods {

private static final String OS_BEAN_J9 = "com.ibm.lang.management.OperatingSystemMXBean";
private static final String OS_BEAN_HOTSPOT = "com.sun.management.OperatingSystemMXBean";
private static final String UNIX_OS_BEAN_J9 = "com.ibm.lang.management.UnixOperatingSystemMXBean";
private static final String UNIX_OS_BEAN_HOTSPOT = "com.sun.management.UnixOperatingSystemMXBean";
private static final String METHOD_PROCESS_CPU_TIME = "getProcessCpuTime";
private static final String METHOD_PROCESS_CPU_LOAD = "getProcessCpuLoad";
private static final String METHOD_CPU_LOAD = "getCpuLoad";
private static final String METHOD_SYSTEM_CPU_LOAD = "getSystemCpuLoad";
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> processCpuTime;
@Nullable private static final Supplier<Double> processCpuUtilization;
@Nullable private static final Supplier<Double> systemCpuUtilization;
@Nullable private static final Supplier<Long> openFileDescriptorCount;
@Nullable private static final Supplier<Long> maxFileDescriptorCount;

static {
OperatingSystemMXBean osBean = ManagementFactory.getOperatingSystemMXBean();

Supplier<Long> processCpuTimeSupplier =
methodInvoker(osBean, OS_BEAN_HOTSPOT, METHOD_PROCESS_CPU_TIME, Long.class);
if (processCpuTimeSupplier == null) {
// More users will be on hotspot than j9, so check for j9 second
processCpuTimeSupplier =
methodInvoker(osBean, OS_BEAN_J9, METHOD_PROCESS_CPU_TIME, Long.class);
}
processCpuTime = processCpuTimeSupplier;
processCpuTime =
findMethod(osBean, OS_BEAN_HOTSPOT, OS_BEAN_J9, METHOD_PROCESS_CPU_TIME, Long.class);

Supplier<Double> processCpuSupplier =
methodInvoker(osBean, OS_BEAN_HOTSPOT, METHOD_PROCESS_CPU_LOAD, Double.class);
if (processCpuSupplier == null) {
// More users will be on hotspot than j9, so check for j9 second
processCpuSupplier = methodInvoker(osBean, OS_BEAN_J9, METHOD_PROCESS_CPU_LOAD, Double.class);
}
processCpuUtilization = processCpuSupplier;
processCpuUtilization =
findMethod(osBean, OS_BEAN_HOTSPOT, OS_BEAN_J9, METHOD_PROCESS_CPU_LOAD, Double.class);

// As of java 14, com.sun.management.OperatingSystemMXBean#getCpuLoad() is preferred and
// #getSystemCpuLoad() is deprecated
Supplier<Double> systemCpuSupplier =
methodInvoker(osBean, OS_BEAN_HOTSPOT, METHOD_CPU_LOAD, Double.class);
if (systemCpuSupplier == null) {
systemCpuSupplier =
methodInvoker(osBean, OS_BEAN_HOTSPOT, METHOD_SYSTEM_CPU_LOAD, Double.class);
}
if (systemCpuSupplier == null) {
// More users will be on hotspot than j9, so check for j9 second
systemCpuSupplier = methodInvoker(osBean, OS_BEAN_J9, METHOD_SYSTEM_CPU_LOAD, Double.class);
findMethod(osBean, OS_BEAN_HOTSPOT, OS_BEAN_J9, METHOD_SYSTEM_CPU_LOAD, Double.class);
}
systemCpuUtilization = systemCpuSupplier;

openFileDescriptorCount =
findMethod(
osBean,
UNIX_OS_BEAN_HOTSPOT,
UNIX_OS_BEAN_J9,
METHOD_OPEN_FILE_DESCRIPTOR_COUNT,
Long.class);

maxFileDescriptorCount =
findMethod(
osBean,
UNIX_OS_BEAN_HOTSPOT,
UNIX_OS_BEAN_J9,
METHOD_MAX_FILE_DESCRIPTOR_COUNT,
Long.class);
}

@Nullable
Expand All @@ -87,6 +94,22 @@ private static <T extends Number> Supplier<T> methodInvoker(
}
}

// judge whether use hotspots or openj9
private static <T extends Number> Supplier<T> findMethod(
OperatingSystemMXBean osBean,
String osBeanClassName,
String osBeanJ9ClassName,
String methodName,
Class<T> returnType) {
Supplier<T> processSupplier = methodInvoker(osBean, osBeanClassName, methodName, returnType);
if (processSupplier == null) {
// More users will be on hotspot than j9, so check for j9 second
processSupplier = methodInvoker(osBean, osBeanJ9ClassName, methodName, returnType);
}

return processSupplier;
}

public static Supplier<Long> processCpuTime() {
return processCpuTime;
}
Expand All @@ -99,5 +122,13 @@ public static Supplier<Double> systemCpuUtilization() {
return systemCpuUtilization;
}

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

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

private OperatingSystemMethods() {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* 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.RegisterExtension;

class ExperimentalFileDescriptorTest {
@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",
"process.open_file_descriptor.count",
metrics ->
metrics.anySatisfy(
metricData ->
assertThat(metricData)
.hasInstrumentationScope(EXPECTED_SCOPE)
.hasDescription("Number of file descriptors in use by the process.")
.hasUnit("{count}")
.hasLongSumSatisfying(
sum -> sum.hasPointsSatisfying(point -> point.hasValue(10L)))));
testing.waitAndAssertMetrics(
"io.opentelemetry.runtime-telemetry-java8",
"process.open_file_descriptor.limit",
metrics ->
metrics.anySatisfy(
metricData ->
assertThat(metricData)
.hasInstrumentationScope(EXPECTED_SCOPE)
.hasDescription("Measure of max file descriptors.")
.hasUnit("{count}")
.hasLongSumSatisfying(
sum -> sum.hasPointsSatisfying(point -> point.hasValue(10000L)))));
}
}
Loading