Skip to content

Commit

Permalink
Style and formatting changes to CsvReporter.
Browse files Browse the repository at this point in the history
  • Loading branch information
codahale committed Nov 3, 2011
1 parent c08fc87 commit 33de206
Showing 1 changed file with 120 additions and 127 deletions.
247 changes: 120 additions & 127 deletions metrics-core/src/main/java/com/yammer/metrics/reporting/CsvReporter.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.yammer.metrics.reporting;

import com.yammer.metrics.core.*;
import com.yammer.metrics.util.MetricPredicate;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
Expand All @@ -10,142 +13,132 @@
import java.util.Set;
import java.util.concurrent.TimeUnit;

import com.yammer.metrics.core.CounterMetric;
import com.yammer.metrics.core.GaugeMetric;
import com.yammer.metrics.core.HistogramMetric;
import com.yammer.metrics.core.MeterMetric;
import com.yammer.metrics.core.Metric;
import com.yammer.metrics.core.MetricName;
import com.yammer.metrics.core.MetricsRegistry;
import com.yammer.metrics.core.TimerMetric;
import com.yammer.metrics.util.MetricPredicate;

public class CsvReporter extends AbstractPollingReporter {
private final MetricPredicate _predicate;
private final File _outputDir;
private final Map<MetricName, PrintStream> _streamMap;
private long _startTime;
private final MetricPredicate predicate;
private final File outputDir;
private final Map<MetricName, PrintStream> streamMap;
private long startTime;

public CsvReporter(File outputDir, MetricsRegistry metricsRegistry,
MetricPredicate predicate) throws Exception {
super(metricsRegistry, "csv-reporter");
_outputDir = outputDir;
_predicate = predicate;
_streamMap = new HashMap<MetricName, PrintStream>();
_startTime = 0L;
}
public CsvReporter(File outputDir,
MetricsRegistry metricsRegistry,
MetricPredicate predicate) throws Exception {
super(metricsRegistry, "csv-reporter");
this.outputDir = outputDir;
this.predicate = predicate;
this.streamMap = new HashMap<MetricName, PrintStream>();
this.startTime = 0L;
}

public CsvReporter(File outputDir, MetricsRegistry metricsRegistry)
throws Exception {
this(outputDir, metricsRegistry, MetricPredicate.ALL);
}
public CsvReporter(File outputDir, MetricsRegistry metricsRegistry)
throws Exception {
this(outputDir, metricsRegistry, MetricPredicate.ALL);
}

private PrintStream getPrintStream(MetricName metricName, Metric metric)
throws IOException {
PrintStream stream = null;
synchronized (_streamMap) {
stream = _streamMap.get(metricName);
if (stream == null) {
File newFile = new File(_outputDir, metricName.getName()
+ ".csv");
newFile.createNewFile();
stream = new PrintStream(new FileOutputStream(newFile));
_streamMap.put(metricName, stream);
if (metric instanceof GaugeMetric<?>) {
stream.println("# time,value");
} else if (metric instanceof CounterMetric) {
stream.println("# time,count");
} else if (metric instanceof HistogramMetric) {
stream.println("# time,min,max,mean,median,stddev,90%,95%,99%");
} else if (metric instanceof MeterMetric) {
stream.println("# time,count,1 min rate,mean rate,5 min rate,15 min rate");
} else if (metric instanceof TimerMetric) {
stream.println("# time,min,max,mean,median,stddev,90%,95%,99%");
}
}
}
return stream;
}
private PrintStream getPrintStream(MetricName metricName, Metric metric)
throws IOException {
PrintStream stream;
synchronized (streamMap) {
stream = streamMap.get(metricName);
if (stream == null) {
final File newFile = new File(outputDir, metricName.getName() + ".csv");
if (newFile.createNewFile()) {
stream = new PrintStream(new FileOutputStream(newFile));
streamMap.put(metricName, stream);
if (metric instanceof GaugeMetric<?>) {
stream.println("# time,value");
} else if (metric instanceof CounterMetric) {
stream.println("# time,count");
} else if (metric instanceof HistogramMetric) {
stream.println("# time,min,max,mean,median,stddev,90%,95%,99%");
} else if (metric instanceof MeterMetric) {
stream.println("# time,count,1 min rate,mean rate,5 min rate,15 min rate");
} else if (metric instanceof TimerMetric) {
stream.println("# time,min,max,mean,median,stddev,90%,95%,99%");
}
} else {
throw new IOException("Unable to create " + newFile);
}
}
}
return stream;
}

@Override
public void run() {
long time = (System.currentTimeMillis() - _startTime) / 1000;
Set<Entry<MetricName, Metric>> metrics = metricsRegistry.allMetrics()
.entrySet();
try {
for (Entry<MetricName, Metric> entry : metrics) {
MetricName metricName = entry.getKey();
Metric metric = entry.getValue();
if (_predicate.matches(metricName, metric)) {
StringBuilder buf = new StringBuilder();
buf.append(time).append(",");
if (metric instanceof GaugeMetric<?>) {
Object objVal = ((GaugeMetric<?>) metric).value();
buf.append(objVal);
} else if (metric instanceof CounterMetric) {
buf.append(((CounterMetric) metric).count());
} else if (metric instanceof HistogramMetric) {
HistogramMetric timer = (HistogramMetric) metric;
@Override
public void run() {
final long time = (System.currentTimeMillis() - startTime) / 1000;
final Set<Entry<MetricName, Metric>> metrics = metricsRegistry.allMetrics().entrySet();
try {
for (Entry<MetricName, Metric> entry : metrics) {
final MetricName metricName = entry.getKey();
final Metric metric = entry.getValue();
if (predicate.matches(metricName, metric)) {
final StringBuilder buf = new StringBuilder();
buf.append(time).append(",");
if (metric instanceof GaugeMetric<?>) {
final Object objVal = ((GaugeMetric<?>) metric).value();
buf.append(objVal);
} else if (metric instanceof CounterMetric) {
buf.append(((CounterMetric) metric).count());
} else if (metric instanceof HistogramMetric) {
final HistogramMetric timer = (HistogramMetric) metric;

final double[] percentiles = timer.percentiles(0.5,
0.90, 0.95, 0.99);
buf.append(timer.min()).append(",");
buf.append(timer.max()).append(",");
buf.append(timer.mean()).append(",");
buf.append(percentiles[0]).append(","); // median
buf.append(timer.stdDev()).append(",");
buf.append(percentiles[1]).append(","); // 90%
buf.append(percentiles[2]).append(","); // 95%
buf.append(percentiles[3]); // 99 %
} else if (metric instanceof MeterMetric) {
buf.append(((MeterMetric) metric).count()).append(",");
buf.append(((MeterMetric) metric).oneMinuteRate())
.append(",");
buf.append(((MeterMetric) metric).meanRate()).append(
",");
buf.append(((MeterMetric) metric).fiveMinuteRate())
.append(",");
buf.append(((MeterMetric) metric).fifteenMinuteRate());
} else if (metric instanceof TimerMetric) {
TimerMetric timer = (TimerMetric) metric;
final double[] percentiles = timer.percentiles(0.5, 0.90, 0.95, 0.99);
buf.append(timer.min()).append(",");
buf.append(timer.max()).append(",");
buf.append(timer.mean()).append(",");
buf.append(percentiles[0]).append(","); // median
buf.append(timer.stdDev()).append(",");
buf.append(percentiles[1]).append(","); // 90%
buf.append(percentiles[2]).append(","); // 95%
buf.append(percentiles[3]); // 99 %
} else if (metric instanceof MeterMetric) {
buf.append(((MeterMetric) metric).count()).append(",");
buf.append(((MeterMetric) metric).oneMinuteRate())
.append(",");
buf.append(((MeterMetric) metric).meanRate()).append(
",");
buf.append(((MeterMetric) metric).fiveMinuteRate())
.append(",");
buf.append(((MeterMetric) metric).fifteenMinuteRate());
} else if (metric instanceof TimerMetric) {
final TimerMetric timer = (TimerMetric) metric;

final double[] percentiles = timer.percentiles(0.5,
0.90, 0.95, 0.99);
buf.append(timer.min()).append(",");
buf.append(timer.max()).append(",");
buf.append(timer.mean()).append(",");
buf.append(percentiles[0]).append(","); // median
buf.append(timer.stdDev()).append(",");
buf.append(percentiles[1]).append(","); // 90%
buf.append(percentiles[2]).append(","); // 95%
buf.append(percentiles[3]); // 99 %
}
final double[] percentiles = timer.percentiles(0.5, 0.90, 0.95, 0.99);
buf.append(timer.min()).append(",");
buf.append(timer.max()).append(",");
buf.append(timer.mean()).append(",");
buf.append(percentiles[0]).append(","); // median
buf.append(timer.stdDev()).append(",");
buf.append(percentiles[1]).append(","); // 90%
buf.append(percentiles[2]).append(","); // 95%
buf.append(percentiles[3]); // 99 %
}

PrintStream out = getPrintStream(metricName, metric);
out.println(buf.toString());
out.flush();
}
}
final PrintStream out = getPrintStream(metricName, metric);
out.println(buf.toString());
out.flush();
}
}

} catch (Exception e) {
e.printStackTrace();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}

@Override
public void start(long period, TimeUnit unit) {
_startTime = System.currentTimeMillis();
super.start(period, unit);
}
@Override
public void start(long period, TimeUnit unit) {
this.startTime = System.currentTimeMillis();
super.start(period, unit);
}

@Override
public void shutdown() {
try {
super.shutdown();
} finally {
for (PrintStream out : _streamMap.values()) {
out.close();
}
}
}
@Override
public void shutdown() {
try {
super.shutdown();
} finally {
for (PrintStream out : streamMap.values()) {
out.close();
}
}
}
}

0 comments on commit 33de206

Please sign in to comment.