-
Notifications
You must be signed in to change notification settings - Fork 130
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
Thread count and classes loaded handlers #571
Merged
trask
merged 9 commits into
open-telemetry:main
from
roberttoyonaga:thread-count-and-classes-loaded-handlers
Nov 11, 2022
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
b51a053
add thread count and tests
roberttoyonaga d244f91
add handler to registry
roberttoyonaga fc24a17
add classes loaded count metric
roberttoyonaga 931a130
Conform to spec. Update tests.
roberttoyonaga 4a7602e
minor touch up
roberttoyonaga b02a36d
Update jfr-streaming/src/main/java/io/opentelemetry/contrib/jfr/metri…
roberttoyonaga 153a743
comment. boolean attribute. simplify test case.
roberttoyonaga 182a2e1
Merge branch 'thread-count-and-classes-loaded-handlers' of github.com…
roberttoyonaga 435c02d
Update jfr-streaming/src/test/java/io/opentelemetry/contrib/jfr/metri…
roberttoyonaga File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
78 changes: 78 additions & 0 deletions
78
...main/java/io/opentelemetry/contrib/jfr/metrics/internal/classes/ClassesLoadedHandler.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,78 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.contrib.jfr.metrics.internal.classes; | ||
|
||
import static io.opentelemetry.contrib.jfr.metrics.internal.Constants.UNIT_CLASSES; | ||
import static io.opentelemetry.contrib.jfr.metrics.internal.RecordedEventHandler.defaultMeter; | ||
|
||
import io.opentelemetry.api.metrics.Meter; | ||
import io.opentelemetry.contrib.jfr.metrics.internal.RecordedEventHandler; | ||
import java.time.Duration; | ||
import java.util.Optional; | ||
import jdk.jfr.consumer.RecordedEvent; | ||
|
||
public final class ClassesLoadedHandler implements RecordedEventHandler { | ||
/** | ||
* process.runtime.jvm.classes.loaded is the total number of classes loaded since JVM start. See: | ||
* https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/metrics/semantic_conventions/runtime-environment-metrics.md#jvm-metrics | ||
*/ | ||
private static final String METRIC_NAME_LOADED = "process.runtime.jvm.classes.loaded"; | ||
|
||
private static final String METRIC_NAME_UNLOADED = "process.runtime.jvm.classes.unloaded"; | ||
/** | ||
* process.runtime.jvm.classes.current_loaded is the number of classes loaded at the time of | ||
* jdk.ClassLoadingStatistics event emission. | ||
*/ | ||
private static final String METRIC_NAME_CURRENT = "process.runtime.jvm.classes.current_loaded"; | ||
|
||
private static final String EVENT_NAME = "jdk.ClassLoadingStatistics"; | ||
private static final String METRIC_DESCRIPTION_CURRENT = "Number of classes currently loaded"; | ||
private static final String METRIC_DESCRIPTION_LOADED = | ||
"Number of classes loaded since JVM start"; | ||
private static final String METRIC_DESCRIPTION_UNLOADED = | ||
"Number of classes unloaded since JVM start"; | ||
private volatile long loaded = 0; | ||
private volatile long unloaded = 0; | ||
|
||
public ClassesLoadedHandler() { | ||
initializeMeter(defaultMeter()); | ||
} | ||
|
||
@Override | ||
public void accept(RecordedEvent ev) { | ||
loaded = ev.getLong("loadedClassCount"); | ||
unloaded = ev.getLong("unloadedClassCount"); | ||
} | ||
|
||
@Override | ||
public String getEventName() { | ||
return EVENT_NAME; | ||
} | ||
|
||
@Override | ||
public void initializeMeter(Meter meter) { | ||
meter | ||
.upDownCounterBuilder(METRIC_NAME_CURRENT) | ||
.setDescription(METRIC_DESCRIPTION_CURRENT) | ||
.setUnit(UNIT_CLASSES) | ||
.buildWithCallback(measurement -> measurement.record(loaded - unloaded)); | ||
meter | ||
.counterBuilder(METRIC_NAME_LOADED) | ||
.setDescription(METRIC_DESCRIPTION_LOADED) | ||
.setUnit(UNIT_CLASSES) | ||
.buildWithCallback(measurement -> measurement.record(loaded)); | ||
meter | ||
.counterBuilder(METRIC_NAME_UNLOADED) | ||
.setDescription(METRIC_DESCRIPTION_UNLOADED) | ||
.setUnit(UNIT_CLASSES) | ||
.buildWithCallback(measurement -> measurement.record(unloaded)); | ||
} | ||
|
||
@Override | ||
public Optional<Duration> getPollingDuration() { | ||
return Optional.of(Duration.ofSeconds(1)); | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
...c/main/java/io/opentelemetry/contrib/jfr/metrics/internal/threads/ThreadCountHandler.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,61 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.contrib.jfr.metrics.internal.threads; | ||
|
||
import static io.opentelemetry.contrib.jfr.metrics.internal.Constants.ATTR_DAEMON; | ||
import static io.opentelemetry.contrib.jfr.metrics.internal.Constants.UNIT_THREADS; | ||
import static io.opentelemetry.contrib.jfr.metrics.internal.RecordedEventHandler.defaultMeter; | ||
|
||
import io.opentelemetry.api.common.Attributes; | ||
import io.opentelemetry.api.metrics.Meter; | ||
import io.opentelemetry.contrib.jfr.metrics.internal.RecordedEventHandler; | ||
import java.time.Duration; | ||
import java.util.Optional; | ||
import jdk.jfr.consumer.RecordedEvent; | ||
|
||
public final class ThreadCountHandler implements RecordedEventHandler { | ||
private static final String METRIC_NAME = "process.runtime.jvm.threads.count"; | ||
private static final String EVENT_NAME = "jdk.JavaThreadStatistics"; | ||
private static final String METRIC_DESCRIPTION = "Number of executing threads"; | ||
private static final Attributes ATTR_DAEMON_TRUE = Attributes.of(ATTR_DAEMON, true); | ||
private static final Attributes ATTR_DAEMON_FALSE = Attributes.of(ATTR_DAEMON, false); | ||
private volatile long activeCount = 0; | ||
private volatile long daemonCount = 0; | ||
|
||
public ThreadCountHandler() { | ||
initializeMeter(defaultMeter()); | ||
} | ||
|
||
@Override | ||
public void accept(RecordedEvent ev) { | ||
activeCount = ev.getLong("activeCount"); | ||
daemonCount = ev.getLong("daemonCount"); | ||
} | ||
|
||
@Override | ||
public String getEventName() { | ||
return EVENT_NAME; | ||
} | ||
|
||
@Override | ||
public void initializeMeter(Meter meter) { | ||
meter | ||
.upDownCounterBuilder(METRIC_NAME) | ||
.setDescription(METRIC_DESCRIPTION) | ||
.setUnit(UNIT_THREADS) | ||
.buildWithCallback( | ||
measurement -> { | ||
long d = daemonCount; | ||
measurement.record(d, ATTR_DAEMON_TRUE); | ||
measurement.record(activeCount - d, ATTR_DAEMON_FALSE); | ||
}); | ||
} | ||
|
||
@Override | ||
public Optional<Duration> getPollingDuration() { | ||
return Optional.of(Duration.ofSeconds(1)); | ||
} | ||
} |
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
56 changes: 56 additions & 0 deletions
56
...reaming/src/test/java/io/opentelemetry/contrib/jfr/metrics/JfrClassesLoadedCountTest.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,56 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.contrib.jfr.metrics; | ||
|
||
import static io.opentelemetry.contrib.jfr.metrics.internal.Constants.UNIT_CLASSES; | ||
|
||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
|
||
class JfrClassesLoadedCountTest extends AbstractMetricsTest { | ||
|
||
@Test | ||
void shouldHaveJfrLoadedClassesCountEvents() throws Exception { | ||
Thread.sleep(2000); | ||
|
||
waitAndAssertMetrics( | ||
metric -> | ||
metric | ||
.hasName("process.runtime.jvm.classes.loaded") | ||
.hasDescription("Number of classes loaded since JVM start") | ||
.hasUnit(UNIT_CLASSES) | ||
.hasLongSumSatisfying( | ||
sum -> | ||
sum.hasPointsSatisfying( | ||
point -> | ||
point.satisfies( | ||
pointData -> Assertions.assertTrue(pointData.getValue() > 0)))), | ||
metric -> | ||
metric | ||
.hasName("process.runtime.jvm.classes.current_loaded") | ||
.hasDescription("Number of classes currently loaded") | ||
.hasUnit(UNIT_CLASSES) | ||
.hasLongSumSatisfying( | ||
sum -> | ||
sum.hasPointsSatisfying( | ||
point -> | ||
point.satisfies( | ||
pointData -> | ||
Assertions.assertTrue(pointData.getValue() >= 0)))), | ||
metric -> | ||
metric | ||
.hasName("process.runtime.jvm.classes.unloaded") | ||
.hasDescription("Number of classes unloaded since JVM start") | ||
.hasUnit(UNIT_CLASSES) | ||
.hasLongSumSatisfying( | ||
sum -> | ||
sum.hasPointsSatisfying( | ||
point -> | ||
point.satisfies( | ||
pointData -> | ||
Assertions.assertTrue(pointData.getValue() >= 0))))); | ||
} | ||
} |
67 changes: 67 additions & 0 deletions
67
jfr-streaming/src/test/java/io/opentelemetry/contrib/jfr/metrics/JfrThreadCountTest.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,67 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.contrib.jfr.metrics; | ||
|
||
import static io.opentelemetry.contrib.jfr.metrics.internal.Constants.DAEMON; | ||
import static io.opentelemetry.contrib.jfr.metrics.internal.Constants.UNIT_THREADS; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import io.opentelemetry.api.common.AttributeKey; | ||
import io.opentelemetry.sdk.metrics.data.LongPointData; | ||
import io.opentelemetry.sdk.metrics.data.SumData; | ||
import org.junit.jupiter.api.Test; | ||
|
||
class JfrThreadCountTest extends AbstractMetricsTest { | ||
private static final int SAMPLING_INTERVAL = 1000; | ||
|
||
private static void doWork() throws InterruptedException { | ||
Thread.sleep(2 * SAMPLING_INTERVAL); | ||
} | ||
|
||
private static boolean isDaemon(LongPointData p) { | ||
Boolean daemon = p.getAttributes().get(AttributeKey.booleanKey(DAEMON)); | ||
assertThat(daemon).isNotNull(); | ||
return daemon; | ||
} | ||
|
||
@Test | ||
void shouldHaveJfrThreadCountEvents() throws Exception { | ||
// This should generate some events | ||
Runnable work = | ||
() -> { | ||
// create contention between threads for one lock | ||
try { | ||
doWork(); | ||
} catch (InterruptedException e) { | ||
throw new RuntimeException(e); | ||
} | ||
}; | ||
Thread userThread = new Thread(work); | ||
userThread.setDaemon(false); | ||
userThread.start(); | ||
|
||
Thread daemonThread = new Thread(work); | ||
daemonThread.setDaemon(true); | ||
daemonThread.start(); | ||
|
||
userThread.join(); | ||
daemonThread.join(); | ||
|
||
waitAndAssertMetrics( | ||
metric -> | ||
metric | ||
.hasName("process.runtime.jvm.threads.count") | ||
.hasUnit(UNIT_THREADS) | ||
.satisfies( | ||
metricData -> { | ||
SumData<?> sumData = metricData.getLongSumData(); | ||
assertThat(sumData.getPoints()) | ||
.map(LongPointData.class::cast) | ||
.anyMatch(p -> p.getValue() > 0 && isDaemon(p)) | ||
.anyMatch(p -> p.getValue() > 0 && !isDaemon(p)); | ||
})); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Do we document somewhere what the difference is between classes.loaded and classes.current_loaded? Maybe link to that definition here?
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.
It's in the JVM metrics spec: https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/metrics/semantic_conventions/runtime-environment-metrics.md#jvm-metrics
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.
I added some comments for further clarity. The metric descriptions also add some information.