|
| 1 | +/* |
| 2 | + * Copyright The OpenTelemetry Authors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package io.opentelemetry.instrumentation.runtimemetrics.java8.internal; |
| 7 | + |
| 8 | +import static io.opentelemetry.instrumentation.runtimemetrics.java8.ScopeUtil.EXPECTED_SCOPE; |
| 9 | +import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.assertThat; |
| 10 | +import static org.junit.jupiter.api.Assumptions.assumeTrue; |
| 11 | +import static org.mockito.Mockito.when; |
| 12 | + |
| 13 | +import com.sun.management.UnixOperatingSystemMXBean; |
| 14 | +import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension; |
| 15 | +import io.opentelemetry.instrumentation.testing.junit.LibraryInstrumentationExtension; |
| 16 | +import java.lang.management.ManagementFactory; |
| 17 | +import java.lang.management.OperatingSystemMXBean; |
| 18 | +import org.junit.jupiter.api.BeforeEach; |
| 19 | +import org.junit.jupiter.api.Test; |
| 20 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 21 | +import org.junit.jupiter.api.extension.RegisterExtension; |
| 22 | +import org.mockito.Mock; |
| 23 | +import org.mockito.junit.jupiter.MockitoExtension; |
| 24 | + |
| 25 | +@ExtendWith(MockitoExtension.class) |
| 26 | +class ExperimentalFileDescriptorTest { |
| 27 | + |
| 28 | + @RegisterExtension |
| 29 | + static final InstrumentationExtension testing = LibraryInstrumentationExtension.create(); |
| 30 | + |
| 31 | + @Mock private UnixOperatingSystemMXBean osBean; |
| 32 | + |
| 33 | + @BeforeEach |
| 34 | + void setUp() { |
| 35 | + // Skip tests on non-Unix systems since UnixOperatingSystemMXBean is only available on Unix |
| 36 | + OperatingSystemMXBean realOsBean = ManagementFactory.getOperatingSystemMXBean(); |
| 37 | + assumeTrue( |
| 38 | + realOsBean instanceof UnixOperatingSystemMXBean, |
| 39 | + "Skipping test: UnixOperatingSystemMXBean is only available on Unix systems"); |
| 40 | + } |
| 41 | + |
| 42 | + @Test |
| 43 | + // verify that mock is called with the correct value |
| 44 | + void registerObservers() { |
| 45 | + when(osBean.getOpenFileDescriptorCount()).thenReturn(42L); |
| 46 | + ExperimentalFileDescriptor.registerObservers(testing.getOpenTelemetry(), osBean); |
| 47 | + |
| 48 | + testing.waitAndAssertMetrics( |
| 49 | + "io.opentelemetry.runtime-telemetry-java8", |
| 50 | + "jvm.file_descriptor.count", |
| 51 | + metrics -> |
| 52 | + metrics.anySatisfy( |
| 53 | + metricData -> |
| 54 | + assertThat(metricData) |
| 55 | + .hasInstrumentationScope(EXPECTED_SCOPE) |
| 56 | + .hasDescription("Number of open file descriptors as reported by the JVM.") |
| 57 | + .hasUnit("{file_descriptor}") |
| 58 | + .hasLongSumSatisfying( |
| 59 | + sum -> sum.hasPointsSatisfying(point -> point.hasValue(42))))); |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + // Verify that no metrics are emitted with non-zero values |
| 64 | + void registerObservers_NegativeValue() { |
| 65 | + when(osBean.getOpenFileDescriptorCount()).thenReturn(-1L); |
| 66 | + ExperimentalFileDescriptor.registerObservers(testing.getOpenTelemetry(), osBean); |
| 67 | + |
| 68 | + testing.waitAndAssertMetrics( |
| 69 | + "io.opentelemetry.runtime-telemetry-java8", |
| 70 | + "jvm.file_descriptor.count", |
| 71 | + metrics -> |
| 72 | + metrics.allSatisfy( |
| 73 | + metricData -> |
| 74 | + assertThat(metricData) |
| 75 | + .hasInstrumentationScope(EXPECTED_SCOPE) |
| 76 | + .hasDescription("Number of open file descriptors as reported by the JVM.") |
| 77 | + .hasUnit("{file_descriptor}") |
| 78 | + .hasLongSumSatisfying( |
| 79 | + sum -> sum.hasPointsSatisfying(point -> point.hasValue(0))))); |
| 80 | + } |
| 81 | +} |
0 commit comments