Skip to content

Commit 23cd1b8

Browse files
committed
Avoid accidentally dropping JMX-only metrics. Hoist meters out of consume lambdas
1 parent 623e89c commit 23cd1b8

File tree

2 files changed

+29
-19
lines changed

2 files changed

+29
-19
lines changed

spectator-ext-jvm/src/main/java/com/netflix/spectator/jvm/Jmx.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,11 @@ public static void registerStandardMXBeans(Registry registry) {
5252
});
5353
JavaFlightRecorder.monitorDefaultEvents(registry, executor);
5454
return;
55+
} else {
56+
monitorClassLoadingMXBean(registry);
57+
monitorThreadMXBean(registry);
58+
monitorCompilationMXBean(registry);
5559
}
56-
monitorClassLoadingMXBean(registry);
57-
monitorThreadMXBean(registry);
58-
monitorCompilationMXBean(registry);
5960
maybeRegisterHotspotInternal(registry);
6061

6162
for (MemoryPoolMXBean mbean : ManagementFactory.getMemoryPoolMXBeans()) {

spectator-ext-jvm/src/main/java17/com/netflix/spectator/jvm/JavaFlightRecorder.java

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@
1515
*/
1616
package com.netflix.spectator.jvm;
1717

18+
import com.netflix.spectator.api.Counter;
19+
import com.netflix.spectator.api.Gauge;
1820
import com.netflix.spectator.api.Registry;
21+
import com.netflix.spectator.api.Timer;
1922
import jdk.jfr.EventSettings;
2023
import jdk.jfr.consumer.RecordedEvent;
2124
import jdk.jfr.consumer.RecordingStream;
@@ -67,29 +70,33 @@ public static AutoCloseable monitorDefaultEvents(Registry registry, Executor exe
6770
}
6871

6972
private static void collectClassLoadingStatistics(Registry registry, RecordingStream rs) {
73+
Counter classesLoaded = registry.counter("jvm.classloading.classesLoaded");
7074
AtomicLong prevLoadedClassCount = new AtomicLong();
75+
Counter classesUnloaded = registry.counter("jvm.classloading.classesUnloaded");
7176
AtomicLong prevUnloadedClassCount = new AtomicLong();
7277
consume(ClassLoadingStatistics, rs, event -> {
73-
long classesLoaded = event.getLong("loadedClassCount");
74-
classesLoaded = classesLoaded - prevLoadedClassCount.getAndSet(classesLoaded);
75-
registry.counter("jvm.classloading.classesLoaded").increment(classesLoaded);
78+
long loadedClassCount = event.getLong("loadedClassCount");
79+
loadedClassCount = loadedClassCount - prevLoadedClassCount.getAndSet(loadedClassCount);
80+
classesLoaded.increment(loadedClassCount);
7681

77-
long classesUnloaded = event.getLong("unloadedClassCount");
78-
classesUnloaded = classesUnloaded - prevUnloadedClassCount.getAndSet(classesUnloaded);
79-
registry.counter("jvm.classloading.classesUnloaded").increment(classesUnloaded);
82+
long unloadedClassCount = event.getLong("unloadedClassCount");
83+
unloadedClassCount = unloadedClassCount - prevUnloadedClassCount.getAndSet(unloadedClassCount);
84+
classesUnloaded.increment(unloadedClassCount);
8085
});
8186
}
8287

8388
private static void collectCompilerStatistics(Registry registry, RecordingStream rs) {
89+
Counter compilationTime = registry.counter("jvm.compilation.compilationTime");
8490
AtomicLong prevTotalTimeSpent = new AtomicLong();
8591
consume(CompilerStatistics, rs, event -> {
8692
long totalTimeSpent = event.getLong("totalTimeSpent");
8793
totalTimeSpent = totalTimeSpent - prevTotalTimeSpent.getAndAdd(totalTimeSpent);
88-
registry.counter("jvm.compilation.compilationTime").add(totalTimeSpent / 1000.0);
94+
compilationTime.add(totalTimeSpent / 1000.0);
8995
});
9096
}
9197

9298
private static void collectThreadStatistics(Registry registry, RecordingStream rs) {
99+
Counter threadsStarted = registry.counter("jvm.thread.threadsStarted");
93100
AtomicLong prevAccumulatedCount = new AtomicLong();
94101
consume(JavaThreadStatistics, rs, event -> {
95102
long activeCount = event.getLong("activeCount");
@@ -98,30 +105,32 @@ private static void collectThreadStatistics(Registry registry, RecordingStream r
98105
registry.gauge("jvm.thread.threadCount", "id", "non-daemon").set(nonDaemonCount);
99106
registry.gauge("jvm.thread.threadCount", "id", "daemon").set(daemonCount);
100107
long accumulatedCount = event.getLong("accumulatedCount");
101-
long threadsStarted = accumulatedCount - prevAccumulatedCount.getAndSet(accumulatedCount);
102-
registry.counter("jvm.thread.threadsStarted").increment(threadsStarted);
108+
accumulatedCount = accumulatedCount - prevAccumulatedCount.getAndSet(accumulatedCount);
109+
threadsStarted.increment(accumulatedCount);
103110
});
104111
}
105112

106113
private static void collectVirtualThreadEvents(Registry registry, RecordingStream rs) {
114+
Timer pinned = registry.timer("jvm.vt.pinned");
115+
Counter submitFailed = registry.counter("jvm.vt.submitFailed");
107116
// 20ms threshold set to match default behavior
108117
consume(VirtualThreadPinned, rs, event ->
109-
registry.timer("jvm.vt.pinned").record(event.getDuration())
118+
pinned.record(event.getDuration())
110119
).withThreshold(Duration.ofMillis(20));
111120
consume(VirtualThreadSubmitFailed, rs, event ->
112-
registry.counter("jvm.vt.submitFailed").increment()
121+
submitFailed.increment()
113122
);
114123
}
115124

116125
private static void collectGcEvents(Registry registry, RecordingStream rs) {
117126
// ZGC and Shenandoah are not covered by the generic event, there is
118127
// a ZGC specific event to get coverage there, right now there doesn't
119128
// appear to be similar data available for Shenandoah
120-
Consumer<RecordedEvent> tenuringThreshold = event ->
121-
registry.gauge("jvm.gc.tenuringThreshold")
122-
.set(event.getLong("tenuringThreshold"));
123-
consume(YoungGarbageCollection, rs, tenuringThreshold);
124-
consume(ZYoungGarbageCollection, rs, tenuringThreshold);
129+
Gauge tenuringThreshold = registry.gauge("jvm.gc.tenuringThreshold");
130+
Consumer<RecordedEvent> tenuringThresholdFn = event ->
131+
tenuringThreshold.set(event.getLong("tenuringThreshold"));
132+
consume(YoungGarbageCollection, rs, tenuringThresholdFn);
133+
consume(ZYoungGarbageCollection, rs, tenuringThresholdFn);
125134

126135
consume(ZAllocationStall, rs, event ->
127136
registry.timer("jvm.gc.allocationStall", "type", event.getString("type"))

0 commit comments

Comments
 (0)