#243 一个参考CodaHale的全新的Metrics Apia
This commit is contained in:
@@ -0,0 +1,5 @@
|
||||
package org.springside.modules.metrics;
|
||||
|
||||
public interface Counting {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>org.springside</groupId>
|
||||
<artifactId>springside-parent</artifactId>
|
||||
<version>4.1.1-SNAPSHOT</version>
|
||||
<relativePath>../parent/</relativePath>
|
||||
</parent>
|
||||
<artifactId>springside-metrics</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
<name>Springside :: Module :: Metrics</name>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>slf4j-api</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- TEST -->
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.mockito</groupId>
|
||||
<artifactId>mockito-core</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>ch.qos.logback</groupId>
|
||||
<artifactId>logback-classic</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<!-- source attach plugin -->
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-source-plugin</artifactId>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>attach-sources</id>
|
||||
<goals>
|
||||
<goal>jar</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
|
||||
<!-- enforcer, 规则统一定义在parent -->
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-enforcer-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
||||
@@ -0,0 +1,71 @@
|
||||
package org.springside.modules.metrics;
|
||||
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
||||
|
||||
import org.springside.modules.metrics.utils.Clock;
|
||||
|
||||
public class Counter {
|
||||
|
||||
private AtomicLong counter = new AtomicLong(0);
|
||||
private long lastReportCounter = 0L;
|
||||
|
||||
private Clock clock;
|
||||
private long startTime;
|
||||
private long lastReportTime;
|
||||
|
||||
public Counter() {
|
||||
this(Clock.DEFAULT);
|
||||
}
|
||||
|
||||
public Counter(Clock clock) {
|
||||
this.clock = clock;
|
||||
startTime = clock.getCurrentTime();
|
||||
lastReportTime = startTime;
|
||||
}
|
||||
|
||||
public void inc() {
|
||||
counter.incrementAndGet();
|
||||
}
|
||||
|
||||
public void inc(long n) {
|
||||
counter.addAndGet(n);
|
||||
}
|
||||
|
||||
public void dec() {
|
||||
counter.decrementAndGet();
|
||||
}
|
||||
|
||||
public void dec(long n) {
|
||||
counter.addAndGet(-n);
|
||||
}
|
||||
|
||||
public long reset() {
|
||||
return counter.getAndSet(0);
|
||||
}
|
||||
|
||||
public long getCount() {
|
||||
return counter.get();
|
||||
}
|
||||
|
||||
public double getLastRate() {
|
||||
long currentCounter = counter.get();
|
||||
long reportTime = clock.getCurrentTime();
|
||||
|
||||
double rate = 0.0;
|
||||
if (currentCounter != 0) {
|
||||
rate = ((currentCounter - lastReportCounter) / (reportTime - lastReportTime)) * 1000;
|
||||
}
|
||||
lastReportCounter = currentCounter;
|
||||
lastReportTime = reportTime;
|
||||
|
||||
return rate;
|
||||
}
|
||||
|
||||
public double getMeanRate() {
|
||||
if (counter.get() == 0) {
|
||||
return 0.0;
|
||||
} else {
|
||||
return (counter.get() / (clock.getCurrentTime() - startTime)) * 1000;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
package org.springside.modules.metrics;
|
||||
|
||||
import org.springside.modules.metrics.utils.Clock;
|
||||
|
||||
public class Execution {
|
||||
|
||||
public static class ExecutiomTimer {
|
||||
private final Execution execution;
|
||||
private final Clock clock;
|
||||
private final long startTime;
|
||||
|
||||
private ExecutiomTimer(Execution execution, Clock clock) {
|
||||
this.execution = execution;
|
||||
this.clock = clock;
|
||||
this.startTime = clock.getCurrentTime();
|
||||
}
|
||||
|
||||
public long stop() {
|
||||
final long elapsed = clock.getCurrentTime() - startTime;
|
||||
execution.update(elapsed);
|
||||
return elapsed;
|
||||
}
|
||||
}
|
||||
|
||||
private Counter counter;
|
||||
private Histogram histogram;
|
||||
private Clock clock;
|
||||
|
||||
public Execution() {
|
||||
this(Clock.DEFAULT);
|
||||
}
|
||||
|
||||
public Execution(Clock clock) {
|
||||
this.clock = clock;
|
||||
}
|
||||
|
||||
public ExecutiomTimer start() {
|
||||
return new ExecutiomTimer(this, clock);
|
||||
}
|
||||
|
||||
private void update(long duration) {
|
||||
histogram.update(duration);
|
||||
counter.inc();
|
||||
}
|
||||
|
||||
public HistogramSnapshot getSnapshot() {
|
||||
return histogram.getSnapshot();
|
||||
}
|
||||
|
||||
public long getCount() {
|
||||
return counter.getCount();
|
||||
}
|
||||
|
||||
public double getLastRate() {
|
||||
return counter.getLastRate();
|
||||
}
|
||||
|
||||
public double getMeanRate() {
|
||||
return counter.getMeanRate();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
package org.springside.modules.metrics;
|
||||
|
||||
import java.util.concurrent.ConcurrentSkipListMap;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
||||
|
||||
import org.springside.modules.metrics.utils.Clock;
|
||||
|
||||
public class Histogram {
|
||||
|
||||
// allow for this many duplicate ticks before overwriting measurements
|
||||
private static final int COLLISION_BUFFER = 256;
|
||||
// only trim on updating once every N
|
||||
private static final int TRIM_THRESHOLD = 256;
|
||||
|
||||
private final Clock clock;
|
||||
private final ConcurrentSkipListMap<Long, Long> measurements;
|
||||
private final long window;
|
||||
private final AtomicLong lastTick;
|
||||
private final AtomicLong count;
|
||||
|
||||
public Histogram(long window, TimeUnit windowUnit) {
|
||||
this(window, windowUnit, Clock.DEFAULT);
|
||||
}
|
||||
|
||||
public Histogram(long window, TimeUnit windowUnit, Clock clock) {
|
||||
this.clock = clock;
|
||||
this.measurements = new ConcurrentSkipListMap<Long, Long>();
|
||||
this.window = windowUnit.toMillis(window) * COLLISION_BUFFER;
|
||||
this.lastTick = new AtomicLong();
|
||||
this.count = new AtomicLong();
|
||||
}
|
||||
|
||||
public void update(long value) {
|
||||
if ((count.incrementAndGet() % TRIM_THRESHOLD) == 0) {
|
||||
trim();
|
||||
}
|
||||
measurements.put(getTick(), value);
|
||||
}
|
||||
|
||||
public HistogramSnapshot getSnapshot() {
|
||||
trim();
|
||||
return new HistogramSnapshot(measurements.values());
|
||||
}
|
||||
|
||||
/**
|
||||
* TODO:??
|
||||
*/
|
||||
private long getTick() {
|
||||
for (;;) {
|
||||
final long oldTick = lastTick.get();
|
||||
final long tick = clock.getCurrentTime() * COLLISION_BUFFER;
|
||||
|
||||
final long newTick = tick > oldTick ? tick : oldTick + 1;
|
||||
if (lastTick.compareAndSet(oldTick, newTick)) {
|
||||
return newTick;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void trim() {
|
||||
measurements.headMap(getTick() - window).clear();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,115 @@
|
||||
package org.springside.modules.metrics;
|
||||
|
||||
import static java.lang.Math.*;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
|
||||
public class HistogramSnapshot {
|
||||
|
||||
private final long[] values;
|
||||
|
||||
/**
|
||||
* Create a new {@link HistogramSnapshot} with the given values.
|
||||
*
|
||||
* @param values an unordered set of values in the reservoir
|
||||
*/
|
||||
public HistogramSnapshot(Collection<Long> values) {
|
||||
final Object[] copy = values.toArray();
|
||||
this.values = new long[copy.length];
|
||||
for (int i = 0; i < copy.length; i++) {
|
||||
this.values[i] = (Long) copy[i];
|
||||
}
|
||||
Arrays.sort(this.values);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new {@link HistogramSnapshot} with the given values.
|
||||
*
|
||||
* @param values an unordered set of values in the reservoir
|
||||
*/
|
||||
public HistogramSnapshot(long[] values) {
|
||||
this.values = Arrays.copyOf(values, values.length);
|
||||
Arrays.sort(this.values);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the value at the given quantile.
|
||||
*
|
||||
* @param quantile a given quantile, in {@code [0..1]}
|
||||
* @return the value in the distribution at {@code quantile}
|
||||
*/
|
||||
public double getValue(double quantile) {
|
||||
if ((quantile < 0.0) || (quantile > 1.0)) {
|
||||
throw new IllegalArgumentException(quantile + " is not in [0..1]");
|
||||
}
|
||||
|
||||
if (values.length == 0) {
|
||||
return 0.0;
|
||||
}
|
||||
|
||||
final double pos = quantile * (values.length + 1);
|
||||
|
||||
if (pos < 1) {
|
||||
return values[0];
|
||||
}
|
||||
|
||||
if (pos >= values.length) {
|
||||
return values[values.length - 1];
|
||||
}
|
||||
|
||||
final double lower = values[(int) pos - 1];
|
||||
final double upper = values[(int) pos];
|
||||
return lower + ((pos - floor(pos)) * (upper - lower));
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the value at the 99th percentile in the distribution.
|
||||
*
|
||||
* @return the value at the 99th percentile
|
||||
*/
|
||||
public double get90th() {
|
||||
return getValue(0.9);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the highest value in the snapshot.
|
||||
*
|
||||
* @return the highest value
|
||||
*/
|
||||
public long getMax() {
|
||||
if (values.length == 0) {
|
||||
return 0;
|
||||
}
|
||||
return values[values.length - 1];
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the lowest value in the snapshot.
|
||||
*
|
||||
* @return the lowest value
|
||||
*/
|
||||
public long getMin() {
|
||||
if (values.length == 0) {
|
||||
return 0;
|
||||
}
|
||||
return values[0];
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the arithmetic mean of the values in the snapshot.
|
||||
*
|
||||
* @return the arithmetic mean
|
||||
*/
|
||||
public double getMean() {
|
||||
if (values.length == 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
double sum = 0;
|
||||
for (long value : values) {
|
||||
sum += value;
|
||||
}
|
||||
return sum / values.length;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package org.springside.modules.metrics;
|
||||
|
||||
public class MetricRegistry {
|
||||
|
||||
}
|
||||
+41
@@ -0,0 +1,41 @@
|
||||
package org.springside.modules.metrics.report;
|
||||
|
||||
import java.io.PrintStream;
|
||||
import java.util.Map;
|
||||
import java.util.SortedMap;
|
||||
|
||||
import org.springside.modules.metrics.report.metrics.CounterMetric;
|
||||
import org.springside.modules.metrics.report.metrics.HistogramMetric;
|
||||
|
||||
public class ConsoleReporter implements Reporter {
|
||||
private static final int CONSOLE_WIDTH = 80;
|
||||
|
||||
private PrintStream output = System.out;
|
||||
|
||||
@Override
|
||||
public void report(SortedMap<String, CounterMetric> counters, SortedMap<String, HistogramMetric> histograms) {
|
||||
if (!counters.isEmpty()) {
|
||||
printWithBanner("-- Counters", '-');
|
||||
for (Map.Entry<String, CounterMetric> entry : counters.entrySet()) {
|
||||
output.println(entry.getKey());
|
||||
printCounter(entry.getValue());
|
||||
}
|
||||
output.println();
|
||||
}
|
||||
}
|
||||
|
||||
private void printWithBanner(String s, char c) {
|
||||
output.print(s);
|
||||
output.print(' ');
|
||||
for (int i = 0; i < (CONSOLE_WIDTH - s.length() - 1); i++) {
|
||||
output.print(c);
|
||||
}
|
||||
output.println();
|
||||
}
|
||||
|
||||
private void printCounter(CounterMetric counter) {
|
||||
output.printf(" count = %d%n", counter.coute);
|
||||
output.printf(" last rate = %2.2f events/%s%n", counter.lastRate);
|
||||
output.printf(" mean rate = %2.2f events/%s%n", counter.meanRate);
|
||||
}
|
||||
}
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
package org.springside.modules.metrics.report;
|
||||
|
||||
public class GraphiteReporter {
|
||||
|
||||
}
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
package org.springside.modules.metrics.report;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.springside.modules.metrics.MetricRegistry;
|
||||
|
||||
public class ReportScheduler {
|
||||
|
||||
private List<Reporter> reporters;
|
||||
private MetricRegistry metricRegistry;
|
||||
|
||||
public ReportScheduler(MetricRegistry metricRegistry, List<Reporter> repoters) {
|
||||
this.reporters = repoters;
|
||||
}
|
||||
|
||||
public void report() {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package org.springside.modules.metrics.report;
|
||||
|
||||
import java.util.SortedMap;
|
||||
|
||||
import org.springside.modules.metrics.report.metrics.CounterMetric;
|
||||
import org.springside.modules.metrics.report.metrics.HistogramMetric;
|
||||
|
||||
public interface Reporter {
|
||||
void report(SortedMap<String, CounterMetric> counters, SortedMap<String, HistogramMetric> histograms);
|
||||
}
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
package org.springside.modules.metrics.report.metrics;
|
||||
|
||||
public class CounterMetric {
|
||||
public long coute;
|
||||
public double lastRate;
|
||||
public double meanRate;
|
||||
}
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
package org.springside.modules.metrics.report.metrics;
|
||||
|
||||
public class HistogramMetric {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
package org.springside.modules.metrics.utils;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* 日期提供者,使用它而不是直接取得系统时间,方便测试。
|
||||
*
|
||||
* @author calvin
|
||||
*/
|
||||
public interface Clock {
|
||||
|
||||
Date getCurrentDate();
|
||||
|
||||
long getCurrentTime();
|
||||
|
||||
static final Clock DEFAULT = new DefaultDateProvider();
|
||||
|
||||
/**
|
||||
* 默认时间提供者,返回当前的时间,线程安全。
|
||||
*/
|
||||
public static class DefaultDateProvider implements Clock {
|
||||
|
||||
@Override
|
||||
public Date getCurrentDate() {
|
||||
return new Date();
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getCurrentTime() {
|
||||
return System.currentTimeMillis();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 可配置的时间提供者,用于测试.
|
||||
*/
|
||||
public static class MockedDateProvider implements Clock {
|
||||
|
||||
private long time;
|
||||
|
||||
public MockedDateProvider(Date date) {
|
||||
this.time = date.getTime();
|
||||
}
|
||||
|
||||
public MockedDateProvider(long time) {
|
||||
this.time = time;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Date getCurrentDate() {
|
||||
return new Date(time);
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getCurrentTime() {
|
||||
return time;
|
||||
}
|
||||
|
||||
/**
|
||||
* 重新设置时间。
|
||||
*/
|
||||
public void update(Date newDate) {
|
||||
time = newDate.getTime();
|
||||
}
|
||||
|
||||
/**
|
||||
* 重新设置时间。
|
||||
*/
|
||||
public void update(long newTime) {
|
||||
this.time = newTime;
|
||||
}
|
||||
|
||||
/**
|
||||
* 增加时间戳.
|
||||
*/
|
||||
public void incrementTime(int millis) {
|
||||
time += millis;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -16,6 +16,7 @@
|
||||
<module>parent</module>
|
||||
<module>core</module>
|
||||
<module>extension</module>
|
||||
<module>metrics</module>
|
||||
<module>test</module>
|
||||
</modules>
|
||||
</project>
|
||||
Reference in New Issue
Block a user