com.codahale.metrics.jvm.GarbageCollectorMetricSet Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of metrics-jvm Show documentation
Show all versions of metrics-jvm Show documentation
A set of classes which allow you to monitor critical aspects of your Java Virtual Machine
using Metrics.
The newest version!
package com.codahale.metrics.jvm;
import com.codahale.metrics.Gauge;
import com.codahale.metrics.Metric;
import com.codahale.metrics.MetricSet;
import java.lang.management.GarbageCollectorMXBean;
import java.lang.management.ManagementFactory;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;
import static com.codahale.metrics.MetricRegistry.name;
/**
* A set of gauges for the counts and elapsed times of garbage collections.
*/
public class GarbageCollectorMetricSet implements MetricSet {
private static final Pattern WHITESPACE = Pattern.compile("[\\s]+");
private final List garbageCollectors;
/**
* Creates a new set of gauges for all discoverable garbage collectors.
*/
public GarbageCollectorMetricSet() {
this(ManagementFactory.getGarbageCollectorMXBeans());
}
/**
* Creates a new set of gauges for the given collection of garbage collectors.
*
* @param garbageCollectors the garbage collectors
*/
public GarbageCollectorMetricSet(Collection garbageCollectors) {
this.garbageCollectors = new ArrayList<>(garbageCollectors);
}
@Override
public Map getMetrics() {
final Map gauges = new HashMap<>();
for (final GarbageCollectorMXBean gc : garbageCollectors) {
final String name = WHITESPACE.matcher(gc.getName()).replaceAll("-");
gauges.put(name(name, "count"), (Gauge) gc::getCollectionCount);
gauges.put(name(name, "time"), (Gauge) gc::getCollectionTime);
}
return Collections.unmodifiableMap(gauges);
}
}