[metrics module] 一系列改进,包括一些重命名,JMXExporter更完善,锁的改进,排序的改进等
This commit is contained in:
@@ -18,7 +18,7 @@ import org.springside.modules.metrics.utils.Clock;
|
||||
public class Counter {
|
||||
public static Clock clock = Clock.DEFAULT;
|
||||
|
||||
public CounterMetric snapshot;
|
||||
public CounterMetric latestMetric;
|
||||
|
||||
private AtomicLong counter;
|
||||
|
||||
@@ -47,32 +47,32 @@ public class Counter {
|
||||
}
|
||||
|
||||
public CounterMetric calculateMetric() {
|
||||
long lastCount = counter.getAndSet(0);
|
||||
long latestCount = counter.getAndSet(0);
|
||||
long currentTime = clock.getCurrentTime();
|
||||
|
||||
CounterMetric metric = new CounterMetric();
|
||||
|
||||
totalCount += lastCount;
|
||||
totalCount += latestCount;
|
||||
long totalElapsed = currentTime - startTime;
|
||||
metric.meanRate = (totalCount * 1000) / totalElapsed;
|
||||
|
||||
metric.lastCount = lastCount;
|
||||
metric.latestCount = latestCount;
|
||||
metric.totalCount = totalCount;
|
||||
|
||||
long elapsed = currentTime - lastReportTime;
|
||||
if (elapsed > 0) {
|
||||
metric.lastRate = (lastCount * 1000) / elapsed;
|
||||
metric.latestRate = (latestCount * 1000) / elapsed;
|
||||
}
|
||||
|
||||
lastReportTime = currentTime;
|
||||
|
||||
snapshot = metric;
|
||||
latestMetric = metric;
|
||||
|
||||
return metric;
|
||||
}
|
||||
|
||||
public void reset() {
|
||||
snapshot = new CounterMetric();
|
||||
latestMetric = new CounterMetric();
|
||||
counter = new AtomicLong(0);
|
||||
totalCount = 0L;
|
||||
startTime = clock.getCurrentTime();
|
||||
|
||||
@@ -8,12 +8,12 @@ package org.springside.modules.metrics;
|
||||
public class CounterMetric {
|
||||
public long totalCount;
|
||||
public long meanRate;
|
||||
public long lastCount;
|
||||
public long lastRate;
|
||||
public long latestCount;
|
||||
public long latestRate;
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "CounterMetric [totalCount=" + totalCount + ", meanRate=" + meanRate + ", lastCount=" + lastCount
|
||||
+ ", lastRate=" + lastRate + "]";
|
||||
return "CounterMetric [totalCount=" + totalCount + ", meanRate=" + meanRate + ", latestCount=" + latestCount
|
||||
+ ", latestRate=" + latestRate + "]";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,36 +12,30 @@ import java.util.List;
|
||||
|
||||
/**
|
||||
* Histogram数据类型, 主要用于计算Latency.
|
||||
* 报告Report间隔时间内Latency最小/最大,平均值,以及某些百分比的请求Latency小于的值。
|
||||
* 报告Report间隔时间内s数值的最小/最大,平均值,以及某些百分比的请求数小于的值。
|
||||
*
|
||||
* @author Calvin
|
||||
*/
|
||||
public class Histogram {
|
||||
|
||||
public HistogramMetric snapshot = new HistogramMetric();
|
||||
public HistogramMetric latestMetric = new HistogramMetric();
|
||||
|
||||
private List<Long> measurements = new LinkedList<Long>();
|
||||
private Double[] pcts;
|
||||
private Object lock = new Object();
|
||||
|
||||
public Histogram(Double... pcts) {
|
||||
this.pcts = pcts;
|
||||
}
|
||||
|
||||
public void update(long value) {
|
||||
synchronized (lock) {
|
||||
measurements.add(value);
|
||||
}
|
||||
measurements.add(value);
|
||||
}
|
||||
|
||||
public HistogramMetric calculateMetric() {
|
||||
// 快照当前的数据,在计算时不阻塞新的metrics update.
|
||||
List<Long> snapshotList = null;
|
||||
List<Long> snapshotList = measurements;
|
||||
|
||||
synchronized (lock) {
|
||||
snapshotList = measurements;
|
||||
measurements = new LinkedList();
|
||||
}
|
||||
measurements = new LinkedList();
|
||||
|
||||
if (snapshotList.isEmpty()) {
|
||||
return createEmptyMetric();
|
||||
@@ -66,7 +60,7 @@ public class Histogram {
|
||||
metric.pcts.put(pct, getPercent(snapshotList, count, pct));
|
||||
}
|
||||
|
||||
snapshot = metric;
|
||||
latestMetric = metric;
|
||||
return metric;
|
||||
}
|
||||
|
||||
|
||||
@@ -8,8 +8,6 @@ package org.springside.modules.metrics;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.SortedMap;
|
||||
import java.util.TreeMap;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.ConcurrentMap;
|
||||
|
||||
@@ -67,13 +65,6 @@ public class MetricRegistry {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 在注册中心获取或创建Histogram, 使用默认的百分比计算设置(90%).
|
||||
*/
|
||||
public Histogram histogram(String name) {
|
||||
return histogram(name, defaultPcts);
|
||||
}
|
||||
|
||||
/**
|
||||
* 在注册中心获取或创建Histogram, 并设置所需的百分比计算.
|
||||
*/
|
||||
@@ -81,18 +72,11 @@ public class MetricRegistry {
|
||||
if (histograms.containsKey(name)) {
|
||||
return histograms.get(name);
|
||||
} else {
|
||||
Histogram histogram = new Histogram(pcts);
|
||||
Histogram histogram = new Histogram(((pcts != null) && (pcts.length > 0)) ? pcts : defaultPcts);
|
||||
return register(histograms, name, histogram);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 在注册中心获取或创建Timer, 使用默认的百分比计算设置().
|
||||
*/
|
||||
public Timer timer(String name) {
|
||||
return timer(name, defaultPcts);
|
||||
}
|
||||
|
||||
/**
|
||||
* 在注册中心获取或创建Timer, 并设置所需的百分比计算.
|
||||
*/
|
||||
@@ -100,11 +84,14 @@ public class MetricRegistry {
|
||||
if (timers.containsKey(name)) {
|
||||
return timers.get(name);
|
||||
} else {
|
||||
Timer timer = new Timer(pcts);
|
||||
Timer timer = new Timer(((pcts != null) && (pcts.length > 0) ? pcts : defaultPcts));
|
||||
return register(timers, name, timer);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 快速清理注册表中全部Metrics.
|
||||
*/
|
||||
public void clearAll() {
|
||||
for (MetricRegistryListener listener : listeners) {
|
||||
for (String key : gauges.keySet()) {
|
||||
@@ -160,39 +147,30 @@ public class MetricRegistry {
|
||||
/**
|
||||
* 返回所有Gauge, 按名称排序.
|
||||
*/
|
||||
public SortedMap<String, Gauge> getGauges() {
|
||||
return getSortedMetrics(gauges);
|
||||
public Map<String, Gauge> getGauges() {
|
||||
return gauges;
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回所有Counter, 按名称排序.
|
||||
*/
|
||||
public SortedMap<String, Counter> getCounters() {
|
||||
return getSortedMetrics(counters);
|
||||
public Map<String, Counter> getCounters() {
|
||||
return counters;
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回所有Histogram, 按名称排序.
|
||||
*/
|
||||
|
||||
public SortedMap<String, Histogram> getHistograms() {
|
||||
return getSortedMetrics(histograms);
|
||||
public Map<String, Histogram> getHistograms() {
|
||||
return histograms;
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回所有Timer, 按名称排序.
|
||||
*/
|
||||
public SortedMap<String, Timer> getTimers() {
|
||||
return getSortedMetrics(timers);
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回按metrics name排序的Map.
|
||||
*
|
||||
* 从get的性能考虑,没有使用ConcurrentSkipListMap而是仍然使用ConcurrentHashMap,因此每次报告时需要用TreeMap重新排序.
|
||||
*/
|
||||
private <T> SortedMap<String, T> getSortedMetrics(Map<String, T> metrics) {
|
||||
return new TreeMap<String, T>(metrics);
|
||||
public Map<String, Timer> getTimers() {
|
||||
return timers;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -34,7 +34,7 @@ public class Timer {
|
||||
|
||||
public static Clock clock = Clock.DEFAULT;
|
||||
|
||||
public TimerMetric snapshot = new TimerMetric();
|
||||
public TimerMetric latestMetric = new TimerMetric();
|
||||
|
||||
private Counter counter;
|
||||
private Histogram histogram;
|
||||
@@ -63,7 +63,7 @@ public class Timer {
|
||||
TimerMetric metric = new TimerMetric();
|
||||
metric.counterMetric = counter.calculateMetric();
|
||||
metric.histogramMetric = histogram.calculateMetric();
|
||||
snapshot = metric;
|
||||
latestMetric = metric;
|
||||
return metric;
|
||||
}
|
||||
|
||||
|
||||
@@ -11,6 +11,6 @@ public class TimerMetric {
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "IimerMetric [counterMetric=" + counterMetric + ", histogramMetric=" + histogramMetric + "]";
|
||||
return "TimerMetric [counterMetric=" + counterMetric + ", histogramMetric=" + histogramMetric + "]";
|
||||
}
|
||||
}
|
||||
|
||||
+78
-109
@@ -84,24 +84,36 @@ public class JmxExporter implements MetricRegistryListener {
|
||||
}
|
||||
}
|
||||
|
||||
private void registerMBean(Object mBean, ObjectName objectName) throws InstanceAlreadyExistsException, JMException {
|
||||
ObjectInstance objectInstance = mBeanServer.registerMBean(mBean, objectName);
|
||||
if (objectInstance != null) {
|
||||
// the websphere mbeanserver rewrites the objectname to include cell, node & server info
|
||||
// make sure we capture the new objectName for unregistration
|
||||
registered.put(objectName, objectInstance.getObjectName());
|
||||
} else {
|
||||
registered.put(objectName, objectName);
|
||||
private void registerMBean(Object mBean, ObjectName objectName) {
|
||||
try {
|
||||
ObjectInstance objectInstance = mBeanServer.registerMBean(mBean, objectName);
|
||||
if (objectInstance != null) {
|
||||
// the websphere mbeanserver rewrites the objectname to include cell, node & server info
|
||||
// make sure we capture the new objectName for unregistration
|
||||
registered.put(objectName, objectInstance.getObjectName());
|
||||
} else {
|
||||
registered.put(objectName, objectName);
|
||||
}
|
||||
} catch (InstanceAlreadyExistsException e) {
|
||||
logger.debug("Unable to register:" + objectName, e);
|
||||
} catch (JMException e) {
|
||||
logger.warn("Unable to register:" + objectName, e);
|
||||
}
|
||||
}
|
||||
|
||||
private void unregisterMBean(ObjectName originalObjectName) throws InstanceNotFoundException,
|
||||
MBeanRegistrationException {
|
||||
private void unregisterMBean(ObjectName originalObjectName) {
|
||||
ObjectName storedObjectName = registered.remove(originalObjectName);
|
||||
if (storedObjectName != null) {
|
||||
mBeanServer.unregisterMBean(storedObjectName);
|
||||
} else {
|
||||
mBeanServer.unregisterMBean(originalObjectName);
|
||||
try {
|
||||
if (storedObjectName != null) {
|
||||
mBeanServer.unregisterMBean(storedObjectName);
|
||||
|
||||
} else {
|
||||
mBeanServer.unregisterMBean(originalObjectName);
|
||||
}
|
||||
} catch (InstanceNotFoundException e) {
|
||||
logger.debug("Unable to unregister:" + originalObjectName, e);
|
||||
} catch (MBeanRegistrationException e) {
|
||||
logger.warn("Unable to unregister:" + originalObjectName, e);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -120,95 +132,50 @@ public class JmxExporter implements MetricRegistryListener {
|
||||
|
||||
@Override
|
||||
public void onGaugeAdded(String name, Gauge gauge) {
|
||||
try {
|
||||
final ObjectName objectName = createName("gauges", name);
|
||||
registerMBean(new JmxGauge(gauge, objectName), objectName);
|
||||
} catch (InstanceAlreadyExistsException e) {
|
||||
logger.debug("Unable to register gauge", e);
|
||||
} catch (JMException e) {
|
||||
logger.warn("Unable to register gauge", e);
|
||||
}
|
||||
final ObjectName objectName = createName("gauges", name);
|
||||
registerMBean(new JmxGauge(gauge, objectName), objectName);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCounterAdded(String name, Counter counter) {
|
||||
try {
|
||||
final ObjectName objectName = createName("counters", name);
|
||||
registerMBean(new JmxCounter(counter, objectName), objectName);
|
||||
} catch (InstanceAlreadyExistsException e) {
|
||||
logger.debug("Unable to register counter", e);
|
||||
} catch (JMException e) {
|
||||
logger.warn("Unable to register counter", e);
|
||||
}
|
||||
|
||||
final ObjectName objectName = createName("counters", name);
|
||||
registerMBean(new JmxCounter(counter, objectName), objectName);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onHistogramAdded(String name, Histogram histogram) {
|
||||
try {
|
||||
final ObjectName objectName = createName("histograms", name);
|
||||
registerMBean(new JmxHistogram(histogram, objectName), objectName);
|
||||
} catch (InstanceAlreadyExistsException e) {
|
||||
logger.debug("Unable to register histogram", e);
|
||||
} catch (JMException e) {
|
||||
logger.warn("Unable to register histogram", e);
|
||||
}
|
||||
|
||||
final ObjectName objectName = createName("histograms", name);
|
||||
registerMBean(new JmxHistogram(histogram, objectName), objectName);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onTimerAdded(String name, Timer timer) {
|
||||
try {
|
||||
final ObjectName objectName = createName("timers", name);
|
||||
registerMBean(new JmxTimer(timer, objectName), objectName);
|
||||
} catch (InstanceAlreadyExistsException e) {
|
||||
logger.debug("Unable to register timer", e);
|
||||
} catch (JMException e) {
|
||||
logger.warn("Unable to register timer", e);
|
||||
}
|
||||
|
||||
final ObjectName objectName = createName("timers", name);
|
||||
registerMBean(new JmxTimer(timer, objectName), objectName);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onGaugeRemoved(String name) {
|
||||
try {
|
||||
final ObjectName objectName = createName("guages", name);
|
||||
unregisterMBean(objectName);
|
||||
} catch (JMException e) {
|
||||
logger.warn("Unable to register guage", e);
|
||||
}
|
||||
final ObjectName objectName = createName("guages", name);
|
||||
unregisterMBean(objectName);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCounterRemoved(String name) {
|
||||
try {
|
||||
final ObjectName objectName = createName("counters", name);
|
||||
unregisterMBean(objectName);
|
||||
} catch (JMException e) {
|
||||
logger.warn("Unable to register counter", e);
|
||||
}
|
||||
|
||||
final ObjectName objectName = createName("counters", name);
|
||||
unregisterMBean(objectName);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onHistogramRemoved(String name) {
|
||||
try {
|
||||
final ObjectName objectName = createName("histograms", name);
|
||||
unregisterMBean(objectName);
|
||||
} catch (JMException e) {
|
||||
logger.warn("Unable to register histogram", e);
|
||||
}
|
||||
|
||||
final ObjectName objectName = createName("histograms", name);
|
||||
unregisterMBean(objectName);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onTimerRemoved(String name) {
|
||||
try {
|
||||
final ObjectName objectName = createName("timers", name);
|
||||
unregisterMBean(objectName);
|
||||
} catch (JMException e) {
|
||||
logger.warn("Unable to register timer", e);
|
||||
}
|
||||
final ObjectName objectName = createName("timers", name);
|
||||
unregisterMBean(objectName);
|
||||
}
|
||||
|
||||
public interface MetricMBean {
|
||||
@@ -220,13 +187,13 @@ public class JmxExporter implements MetricRegistryListener {
|
||||
}
|
||||
|
||||
public interface JmxCounterMBean extends MetricMBean {
|
||||
long getLastCount();
|
||||
long getLatestCount();
|
||||
|
||||
double getLastRate();
|
||||
long getLatestRate();
|
||||
|
||||
long getTotalCount();
|
||||
|
||||
double getTotalRate();
|
||||
long getMeanRate();
|
||||
}
|
||||
|
||||
public interface JmxHistogramMBean extends MetricMBean {
|
||||
@@ -236,22 +203,26 @@ public class JmxExporter implements MetricRegistryListener {
|
||||
long getMax();
|
||||
|
||||
double getMean();
|
||||
|
||||
// TODO: add pcts
|
||||
}
|
||||
|
||||
public interface JmxTimerMBean extends MetricMBean {
|
||||
long getLastCount();
|
||||
long getLatestCount();
|
||||
|
||||
double getLastRate();
|
||||
long getLatestRate();
|
||||
|
||||
long getTotalCount();
|
||||
|
||||
double getTotalRate();
|
||||
long getMeanRate();
|
||||
|
||||
long getMin();
|
||||
long getMinLatency();
|
||||
|
||||
long getMax();
|
||||
long getMaxLatency();
|
||||
|
||||
double getMean();
|
||||
double getMeanLatency();
|
||||
|
||||
// TODO: add pcts
|
||||
}
|
||||
|
||||
private abstract static class AbstractBean implements MetricMBean {
|
||||
@@ -291,23 +262,23 @@ public class JmxExporter implements MetricRegistryListener {
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getLastCount() {
|
||||
return metric.snapshot.lastCount;
|
||||
public long getLatestCount() {
|
||||
return metric.latestMetric.latestCount;
|
||||
}
|
||||
|
||||
@Override
|
||||
public double getLastRate() {
|
||||
return metric.snapshot.lastRate;
|
||||
public long getLatestRate() {
|
||||
return metric.latestMetric.latestRate;
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getTotalCount() {
|
||||
return metric.snapshot.totalCount;
|
||||
return metric.latestMetric.totalCount;
|
||||
}
|
||||
|
||||
@Override
|
||||
public double getTotalRate() {
|
||||
return metric.snapshot.meanRate;
|
||||
public long getMeanRate() {
|
||||
return metric.latestMetric.meanRate;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -322,17 +293,17 @@ public class JmxExporter implements MetricRegistryListener {
|
||||
|
||||
@Override
|
||||
public long getMin() {
|
||||
return metric.snapshot.min;
|
||||
return metric.latestMetric.min;
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getMax() {
|
||||
return metric.snapshot.max;
|
||||
return metric.latestMetric.max;
|
||||
}
|
||||
|
||||
@Override
|
||||
public double getMean() {
|
||||
return metric.snapshot.mean;
|
||||
return metric.latestMetric.mean;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -344,41 +315,39 @@ public class JmxExporter implements MetricRegistryListener {
|
||||
this.metric = metric;
|
||||
}
|
||||
|
||||
// TODO: other data
|
||||
@Override
|
||||
public long getLastCount() {
|
||||
return metric.snapshot.counterMetric.lastCount;
|
||||
public long getLatestCount() {
|
||||
return metric.latestMetric.counterMetric.latestCount;
|
||||
}
|
||||
|
||||
@Override
|
||||
public double getLastRate() {
|
||||
return metric.snapshot.counterMetric.lastRate;
|
||||
public long getLatestRate() {
|
||||
return metric.latestMetric.counterMetric.latestRate;
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getTotalCount() {
|
||||
return metric.snapshot.counterMetric.totalCount;
|
||||
return metric.latestMetric.counterMetric.totalCount;
|
||||
}
|
||||
|
||||
@Override
|
||||
public double getTotalRate() {
|
||||
return metric.snapshot.counterMetric.meanRate;
|
||||
public long getMeanRate() {
|
||||
return metric.latestMetric.counterMetric.meanRate;
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getMin() {
|
||||
return metric.snapshot.histogramMetric.min;
|
||||
public long getMinLatency() {
|
||||
return metric.latestMetric.histogramMetric.min;
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getMax() {
|
||||
return metric.snapshot.histogramMetric.max;
|
||||
public long getMaxLatency() {
|
||||
return metric.latestMetric.histogramMetric.max;
|
||||
}
|
||||
|
||||
@Override
|
||||
public double getMean() {
|
||||
return metric.snapshot.histogramMetric.mean;
|
||||
public double getMeanLatency() {
|
||||
return metric.latestMetric.histogramMetric.mean;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+28
-15
@@ -9,6 +9,8 @@ import java.io.PrintStream;
|
||||
import java.util.Date;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.SortedMap;
|
||||
import java.util.TreeMap;
|
||||
|
||||
import org.springside.modules.metrics.Counter;
|
||||
import org.springside.modules.metrics.CounterMetric;
|
||||
@@ -32,7 +34,7 @@ public class ConsoleReporter implements Reporter {
|
||||
|
||||
if (!gauges.isEmpty()) {
|
||||
printWithBanner("-- Gaugues", '-');
|
||||
for (Map.Entry<String, Gauge> entry : gauges.entrySet()) {
|
||||
for (Map.Entry<String, Gauge> entry : getSortedMetrics(gauges).entrySet()) {
|
||||
output.println(entry.getKey());
|
||||
printGauge(entry.getValue());
|
||||
}
|
||||
@@ -41,27 +43,27 @@ public class ConsoleReporter implements Reporter {
|
||||
|
||||
if (!counters.isEmpty()) {
|
||||
printWithBanner("-- Counters", '-');
|
||||
for (Map.Entry<String, Counter> entry : counters.entrySet()) {
|
||||
for (Map.Entry<String, Counter> entry : getSortedMetrics(counters).entrySet()) {
|
||||
output.println(entry.getKey());
|
||||
printCounter(entry.getValue().snapshot);
|
||||
printCounter(entry.getValue().latestMetric);
|
||||
}
|
||||
output.println();
|
||||
}
|
||||
|
||||
if (!histograms.isEmpty()) {
|
||||
printWithBanner("-- Histograms", '-');
|
||||
for (Map.Entry<String, Histogram> entry : histograms.entrySet()) {
|
||||
for (Map.Entry<String, Histogram> entry : getSortedMetrics(histograms).entrySet()) {
|
||||
output.println(entry.getKey());
|
||||
printHistogram(entry.getValue().snapshot);
|
||||
printHistogram(entry.getValue().latestMetric);
|
||||
}
|
||||
output.println();
|
||||
}
|
||||
|
||||
if (!timers.isEmpty()) {
|
||||
printWithBanner("-- Timers", '-');
|
||||
for (Map.Entry<String, Timer> entry : timers.entrySet()) {
|
||||
for (Map.Entry<String, Timer> entry : getSortedMetrics(timers).entrySet()) {
|
||||
output.println(entry.getKey());
|
||||
printTimer(entry.getValue().snapshot);
|
||||
printTimer(entry.getValue().latestMetric);
|
||||
}
|
||||
output.println();
|
||||
}
|
||||
@@ -81,9 +83,9 @@ public class ConsoleReporter implements Reporter {
|
||||
}
|
||||
|
||||
private void printCounter(CounterMetric counter) {
|
||||
output.printf(" last count = %d%n", counter.lastCount);
|
||||
output.printf(" latest count = %d%n", counter.latestCount);
|
||||
output.printf(" total count = %d%n", counter.totalCount);
|
||||
output.printf(" last rate = %d%n", counter.lastRate);
|
||||
output.printf(" latest rate = %d%n", counter.latestRate);
|
||||
output.printf(" mean rate = %d%n", counter.meanRate);
|
||||
}
|
||||
|
||||
@@ -97,14 +99,25 @@ public class ConsoleReporter implements Reporter {
|
||||
}
|
||||
|
||||
private void printTimer(TimerMetric timer) {
|
||||
output.printf(" count = %d%n", timer.counterMetric.totalCount);
|
||||
output.printf(" last rate = %d%n", timer.counterMetric.lastRate);
|
||||
output.printf(" latest count = %d%n", timer.counterMetric.latestCount);
|
||||
output.printf(" total count = %d%n", timer.counterMetric.totalCount);
|
||||
output.printf(" latest rate = %d%n", timer.counterMetric.latestRate);
|
||||
output.printf(" mean rate = %d%n", timer.counterMetric.meanRate);
|
||||
output.printf(" min = %d ms%n", timer.histogramMetric.min);
|
||||
output.printf(" max = %d ms%n", timer.histogramMetric.max);
|
||||
output.printf(" mean = %2.2f ms%n", timer.histogramMetric.mean);
|
||||
output.printf(" min latency = %d ms%n", timer.histogramMetric.min);
|
||||
output.printf(" max latency = %d ms%n", timer.histogramMetric.max);
|
||||
output.printf(" mean latency = %2.2f ms%n", timer.histogramMetric.mean);
|
||||
for (Entry<Double, Long> pct : timer.histogramMetric.pcts.entrySet()) {
|
||||
output.printf(" %2.2f%% <= %d ms%n", pct.getKey(), pct.getValue());
|
||||
output.printf(" %2.2f%% latency <= %d ms%n", pct.getKey(), pct.getValue());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回按metrics name排序的Map.
|
||||
*
|
||||
* 从get的性能考虑,没有使用ConcurrentSkipListMap而是仍然使用ConcurrentHashMap,因此每次报告时需要用TreeMap重新排序.
|
||||
*/
|
||||
private <T> SortedMap<String, T> getSortedMetrics(Map<String, T> metrics) {
|
||||
return new TreeMap<String, T>(metrics);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+5
-5
@@ -71,15 +71,15 @@ public class GraphiteReporter implements Reporter {
|
||||
}
|
||||
|
||||
for (Map.Entry<String, Counter> entry : counters.entrySet()) {
|
||||
reportCounter(entry.getKey(), entry.getValue().snapshot, timestamp);
|
||||
reportCounter(entry.getKey(), entry.getValue().latestMetric, timestamp);
|
||||
}
|
||||
|
||||
for (Map.Entry<String, Histogram> entry : histograms.entrySet()) {
|
||||
reportHistogram(entry.getKey(), entry.getValue().snapshot, timestamp);
|
||||
reportHistogram(entry.getKey(), entry.getValue().latestMetric, timestamp);
|
||||
}
|
||||
|
||||
for (Map.Entry<String, Timer> entry : timers.entrySet()) {
|
||||
reportTimer(entry.getKey(), entry.getValue().snapshot, timestamp);
|
||||
reportTimer(entry.getKey(), entry.getValue().latestMetric, timestamp);
|
||||
}
|
||||
|
||||
flush();
|
||||
@@ -101,7 +101,7 @@ public class GraphiteReporter implements Reporter {
|
||||
}
|
||||
|
||||
private void reportCounter(String name, CounterMetric counter, long timestamp) throws IOException {
|
||||
send(MetricRegistry.name(prefix, name, "count"), format(counter.lastCount), timestamp);
|
||||
send(MetricRegistry.name(prefix, name, "count"), format(counter.latestCount), timestamp);
|
||||
}
|
||||
|
||||
private void reportHistogram(String name, HistogramMetric histogram, long timestamp) throws IOException {
|
||||
@@ -115,7 +115,7 @@ public class GraphiteReporter implements Reporter {
|
||||
}
|
||||
|
||||
private void reportTimer(String name, TimerMetric timer, long timestamp) throws IOException {
|
||||
send(MetricRegistry.name(prefix, name, "count"), format(timer.counterMetric.lastCount), timestamp);
|
||||
send(MetricRegistry.name(prefix, name, "count"), format(timer.counterMetric.latestCount), timestamp);
|
||||
|
||||
send(MetricRegistry.name(prefix, name, "min"), format(timer.histogramMetric.min), timestamp);
|
||||
send(MetricRegistry.name(prefix, name, "max"), format(timer.histogramMetric.max), timestamp);
|
||||
|
||||
+5
-5
@@ -7,7 +7,7 @@ package org.springside.modules.metrics.reporter;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.SortedMap;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
import java.util.concurrent.ThreadFactory;
|
||||
@@ -86,10 +86,10 @@ public class ReportScheduler {
|
||||
public void report() {
|
||||
|
||||
// 取出所有Metrics,已按名称排序.
|
||||
SortedMap<String, Gauge> gaugeMap = metricRegistry.getGauges();
|
||||
SortedMap<String, Counter> counterMap = metricRegistry.getCounters();
|
||||
SortedMap<String, Histogram> histogramMap = metricRegistry.getHistograms();
|
||||
SortedMap<String, Timer> timerMap = metricRegistry.getTimers();
|
||||
Map<String, Gauge> gaugeMap = metricRegistry.getGauges();
|
||||
Map<String, Counter> counterMap = metricRegistry.getCounters();
|
||||
Map<String, Histogram> histogramMap = metricRegistry.getHistograms();
|
||||
Map<String, Timer> timerMap = metricRegistry.getTimers();
|
||||
|
||||
// 调度每个Metrics的caculateMetrics()方法,计算单位时间内的metrics值
|
||||
for (Gauge gauge : gaugeMap.values()) {
|
||||
|
||||
+25
-14
@@ -7,6 +7,8 @@ package org.springside.modules.metrics.reporter;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.SortedMap;
|
||||
import java.util.TreeMap;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
@@ -38,22 +40,21 @@ public class Slf4jReporter implements Reporter {
|
||||
public void report(Map<String, Gauge> gauges, Map<String, Counter> counters, Map<String, Histogram> histograms,
|
||||
Map<String, Timer> timers) {
|
||||
|
||||
for (Entry<String, Gauge> entry : gauges.entrySet()) {
|
||||
for (Entry<String, Gauge> entry : getSortedMetrics(gauges).entrySet()) {
|
||||
logGauge(entry.getKey(), entry.getValue().snapshot);
|
||||
}
|
||||
|
||||
for (Entry<String, Counter> entry : counters.entrySet()) {
|
||||
logCounter(entry.getKey(), entry.getValue().snapshot);
|
||||
for (Entry<String, Counter> entry : getSortedMetrics(counters).entrySet()) {
|
||||
logCounter(entry.getKey(), entry.getValue().latestMetric);
|
||||
}
|
||||
|
||||
for (Entry<String, Histogram> entry : histograms.entrySet()) {
|
||||
logHistogram(entry.getKey(), entry.getValue().snapshot);
|
||||
for (Entry<String, Histogram> entry : getSortedMetrics(histograms).entrySet()) {
|
||||
logHistogram(entry.getKey(), entry.getValue().latestMetric);
|
||||
}
|
||||
|
||||
for (Entry<String, Timer> entry : timers.entrySet()) {
|
||||
logTimer(entry.getKey(), entry.getValue().snapshot);
|
||||
for (Entry<String, Timer> entry : getSortedMetrics(timers).entrySet()) {
|
||||
logTimer(entry.getKey(), entry.getValue().latestMetric);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void logGauge(String name, Number gauge) {
|
||||
@@ -61,8 +62,8 @@ public class Slf4jReporter implements Reporter {
|
||||
}
|
||||
|
||||
private void logCounter(String name, CounterMetric counter) {
|
||||
reportLogger.info("type=COUNTER, name={}, count={}, lastRate={}, meanRate={}", name, counter.totalCount,
|
||||
counter.lastRate, counter.meanRate);
|
||||
reportLogger.info("type=COUNTER, name={}, totalCount={}, meanRate={}, latestRate={}", name, counter.totalCount,
|
||||
counter.meanRate, counter.latestRate);
|
||||
}
|
||||
|
||||
private void logHistogram(String name, HistogramMetric histogram) {
|
||||
@@ -71,10 +72,11 @@ public class Slf4jReporter implements Reporter {
|
||||
}
|
||||
|
||||
private void logTimer(String name, TimerMetric timer) {
|
||||
reportLogger.info("type=TIMER, name={}, count={}, lastRate={}, meanRate={}, min={}ms, max={}ms, mean={}ms",
|
||||
name, timer.counterMetric.totalCount, timer.counterMetric.lastRate, timer.counterMetric.meanRate,
|
||||
timer.histogramMetric.min, timer.histogramMetric.max, timer.histogramMetric.mean,
|
||||
buildPcts(timer.histogramMetric.pcts));
|
||||
reportLogger
|
||||
.info("type=TIMER, name={}, totalCount={}, meanRate={}, latestRate={}, minLatency={}ms, maxLatency={}ms, meanLatency={}ms{}",
|
||||
name, timer.counterMetric.totalCount, timer.counterMetric.meanRate,
|
||||
timer.counterMetric.latestRate, timer.histogramMetric.min, timer.histogramMetric.max,
|
||||
timer.histogramMetric.mean, buildPcts(timer.histogramMetric.pcts));
|
||||
}
|
||||
|
||||
private String buildPcts(Map<Double, Long> pcts) {
|
||||
@@ -86,4 +88,13 @@ public class Slf4jReporter implements Reporter {
|
||||
|
||||
return builder.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回按metrics name排序的Map.
|
||||
*
|
||||
* 从get的性能考虑,没有使用ConcurrentSkipListMap而是仍然使用ConcurrentHashMap,因此每次顺序报告时需要用TreeMap重新排序.
|
||||
*/
|
||||
private <T> SortedMap<String, T> getSortedMetrics(Map<String, T> metrics) {
|
||||
return new TreeMap<String, T>(metrics);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -32,8 +32,8 @@ public class CounterTest {
|
||||
CounterMetric metric = counter.calculateMetric();
|
||||
assertThat(metric.totalCount).isEqualTo(60);
|
||||
assertThat(metric.meanRate).isEqualTo(60);
|
||||
assertThat(metric.lastCount).isEqualTo(60);
|
||||
assertThat(metric.lastRate).isEqualTo(60);
|
||||
assertThat(metric.latestCount).isEqualTo(60);
|
||||
assertThat(metric.latestRate).isEqualTo(60);
|
||||
|
||||
counter.inc(20);
|
||||
clock.increaseTime(1000);
|
||||
@@ -41,8 +41,8 @@ public class CounterTest {
|
||||
|
||||
assertThat(metric.totalCount).isEqualTo(80);
|
||||
assertThat(metric.meanRate).isEqualTo(40);
|
||||
assertThat(metric.lastCount).isEqualTo(20);
|
||||
assertThat(metric.lastRate).isEqualTo(20);
|
||||
assertThat(metric.latestCount).isEqualTo(20);
|
||||
assertThat(metric.latestRate).isEqualTo(20);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -67,6 +67,6 @@ public class CounterTest {
|
||||
|
||||
CounterMetric metric = counter.calculateMetric();
|
||||
assertThat(metric.totalCount).isEqualTo(0);
|
||||
assertThat(metric.lastRate).isEqualTo(0);
|
||||
assertThat(metric.latestRate).isEqualTo(0);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -32,8 +32,8 @@ public class TimerTest {
|
||||
|
||||
assertThat(metric.counterMetric.totalCount).isEqualTo(2);
|
||||
assertThat(metric.counterMetric.meanRate).isEqualTo(4);
|
||||
assertThat(metric.counterMetric.lastCount).isEqualTo(2);
|
||||
assertThat(metric.counterMetric.lastRate).isEqualTo(4);
|
||||
assertThat(metric.counterMetric.latestCount).isEqualTo(2);
|
||||
assertThat(metric.counterMetric.latestRate).isEqualTo(4);
|
||||
|
||||
assertThat(metric.histogramMetric.min).isEqualTo(200);
|
||||
assertThat(metric.histogramMetric.mean).isEqualTo(250);
|
||||
|
||||
Reference in New Issue
Block a user