com.codahale.metrics.JvmAttributeGaugeSet Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of metrics-core Show documentation
Show all versions of metrics-core Show documentation
Metrics is a Java library which gives you unparalleled insight into what your code does in
production. Metrics provides a powerful toolkit of ways to measure the behavior of critical
components in your production environment.
package com.codahale.metrics;
import java.lang.management.ManagementFactory;
import java.lang.management.RuntimeMXBean;
import java.util.Collections;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
/**
* A set of gauges for the JVM name, vendor, and uptime.
*/
public class JvmAttributeGaugeSet implements MetricSet {
private final RuntimeMXBean runtime;
/**
* Creates a new set of gauges.
*/
public JvmAttributeGaugeSet() {
this(ManagementFactory.getRuntimeMXBean());
}
/**
* Creates a new set of gauges with the given {@link RuntimeMXBean}.
*/
public JvmAttributeGaugeSet(RuntimeMXBean runtime) {
this.runtime = runtime;
}
@Override
public Map getMetrics() {
final Map gauges = new HashMap();
gauges.put("name", new Gauge() {
@Override
public String getValue() {
return runtime.getName();
}
});
gauges.put("vendor", new Gauge() {
@Override
public String getValue() {
return String.format(Locale.US,
"%s %s %s (%s)",
runtime.getVmVendor(),
runtime.getVmName(),
runtime.getVmVersion(),
runtime.getSpecVersion());
}
});
gauges.put("uptime", new Gauge() {
@Override
public Long getValue() {
return runtime.getUptime();
}
});
return Collections.unmodifiableMap(gauges);
}
}