-
Notifications
You must be signed in to change notification settings - Fork 860
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
GC JVM runtime metrics proposal #6362
Closed
Closed
Changes from all commits
Commits
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
89 changes: 89 additions & 0 deletions
89
...ime-metrics/library/src/main/java/io/opentelemetry/instrumentation/runtimemetrics/Gc.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,89 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.instrumentation.runtimemetrics; | ||
|
||
import com.sun.management.GarbageCollectionNotificationInfo; | ||
import io.opentelemetry.api.OpenTelemetry; | ||
import io.opentelemetry.api.common.AttributeKey; | ||
import io.opentelemetry.api.common.Attributes; | ||
import io.opentelemetry.api.metrics.LongHistogram; | ||
import io.opentelemetry.api.metrics.Meter; | ||
import java.lang.management.GarbageCollectorMXBean; | ||
import java.lang.management.ManagementFactory; | ||
import java.util.List; | ||
import javax.management.Notification; | ||
import javax.management.NotificationEmitter; | ||
import javax.management.NotificationFilter; | ||
import javax.management.NotificationListener; | ||
import javax.management.openmbean.CompositeData; | ||
|
||
public final class Gc { | ||
|
||
// Visible for testing | ||
static final Gc INSTANCE = new Gc(); | ||
|
||
private static final AttributeKey<String> GC_KEY = AttributeKey.stringKey("gc"); | ||
private static final AttributeKey<String> CAUSE_KEY = AttributeKey.stringKey("cause"); | ||
private static final AttributeKey<String> ACTION_KEY = AttributeKey.stringKey("action"); | ||
|
||
private static final NotificationFilter GC_FILTER = | ||
notification -> | ||
notification | ||
.getType() | ||
.equals(GarbageCollectionNotificationInfo.GARBAGE_COLLECTION_NOTIFICATION); | ||
|
||
public static void registerObservers(OpenTelemetry openTelemetry) { | ||
INSTANCE.registerObservers(openTelemetry, ManagementFactory.getGarbageCollectorMXBeans()); | ||
} | ||
|
||
// Visible for testing | ||
void registerObservers(OpenTelemetry openTelemetry, List<GarbageCollectorMXBean> gcBeans) { | ||
Meter meter = openTelemetry.getMeter("io.opentelemetry.runtime-metrics"); | ||
|
||
LongHistogram gcTime = | ||
meter | ||
.histogramBuilder("process.runtime.jvm.gc.time") | ||
.setDescription("Time spent performing JVM garbage collection actions") | ||
.setUnit("ms") | ||
.ofLongs() | ||
.build(); | ||
|
||
for (GarbageCollectorMXBean gcBean : ManagementFactory.getGarbageCollectorMXBeans()) { | ||
if (!(gcBean instanceof NotificationEmitter)) { | ||
continue; | ||
} | ||
NotificationEmitter notificationEmitter = (NotificationEmitter) gcBean; | ||
notificationEmitter.addNotificationListener( | ||
new GcNotificationListener(gcTime), GC_FILTER, null); | ||
} | ||
} | ||
|
||
private static final class GcNotificationListener implements NotificationListener { | ||
|
||
private final LongHistogram gcTime; | ||
|
||
private GcNotificationListener(LongHistogram gcTime) { | ||
this.gcTime = gcTime; | ||
} | ||
|
||
@Override | ||
public void handleNotification(Notification notification, Object handback) { | ||
GarbageCollectionNotificationInfo notificationInfo = | ||
GarbageCollectionNotificationInfo.from((CompositeData) notification.getUserData()); | ||
gcTime.record( | ||
notificationInfo.getGcInfo().getDuration(), | ||
Attributes.of( | ||
GC_KEY, | ||
notificationInfo.getGcName(), | ||
CAUSE_KEY, | ||
notificationInfo.getGcCause(), | ||
ACTION_KEY, | ||
notificationInfo.getGcAction())); | ||
} | ||
} | ||
|
||
private Gc() {} | ||
} |
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.
probably trim string to have first N chars only ?