-
Notifications
You must be signed in to change notification settings - Fork 872
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add JVM classes instrumentation (#6069)
* Add JVM classes instrumentation * Add unit of 1 * Assert monotonicity
- Loading branch information
Showing
4 changed files
with
157 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
...etrics/library/src/main/java/io/opentelemetry/instrumentation/runtimemetrics/Classes.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.instrumentation.runtimemetrics; | ||
|
||
import io.opentelemetry.api.OpenTelemetry; | ||
import io.opentelemetry.api.metrics.Meter; | ||
import java.lang.management.ClassLoadingMXBean; | ||
import java.lang.management.ManagementFactory; | ||
|
||
/** | ||
* Registers measurements that generate metrics about JVM classes. | ||
* | ||
* <p>Example usage: | ||
* | ||
* <pre>{@code | ||
* Classes.registerObservers(GlobalOpenTelemetry.get()); | ||
* }</pre> | ||
* | ||
* <p>Example metrics being exported: | ||
* | ||
* <pre> | ||
* process.runtime.jvm.classes.loaded 100 | ||
* process.runtime.jvm.classes.unloaded 2 | ||
* process.runtime.jvm.classes.current_loaded 98 | ||
* </pre> | ||
*/ | ||
public final class Classes { | ||
|
||
// Visible for testing | ||
static final Classes INSTANCE = new Classes(); | ||
|
||
/** Register observers for java runtime class metrics. */ | ||
public static void registerObservers(OpenTelemetry openTelemetry) { | ||
INSTANCE.registerObservers(openTelemetry, ManagementFactory.getClassLoadingMXBean()); | ||
} | ||
|
||
// Visible for testing | ||
void registerObservers(OpenTelemetry openTelemetry, ClassLoadingMXBean classBean) { | ||
Meter meter = openTelemetry.getMeter("io.opentelemetry.runtime-metrics"); | ||
|
||
meter | ||
.counterBuilder("process.runtime.jvm.classes.loaded") | ||
.setDescription("Number of classes loaded since JVM start") | ||
.setUnit("1") | ||
.buildWithCallback( | ||
observableMeasurement -> | ||
observableMeasurement.record(classBean.getTotalLoadedClassCount())); | ||
|
||
meter | ||
.counterBuilder("process.runtime.jvm.classes.unloaded") | ||
.setDescription("Number of classes unloaded since JVM start") | ||
.setUnit("1") | ||
.buildWithCallback( | ||
observableMeasurement -> | ||
observableMeasurement.record(classBean.getUnloadedClassCount())); | ||
|
||
meter | ||
.upDownCounterBuilder("process.runtime.jvm.classes.current_loaded") | ||
.setDescription("Number of classes currently loaded") | ||
.setUnit("1") | ||
.buildWithCallback( | ||
observableMeasurement -> observableMeasurement.record(classBean.getLoadedClassCount())); | ||
} | ||
|
||
private Classes() {} | ||
} |
83 changes: 83 additions & 0 deletions
83
...cs/library/src/test/java/io/opentelemetry/instrumentation/runtimemetrics/ClassesTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.instrumentation.runtimemetrics; | ||
|
||
import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.assertThat; | ||
import static org.mockito.Mockito.when; | ||
|
||
import io.opentelemetry.api.common.Attributes; | ||
import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension; | ||
import io.opentelemetry.instrumentation.testing.junit.LibraryInstrumentationExtension; | ||
import java.lang.management.ClassLoadingMXBean; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
class ClassesTest { | ||
|
||
@RegisterExtension | ||
static final InstrumentationExtension testing = LibraryInstrumentationExtension.create(); | ||
|
||
@Mock private ClassLoadingMXBean classBean; | ||
|
||
@Test | ||
void registerObservers() { | ||
when(classBean.getTotalLoadedClassCount()).thenReturn(3L); | ||
when(classBean.getUnloadedClassCount()).thenReturn(2L); | ||
when(classBean.getLoadedClassCount()).thenReturn(1); | ||
|
||
Classes.INSTANCE.registerObservers(testing.getOpenTelemetry(), classBean); | ||
|
||
testing.waitAndAssertMetrics( | ||
"io.opentelemetry.runtime-metrics", | ||
"process.runtime.jvm.classes.loaded", | ||
metrics -> | ||
metrics.anySatisfy( | ||
metricData -> | ||
assertThat(metricData) | ||
.hasDescription("Number of classes loaded since JVM start") | ||
.hasUnit("1") | ||
.hasLongSumSatisfying( | ||
sum -> | ||
sum.isMonotonic() | ||
.hasPointsSatisfying( | ||
point -> | ||
point.hasValue(3).hasAttributes(Attributes.empty()))))); | ||
testing.waitAndAssertMetrics( | ||
"io.opentelemetry.runtime-metrics", | ||
"process.runtime.jvm.classes.unloaded", | ||
metrics -> | ||
metrics.anySatisfy( | ||
metricData -> | ||
assertThat(metricData) | ||
.hasDescription("Number of classes unloaded since JVM start") | ||
.hasUnit("1") | ||
.hasLongSumSatisfying( | ||
sum -> | ||
sum.isMonotonic() | ||
.hasPointsSatisfying( | ||
point -> | ||
point.hasValue(2).hasAttributes(Attributes.empty()))))); | ||
testing.waitAndAssertMetrics( | ||
"io.opentelemetry.runtime-metrics", | ||
"process.runtime.jvm.classes.current_loaded", | ||
metrics -> | ||
metrics.anySatisfy( | ||
metricData -> | ||
assertThat(metricData) | ||
.hasDescription("Number of classes currently loaded") | ||
.hasUnit("1") | ||
.hasLongSumSatisfying( | ||
sum -> | ||
sum.isNotMonotonic() | ||
.hasPointsSatisfying( | ||
point -> | ||
point.hasValue(1).hasAttributes(Attributes.empty()))))); | ||
} | ||
} |