|
@@ -4,6 +4,7 @@ import java.io.PrintStream;
|
|
import java.text.DateFormat;
|
|
import java.text.DateFormat;
|
|
import java.util.Collections;
|
|
import java.util.Collections;
|
|
import java.util.Date;
|
|
import java.util.Date;
|
|
|
|
+import java.util.List;
|
|
import java.util.Locale;
|
|
import java.util.Locale;
|
|
import java.util.Map;
|
|
import java.util.Map;
|
|
import java.util.Set;
|
|
import java.util.Set;
|
|
@@ -23,6 +24,7 @@ import com.codahale.metrics.MetricRegistry;
|
|
import com.codahale.metrics.ScheduledReporter;
|
|
import com.codahale.metrics.ScheduledReporter;
|
|
import com.codahale.metrics.Snapshot;
|
|
import com.codahale.metrics.Snapshot;
|
|
import com.codahale.metrics.Timer;
|
|
import com.codahale.metrics.Timer;
|
|
|
|
+import com.google.common.collect.Lists;
|
|
|
|
|
|
/**
|
|
/**
|
|
* 数据报告
|
|
* 数据报告
|
|
@@ -226,12 +228,6 @@ public class DataReportor extends ScheduledReporter {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
- private static final int CONSOLE_WIDTH = 80;
|
|
|
|
-
|
|
|
|
- private final PrintStream output;
|
|
|
|
-
|
|
|
|
- private final Locale locale;
|
|
|
|
-
|
|
|
|
private final Clock clock;
|
|
private final Clock clock;
|
|
|
|
|
|
private final DateFormat dateFormat;
|
|
private final DateFormat dateFormat;
|
|
@@ -242,8 +238,6 @@ public class DataReportor extends ScheduledReporter {
|
|
Set<MetricAttribute> disabledMetricAttributes) {
|
|
Set<MetricAttribute> disabledMetricAttributes) {
|
|
super(registry, "console-reporter", filter, rateUnit, durationUnit, executor,
|
|
super(registry, "console-reporter", filter, rateUnit, durationUnit, executor,
|
|
shutdownExecutorOnStop, disabledMetricAttributes);
|
|
shutdownExecutorOnStop, disabledMetricAttributes);
|
|
- this.output = output;
|
|
|
|
- this.locale = locale;
|
|
|
|
this.clock = clock;
|
|
this.clock = clock;
|
|
this.dateFormat = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.MEDIUM,
|
|
this.dateFormat = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.MEDIUM,
|
|
locale);
|
|
locale);
|
|
@@ -255,172 +249,113 @@ public class DataReportor extends ScheduledReporter {
|
|
public void report(SortedMap<String, Gauge> gauges, SortedMap<String, Counter> counters,
|
|
public void report(SortedMap<String, Gauge> gauges, SortedMap<String, Counter> counters,
|
|
SortedMap<String, Histogram> histograms, SortedMap<String, Meter> meters,
|
|
SortedMap<String, Histogram> histograms, SortedMap<String, Meter> meters,
|
|
SortedMap<String, Timer> timers) {
|
|
SortedMap<String, Timer> timers) {
|
|
- final String dateTime = dateFormat.format(new Date(clock.getTime()));
|
|
|
|
- printWithBanner(dateTime, '=');
|
|
|
|
- output.println();
|
|
|
|
-
|
|
|
|
- if (!gauges.isEmpty()) {
|
|
|
|
- printWithBanner("-- Gauges", '-');
|
|
|
|
- for (Map.Entry<String, Gauge> entry : gauges.entrySet()) {
|
|
|
|
- output.println(entry.getKey());
|
|
|
|
- printGauge(entry.getValue());
|
|
|
|
- }
|
|
|
|
- output.println();
|
|
|
|
- }
|
|
|
|
|
|
|
|
- if (!counters.isEmpty()) {
|
|
|
|
- printWithBanner("-- Counters", '-');
|
|
|
|
- for (Map.Entry<String, Counter> entry : counters.entrySet()) {
|
|
|
|
- output.println(entry.getKey());
|
|
|
|
- printCounter(entry);
|
|
|
|
- }
|
|
|
|
- output.println();
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- if (!histograms.isEmpty()) {
|
|
|
|
- printWithBanner("-- Histograms", '-');
|
|
|
|
- for (Map.Entry<String, Histogram> entry : histograms.entrySet()) {
|
|
|
|
- output.println(entry.getKey());
|
|
|
|
- printHistogram(entry.getValue());
|
|
|
|
- }
|
|
|
|
- output.println();
|
|
|
|
- }
|
|
|
|
|
|
+ ReportInfo info = new ReportInfo();
|
|
|
|
+ info.setDateTime(new Date(clock.getTime()));
|
|
|
|
+ final List<MeterInfo> meterInfoList = Lists.newArrayList();
|
|
|
|
+ final List<TimerInfo> timerInfoList = Lists.newArrayList();
|
|
|
|
+ info.setMeterInfoList(meterInfoList);
|
|
|
|
+ info.setTimerInfoList(timerInfoList);
|
|
|
|
|
|
if (!meters.isEmpty()) {
|
|
if (!meters.isEmpty()) {
|
|
- printWithBanner("-- Meters", '-');
|
|
|
|
for (Map.Entry<String, Meter> entry : meters.entrySet()) {
|
|
for (Map.Entry<String, Meter> entry : meters.entrySet()) {
|
|
- output.println(entry.getKey());
|
|
|
|
- printMeter(entry.getValue());
|
|
|
|
|
|
+ MeterInfo meterInfo = getMeterInfo(entry.getKey(), entry.getValue());
|
|
|
|
+ meterInfoList.add(meterInfo);
|
|
}
|
|
}
|
|
- output.println();
|
|
|
|
}
|
|
}
|
|
|
|
|
|
if (!timers.isEmpty()) {
|
|
if (!timers.isEmpty()) {
|
|
- printWithBanner("-- Timers", '-');
|
|
|
|
for (Map.Entry<String, Timer> entry : timers.entrySet()) {
|
|
for (Map.Entry<String, Timer> entry : timers.entrySet()) {
|
|
- output.println(entry.getKey());
|
|
|
|
- printTimer(entry.getValue());
|
|
|
|
|
|
+ TimerInfo timerInfo = getTimerInfo(entry.getKey(), entry.getValue());
|
|
|
|
+ timerInfoList.add(timerInfo);
|
|
}
|
|
}
|
|
- output.println();
|
|
|
|
}
|
|
}
|
|
|
|
|
|
- output.println();
|
|
|
|
- output.flush();
|
|
|
|
}
|
|
}
|
|
|
|
|
|
- private void printMeter(Meter meter) {
|
|
|
|
- printIfEnabled(MetricAttribute.COUNT,
|
|
|
|
- String.format(locale, " count = %d", meter.getCount()));
|
|
|
|
- printIfEnabled(MetricAttribute.MEAN_RATE,
|
|
|
|
- String.format(locale, " mean rate = %2.2f events/%s",
|
|
|
|
- convertRate(meter.getMeanRate()), getRateUnit()));
|
|
|
|
- printIfEnabled(MetricAttribute.M1_RATE,
|
|
|
|
- String.format(locale, " 1-minute rate = %2.2f events/%s",
|
|
|
|
- convertRate(meter.getOneMinuteRate()), getRateUnit()));
|
|
|
|
- printIfEnabled(MetricAttribute.M5_RATE,
|
|
|
|
- String.format(locale, " 5-minute rate = %2.2f events/%s",
|
|
|
|
- convertRate(meter.getFiveMinuteRate()), getRateUnit()));
|
|
|
|
- printIfEnabled(MetricAttribute.M15_RATE,
|
|
|
|
- String.format(locale, " 15-minute rate = %2.2f events/%s",
|
|
|
|
- convertRate(meter.getFifteenMinuteRate()), getRateUnit()));
|
|
|
|
- }
|
|
|
|
|
|
+ private MeterInfo getMeterInfo(String key, Meter meter) {
|
|
|
|
|
|
- private void printCounter(Map.Entry<String, Counter> entry) {
|
|
|
|
- output.printf(locale, " count = %d%n", entry.getValue().getCount());
|
|
|
|
- }
|
|
|
|
|
|
+ MeterInfo info = new MeterInfo();
|
|
|
|
+ info.setKey(key);
|
|
|
|
+ info.setRateUnit(getRateUnit());
|
|
|
|
|
|
- private void printGauge(Gauge<?> gauge) {
|
|
|
|
- output.printf(locale, " value = %s%n", gauge.getValue());
|
|
|
|
- }
|
|
|
|
|
|
+ if (isEnabled(MetricAttribute.COUNT)) {
|
|
|
|
+ info.setCount(meter.getCount());
|
|
|
|
+ }
|
|
|
|
+ if (isEnabled(MetricAttribute.MEAN_RATE)) {
|
|
|
|
+ info.setMeanRate(meter.getMeanRate());
|
|
|
|
+ }
|
|
|
|
|
|
- private void printHistogram(Histogram histogram) {
|
|
|
|
- printIfEnabled(MetricAttribute.COUNT,
|
|
|
|
- String.format(locale, " count = %d", histogram.getCount()));
|
|
|
|
- Snapshot snapshot = histogram.getSnapshot();
|
|
|
|
- printIfEnabled(MetricAttribute.MIN,
|
|
|
|
- String.format(locale, " min = %d", snapshot.getMin()));
|
|
|
|
- printIfEnabled(MetricAttribute.MAX,
|
|
|
|
- String.format(locale, " max = %d", snapshot.getMax()));
|
|
|
|
- printIfEnabled(MetricAttribute.MEAN,
|
|
|
|
- String.format(locale, " mean = %2.2f", snapshot.getMean()));
|
|
|
|
- printIfEnabled(MetricAttribute.STDDEV,
|
|
|
|
- String.format(locale, " stddev = %2.2f", snapshot.getStdDev()));
|
|
|
|
- printIfEnabled(MetricAttribute.P50,
|
|
|
|
- String.format(locale, " median = %2.2f", snapshot.getMedian()));
|
|
|
|
- printIfEnabled(MetricAttribute.P75,
|
|
|
|
- String.format(locale, " 75%% <= %2.2f", snapshot.get75thPercentile()));
|
|
|
|
- printIfEnabled(MetricAttribute.P95,
|
|
|
|
- String.format(locale, " 95%% <= %2.2f", snapshot.get95thPercentile()));
|
|
|
|
- printIfEnabled(MetricAttribute.P98,
|
|
|
|
- String.format(locale, " 98%% <= %2.2f", snapshot.get98thPercentile()));
|
|
|
|
- printIfEnabled(MetricAttribute.P99,
|
|
|
|
- String.format(locale, " 99%% <= %2.2f", snapshot.get99thPercentile()));
|
|
|
|
- printIfEnabled(MetricAttribute.P999, String.format(locale, " 99.9%% <= %2.2f",
|
|
|
|
- snapshot.get999thPercentile()));
|
|
|
|
|
|
+ if (isEnabled(MetricAttribute.M1_RATE)) {
|
|
|
|
+ info.setOneMinuteRate(convertRate(meter.getOneMinuteRate()));
|
|
|
|
+ }
|
|
|
|
+ if (isEnabled(MetricAttribute.M5_RATE)) {
|
|
|
|
+ info.setFiveMinuteRate(convertRate(meter.getFiveMinuteRate()));
|
|
|
|
+ }
|
|
|
|
+ if (isEnabled(MetricAttribute.M15_RATE)) {
|
|
|
|
+ info.setFifteenMinuteRate(convertRate(meter.getFifteenMinuteRate()));
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return info;
|
|
}
|
|
}
|
|
|
|
|
|
- private void printTimer(Timer timer) {
|
|
|
|
|
|
+ private TimerInfo getTimerInfo(String key, Timer timer) {
|
|
final Snapshot snapshot = timer.getSnapshot();
|
|
final Snapshot snapshot = timer.getSnapshot();
|
|
- printIfEnabled(MetricAttribute.COUNT,
|
|
|
|
- String.format(locale, " count = %d", timer.getCount()));
|
|
|
|
- printIfEnabled(MetricAttribute.MEAN_RATE,
|
|
|
|
- String.format(locale, " mean rate = %2.2f calls/%s",
|
|
|
|
- convertRate(timer.getMeanRate()), getRateUnit()));
|
|
|
|
- printIfEnabled(MetricAttribute.M1_RATE,
|
|
|
|
- String.format(locale, " 1-minute rate = %2.2f calls/%s",
|
|
|
|
- convertRate(timer.getOneMinuteRate()), getRateUnit()));
|
|
|
|
- printIfEnabled(MetricAttribute.M5_RATE,
|
|
|
|
- String.format(locale, " 5-minute rate = %2.2f calls/%s",
|
|
|
|
- convertRate(timer.getFiveMinuteRate()), getRateUnit()));
|
|
|
|
- printIfEnabled(MetricAttribute.M15_RATE,
|
|
|
|
- String.format(locale, " 15-minute rate = %2.2f calls/%s",
|
|
|
|
- convertRate(timer.getFifteenMinuteRate()), getRateUnit()));
|
|
|
|
-
|
|
|
|
- printIfEnabled(MetricAttribute.MIN, String.format(locale, " min = %2.2f %s",
|
|
|
|
- convertDuration(snapshot.getMin()), getDurationUnit()));
|
|
|
|
- printIfEnabled(MetricAttribute.MAX, String.format(locale, " max = %2.2f %s",
|
|
|
|
- convertDuration(snapshot.getMax()), getDurationUnit()));
|
|
|
|
- printIfEnabled(MetricAttribute.MEAN, String.format(locale, " mean = %2.2f %s",
|
|
|
|
- convertDuration(snapshot.getMean()), getDurationUnit()));
|
|
|
|
- printIfEnabled(MetricAttribute.STDDEV,
|
|
|
|
- String.format(locale, " stddev = %2.2f %s",
|
|
|
|
- convertDuration(snapshot.getStdDev()), getDurationUnit()));
|
|
|
|
- printIfEnabled(MetricAttribute.P50, String.format(locale, " median = %2.2f %s",
|
|
|
|
- convertDuration(snapshot.getMedian()), getDurationUnit()));
|
|
|
|
- printIfEnabled(MetricAttribute.P75, String.format(locale, " 75%% <= %2.2f %s",
|
|
|
|
- convertDuration(snapshot.get75thPercentile()), getDurationUnit()));
|
|
|
|
- printIfEnabled(MetricAttribute.P95, String.format(locale, " 95%% <= %2.2f %s",
|
|
|
|
- convertDuration(snapshot.get95thPercentile()), getDurationUnit()));
|
|
|
|
- printIfEnabled(MetricAttribute.P98, String.format(locale, " 98%% <= %2.2f %s",
|
|
|
|
- convertDuration(snapshot.get98thPercentile()), getDurationUnit()));
|
|
|
|
- printIfEnabled(MetricAttribute.P99, String.format(locale, " 99%% <= %2.2f %s",
|
|
|
|
- convertDuration(snapshot.get99thPercentile()), getDurationUnit()));
|
|
|
|
- printIfEnabled(MetricAttribute.P999, String.format(locale, " 99.9%% <= %2.2f %s",
|
|
|
|
- convertDuration(snapshot.get999thPercentile()), getDurationUnit()));
|
|
|
|
- }
|
|
|
|
|
|
+ TimerInfo info = new TimerInfo();
|
|
|
|
+ info.setKey(key);
|
|
|
|
+ info.setRateUnit(getRateUnit());
|
|
|
|
+ info.setDurationUnit(getDurationUnit());
|
|
|
|
|
|
- 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);
|
|
|
|
|
|
+ if (isEnabled(MetricAttribute.COUNT)) {
|
|
|
|
+ info.setCount(timer.getCount());
|
|
|
|
+ }
|
|
|
|
+ if (isEnabled(MetricAttribute.MEAN_RATE)) {
|
|
|
|
+ info.setMeanRate(timer.getMeanRate());
|
|
}
|
|
}
|
|
- output.println();
|
|
|
|
- }
|
|
|
|
|
|
|
|
- /**
|
|
|
|
- * Print only if the attribute is enabled
|
|
|
|
- *
|
|
|
|
- * @param type
|
|
|
|
- * Metric attribute
|
|
|
|
- * @param status
|
|
|
|
- * Status to be logged
|
|
|
|
- */
|
|
|
|
- private void printIfEnabled(MetricAttribute type, String status) {
|
|
|
|
- if (getDisabledMetricAttributes().contains(type)) {
|
|
|
|
- return;
|
|
|
|
|
|
+ if (isEnabled(MetricAttribute.M1_RATE)) {
|
|
|
|
+ info.setOneMinuteRate(convertRate(timer.getOneMinuteRate()));
|
|
|
|
+ }
|
|
|
|
+ if (isEnabled(MetricAttribute.M5_RATE)) {
|
|
|
|
+ info.setFiveMinuteRate(convertRate(timer.getFiveMinuteRate()));
|
|
|
|
+ }
|
|
|
|
+ if (isEnabled(MetricAttribute.M15_RATE)) {
|
|
|
|
+ info.setFifteenMinuteRate(convertRate(timer.getFifteenMinuteRate()));
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (isEnabled(MetricAttribute.MIN)) {
|
|
|
|
+ info.setMin(convertDuration(snapshot.getMin()));
|
|
|
|
+ }
|
|
|
|
+ if (isEnabled(MetricAttribute.MAX)) {
|
|
|
|
+ info.setMax(convertDuration(snapshot.getMax()));
|
|
}
|
|
}
|
|
|
|
+ if (isEnabled(MetricAttribute.MEAN)) {
|
|
|
|
+ info.setMean(convertDuration(snapshot.getMean()));
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (isEnabled(MetricAttribute.P50)) {
|
|
|
|
+ info.setP50(convertDuration(snapshot.getMedian()));
|
|
|
|
+ }
|
|
|
|
+ if (isEnabled(MetricAttribute.P75)) {
|
|
|
|
+ info.setP75(convertDuration(snapshot.get75thPercentile()));
|
|
|
|
+ }
|
|
|
|
+ if (isEnabled(MetricAttribute.P95)) {
|
|
|
|
+ info.setP95(convertDuration(snapshot.get95thPercentile()));
|
|
|
|
+ }
|
|
|
|
+ if (isEnabled(MetricAttribute.P98)) {
|
|
|
|
+ info.setP98(convertDuration(snapshot.get98thPercentile()));
|
|
|
|
+ }
|
|
|
|
+ if (isEnabled(MetricAttribute.P99)) {
|
|
|
|
+ info.setP99(convertDuration(snapshot.get99thPercentile()));
|
|
|
|
+ }
|
|
|
|
+ if (isEnabled(MetricAttribute.P999)) {
|
|
|
|
+ info.setP999(convertDuration(snapshot.get999thPercentile()));
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return info;
|
|
|
|
+ }
|
|
|
|
|
|
- output.println(status);
|
|
|
|
|
|
+ private boolean isEnabled(MetricAttribute type) {
|
|
|
|
+ return !getDisabledMetricAttributes().contains(type);
|
|
}
|
|
}
|
|
}
|
|
}
|