-
Notifications
You must be signed in to change notification settings - Fork 910
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Stable JVM semconv implementation: threads
- Loading branch information
Mateusz Rzeszutek
committed
Nov 10, 2023
1 parent
31ad4b1
commit b59395d
Showing
2 changed files
with
248 additions
and
15 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
144 changes: 144 additions & 0 deletions
144
.../java/io/opentelemetry/instrumentation/runtimemetrics/java8/ThreadsStableSemconvTest.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,144 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.instrumentation.runtimemetrics.java8; | ||
|
||
import static io.opentelemetry.instrumentation.runtimemetrics.java8.ScopeUtil.EXPECTED_SCOPE; | ||
import static io.opentelemetry.instrumentation.runtimemetrics.java8.Threads.JVM_THREAD_DAEMON; | ||
import static io.opentelemetry.instrumentation.runtimemetrics.java8.Threads.JVM_THREAD_STATE; | ||
import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.assertThat; | ||
import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.equalTo; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
|
||
import io.opentelemetry.instrumentation.testing.internal.AutoCleanupExtension; | ||
import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension; | ||
import io.opentelemetry.instrumentation.testing.junit.LibraryInstrumentationExtension; | ||
import java.lang.management.ThreadInfo; | ||
import java.lang.management.ThreadMXBean; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.condition.EnabledForJreRange; | ||
import org.junit.jupiter.api.condition.EnabledOnJre; | ||
import org.junit.jupiter.api.condition.JRE; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
import org.mockito.Mock; | ||
import org.mockito.invocation.InvocationOnMock; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
import org.mockito.stubbing.Answer; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
class ThreadsStableSemconvTest { | ||
|
||
@RegisterExtension | ||
static final InstrumentationExtension testing = LibraryInstrumentationExtension.create(); | ||
|
||
@RegisterExtension static final AutoCleanupExtension cleanup = AutoCleanupExtension.create(); | ||
|
||
@Mock private ThreadMXBean threadBean; | ||
|
||
@Test | ||
@EnabledOnJre(JRE.JAVA_8) | ||
void registerObservers_Java8() { | ||
when(threadBean.getThreadCount()).thenReturn(7); | ||
when(threadBean.getDaemonThreadCount()).thenReturn(2); | ||
|
||
Threads.INSTANCE | ||
.registerObservers(testing.getOpenTelemetry(), threadBean) | ||
.forEach(cleanup::deferCleanup); | ||
|
||
testing.waitAndAssertMetrics( | ||
"io.opentelemetry.runtime-telemetry-java8", | ||
"jvm.thread.count", | ||
metrics -> | ||
metrics.anySatisfy( | ||
metricData -> | ||
assertThat(metricData) | ||
.hasInstrumentationScope(EXPECTED_SCOPE) | ||
.hasDescription("Number of executing platform threads.") | ||
.hasUnit("{thread}") | ||
.hasLongSumSatisfying( | ||
sum -> | ||
sum.isNotMonotonic() | ||
.hasPointsSatisfying( | ||
point -> | ||
point | ||
.hasValue(2) | ||
.hasAttributesSatisfying( | ||
equalTo(JVM_THREAD_DAEMON, true)), | ||
point -> | ||
point | ||
.hasValue(5) | ||
.hasAttributesSatisfying( | ||
equalTo(JVM_THREAD_DAEMON, false)))))); | ||
} | ||
|
||
@Test | ||
@EnabledForJreRange(min = JRE.JAVA_9) | ||
void registerObservers_Java9AndNewer() { | ||
ThreadInfo threadInfo1 = | ||
mock(ThreadInfo.class, new ThreadInfoAnswer(false, Thread.State.RUNNABLE)); | ||
ThreadInfo threadInfo2 = | ||
mock(ThreadInfo.class, new ThreadInfoAnswer(true, Thread.State.WAITING)); | ||
|
||
long[] threadIds = {12, 32, 42}; | ||
when(threadBean.getAllThreadIds()).thenReturn(threadIds); | ||
when(threadBean.getThreadInfo(threadIds)) | ||
.thenReturn(new ThreadInfo[] {threadInfo1, null, threadInfo2}); | ||
|
||
Threads.INSTANCE | ||
.registerObservers(testing.getOpenTelemetry(), threadBean) | ||
.forEach(cleanup::deferCleanup); | ||
|
||
testing.waitAndAssertMetrics( | ||
"io.opentelemetry.runtime-telemetry-java8", | ||
"jvm.thread.count", | ||
metrics -> | ||
metrics.anySatisfy( | ||
metricData -> | ||
assertThat(metricData) | ||
.hasInstrumentationScope(EXPECTED_SCOPE) | ||
.hasDescription("Number of executing platform threads.") | ||
.hasUnit("{thread}") | ||
.hasLongSumSatisfying( | ||
sum -> | ||
sum.isNotMonotonic() | ||
.hasPointsSatisfying( | ||
point -> | ||
point | ||
.hasValue(1) | ||
.hasAttributesSatisfying( | ||
equalTo(JVM_THREAD_DAEMON, false), | ||
equalTo(JVM_THREAD_STATE, "runnable")), | ||
point -> | ||
point | ||
.hasValue(1) | ||
.hasAttributesSatisfying( | ||
equalTo(JVM_THREAD_DAEMON, true), | ||
equalTo(JVM_THREAD_STATE, "waiting")))))); | ||
} | ||
|
||
static final class ThreadInfoAnswer implements Answer<Object> { | ||
|
||
private final boolean isDaemon; | ||
private final Thread.State state; | ||
|
||
ThreadInfoAnswer(boolean isDaemon, Thread.State state) { | ||
this.isDaemon = isDaemon; | ||
this.state = state; | ||
} | ||
|
||
@Override | ||
public Object answer(InvocationOnMock invocation) { | ||
String methodName = invocation.getMethod().getName(); | ||
if (methodName.equals("isDaemon")) { | ||
return isDaemon; | ||
} else if (methodName.equals("getThreadState")) { | ||
return state; | ||
} | ||
return null; | ||
} | ||
} | ||
} |