-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add jvm threads instrumentation (open-telemetry#6070)
* Add jvm threads instrumentation * Use up down counter
- Loading branch information
Showing
4 changed files
with
109 additions
and
4 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
51 changes: 51 additions & 0 deletions
51
...etrics/library/src/main/java/io/opentelemetry/instrumentation/runtimemetrics/Threads.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,51 @@ | ||
/* | ||
* 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.ManagementFactory; | ||
import java.lang.management.ThreadMXBean; | ||
|
||
/** | ||
* Registers measurements that generate metrics about JVM threads. | ||
* | ||
* <p>Example usage: | ||
* | ||
* <pre>{@code | ||
* Classes.registerObservers(GlobalOpenTelemetry.get()); | ||
* }</pre> | ||
* | ||
* <p>Example metrics being exported: | ||
* | ||
* <pre> | ||
* process.runtime.jvm.threads.count 4 | ||
* </pre> | ||
*/ | ||
public final class Threads { | ||
|
||
// Visible for testing | ||
static final Threads INSTANCE = new Threads(); | ||
|
||
/** Register observers for java runtime class metrics. */ | ||
public static void registerObservers(OpenTelemetry openTelemetry) { | ||
INSTANCE.registerObservers(openTelemetry, ManagementFactory.getThreadMXBean()); | ||
} | ||
|
||
// Visible for testing | ||
void registerObservers(OpenTelemetry openTelemetry, ThreadMXBean threadBean) { | ||
Meter meter = openTelemetry.getMeter("io.opentelemetry.runtime-metrics"); | ||
|
||
meter | ||
.upDownCounterBuilder("process.runtime.jvm.threads.count") | ||
.setDescription("Number of executing threads") | ||
.setUnit("1") | ||
.buildWithCallback( | ||
observableMeasurement -> observableMeasurement.record(threadBean.getThreadCount())); | ||
} | ||
|
||
private Threads() {} | ||
} |
51 changes: 51 additions & 0 deletions
51
...cs/library/src/test/java/io/opentelemetry/instrumentation/runtimemetrics/ThreadsTest.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,51 @@ | ||
/* | ||
* 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.ThreadMXBean; | ||
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 ThreadsTest { | ||
|
||
@RegisterExtension | ||
static final InstrumentationExtension testing = LibraryInstrumentationExtension.create(); | ||
|
||
@Mock private ThreadMXBean threadBean; | ||
|
||
@Test | ||
void registerObservers() { | ||
when(threadBean.getThreadCount()).thenReturn(3); | ||
|
||
Threads.INSTANCE.registerObservers(testing.getOpenTelemetry(), threadBean); | ||
|
||
testing.waitAndAssertMetrics( | ||
"io.opentelemetry.runtime-metrics", | ||
"process.runtime.jvm.threads.count", | ||
metrics -> | ||
metrics.anySatisfy( | ||
metricData -> | ||
assertThat(metricData) | ||
.hasDescription("Number of executing threads") | ||
.hasUnit("1") | ||
.hasLongSumSatisfying( | ||
sum -> | ||
sum.isNotMonotonic() | ||
.hasPointsSatisfying( | ||
point -> | ||
point.hasValue(3).hasAttributes(Attributes.empty()))))); | ||
} | ||
} |