metrics update

This commit is contained in:
Calvin
2014-09-13 00:28:31 +08:00
parent b0f8770f66
commit a61d40699c
5 changed files with 52 additions and 6 deletions
@@ -18,16 +18,16 @@ import org.springside.modules.metrics.utils.Clock;
public class Counter {
public static Clock clock = Clock.DEFAULT;
public CounterMetric snapshot = new CounterMetric();
public CounterMetric snapshot;
private AtomicLong counter = new AtomicLong(0);
private AtomicLong counter;
private long totalCount = 0L;
private long totalCount;
private long startTime;
private long lastReportTime;
public Counter() {
lastReportTime = clock.getCurrentTime();
reset();
}
public void inc() {
@@ -71,6 +71,14 @@ public class Counter {
return metric;
}
public void reset() {
snapshot = new CounterMetric();
counter = new AtomicLong(0);
totalCount = 0L;
startTime = clock.getCurrentTime();
lastReportTime = 0L;
}
@Override
public String toString() {
return "Counter [counter=" + counter + ", totalCount=" + totalCount + ", startTime=" + startTime
@@ -97,6 +97,12 @@ public class MetricRegistry {
}
}
public void clearAll() {
counters.clear();
histograms.clear();
timers.clear();
}
private <T> T register(ConcurrentMap<String, T> metrics, String name, T newMetric) {
T existingMetric = metrics.putIfAbsent(name, newMetric);
if (existingMetric != null) {
@@ -66,7 +66,8 @@ public class ConsoleReporter implements Reporter {
}
private void printCounter(CounterMetric counter) {
output.printf(" count = %d%n", counter.totalCount);
output.printf(" last count = %d%n", counter.lastCount);
output.printf(" total count = %d%n", counter.totalCount);
output.printf(" last rate = %2.2f/s%n", counter.lastRate);
output.printf(" mean rate = %2.2f/s%n", counter.meanRate);
}
@@ -43,6 +43,9 @@ public class GraphiteReporter implements Reporter {
private Socket socket;
private Writer writer;
// use to only print connection error message once.
private GraphiteConnStatus graphiteConnStatus = GraphiteConnStatus.CONN_OK;
public GraphiteReporter(InetSocketAddress address) {
this(address, "metrics");
}
@@ -72,8 +75,10 @@ public class GraphiteReporter implements Reporter {
}
flush();
onConnSuccess();
} catch (IOException e) {
logger.warn("Unable to report to Graphite", e);
onConnFail(e);
} finally {
try {
close();
@@ -136,6 +141,10 @@ public class GraphiteReporter implements Reporter {
}
private void close() throws IOException {
if (writer != null) {
writer.flush();
}
if (socket != null) {
socket.close();
}
@@ -154,4 +163,22 @@ public class GraphiteReporter implements Reporter {
private String sanitize(String s) {
return WHITESPACE.matcher(s).replaceAll("-");
}
private void onConnFail(Exception exception) {
if (graphiteConnStatus != GraphiteConnStatus.CONN_NOK) {
logger.warn("Unable to report to Graphite", exception);
graphiteConnStatus = GraphiteConnStatus.CONN_NOK;
}
}
private void onConnSuccess() {
if (graphiteConnStatus != GraphiteConnStatus.CONN_OK) {
logger.info("Graphite connection is recovered.");
graphiteConnStatus = GraphiteConnStatus.CONN_OK;
}
}
private enum GraphiteConnStatus {
CONN_OK, CONN_NOK
}
}
@@ -51,6 +51,10 @@ public class ReportScheduler {
reporters.add(reporter);
}
public void removeReporter(Reporter reporter) {
reporters.remove(reporter);
}
public void start(long period, TimeUnit unit) {
executor.scheduleAtFixedRate(new Runnable() {
@Override