-
Notifications
You must be signed in to change notification settings - Fork 860
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
base: main
Are you sure you want to change the base?
add file descriptor metrics #11876
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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") | ||
.setDescription("number of open file descriptors") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
.setUnit("{file}") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
This looks the same |
||
.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") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Host metrics receiver does not have this. It is similar to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. +1 on @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? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looking at https://github.com/openjdk/jdk/blob/2f2223d7524c4405cc7ca6ab77da62016bbfa911/src/jdk.management/unix/native/libmanagement_ext/OperatingSystemImpl.c#L270 I think these are per process. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've opened an issue open-telemetry/semantic-conventions#1275 |
||
.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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is mostly copy paste from https://github.com/open-telemetry/opentelemetry-java-instrumentation/blob/main/instrumentation/runtime-telemetry/runtime-telemetry-java8/library/src/main/java/io/opentelemetry/instrumentation/runtimemetrics/java8/internal/CpuMethods.java You could add your methods in that class and rename the class appropriately. |
||
|
||
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))))); | ||
} | ||
} |
There was a problem hiding this comment.
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, perhapsjvm.process.open_file_descriptors
.