WANG 6 年 前
コミット
a15e856d7c

+ 85 - 150
src/main/java/cn/com/qmth/examcloud/web/actuator/DataReportor.java

@@ -4,6 +4,7 @@ import java.io.PrintStream;
 import java.text.DateFormat;
 import java.util.Collections;
 import java.util.Date;
+import java.util.List;
 import java.util.Locale;
 import java.util.Map;
 import java.util.Set;
@@ -23,6 +24,7 @@ import com.codahale.metrics.MetricRegistry;
 import com.codahale.metrics.ScheduledReporter;
 import com.codahale.metrics.Snapshot;
 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 DateFormat dateFormat;
@@ -242,8 +238,6 @@ public class DataReportor extends ScheduledReporter {
 			Set<MetricAttribute> disabledMetricAttributes) {
 		super(registry, "console-reporter", filter, rateUnit, durationUnit, executor,
 				shutdownExecutorOnStop, disabledMetricAttributes);
-		this.output = output;
-		this.locale = locale;
 		this.clock = clock;
 		this.dateFormat = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.MEDIUM,
 				locale);
@@ -255,172 +249,113 @@ public class DataReportor extends ScheduledReporter {
 	public void report(SortedMap<String, Gauge> gauges, SortedMap<String, Counter> counters,
 			SortedMap<String, Histogram> histograms, SortedMap<String, Meter> meters,
 			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()) {
-			printWithBanner("-- Meters", '-');
 			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()) {
-			printWithBanner("-- Timers", '-');
 			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();
-		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);
 	}
 }

+ 25 - 5
src/main/java/cn/com/qmth/examcloud/web/actuator/MeterInfo.java

@@ -2,7 +2,9 @@ package cn.com.qmth.examcloud.web.actuator;
 
 public class MeterInfo {
 
-	private Long total;
+	private String key;
+
+	private Long count;
 
 	private double meanRate;
 
@@ -12,12 +14,22 @@ public class MeterInfo {
 
 	private double fifteenMinuteRate;
 
-	public Long getTotal() {
-		return total;
+	private String rateUnit;
+
+	public String getKey() {
+		return key;
+	}
+
+	public void setKey(String key) {
+		this.key = key;
+	}
+
+	public Long getCount() {
+		return count;
 	}
 
-	public void setTotal(Long total) {
-		this.total = total;
+	public void setCount(Long count) {
+		this.count = count;
 	}
 
 	public double getMeanRate() {
@@ -52,4 +64,12 @@ public class MeterInfo {
 		this.fifteenMinuteRate = fifteenMinuteRate;
 	}
 
+	public String getRateUnit() {
+		return rateUnit;
+	}
+
+	public void setRateUnit(String rateUnit) {
+		this.rateUnit = rateUnit;
+	}
+
 }

+ 38 - 0
src/main/java/cn/com/qmth/examcloud/web/actuator/ReportInfo.java

@@ -0,0 +1,38 @@
+package cn.com.qmth.examcloud.web.actuator;
+
+import java.util.Date;
+import java.util.List;
+
+public class ReportInfo {
+
+	private List<MeterInfo> meterInfoList;
+
+	private List<TimerInfo> timerInfoList;
+
+	private Date dateTime;
+
+	public List<MeterInfo> getMeterInfoList() {
+		return meterInfoList;
+	}
+
+	public void setMeterInfoList(List<MeterInfo> meterInfoList) {
+		this.meterInfoList = meterInfoList;
+	}
+
+	public List<TimerInfo> getTimerInfoList() {
+		return timerInfoList;
+	}
+
+	public void setTimerInfoList(List<TimerInfo> timerInfoList) {
+		this.timerInfoList = timerInfoList;
+	}
+
+	public Date getDateTime() {
+		return dateTime;
+	}
+
+	public void setDateTime(Date dateTime) {
+		this.dateTime = dateTime;
+	}
+
+}

+ 30 - 0
src/main/java/cn/com/qmth/examcloud/web/actuator/TimerInfo.java

@@ -8,6 +8,10 @@ public class TimerInfo extends MeterInfo {
 
 	private double mean;
 
+	private String durationUnit;
+
+	private double p50;
+
 	private double p75;
 
 	private double p95;
@@ -16,6 +20,8 @@ public class TimerInfo extends MeterInfo {
 
 	private double p99;
 
+	private double p999;
+
 	public double getMin() {
 		return min;
 	}
@@ -40,6 +46,22 @@ public class TimerInfo extends MeterInfo {
 		this.mean = mean;
 	}
 
+	public String getDurationUnit() {
+		return durationUnit;
+	}
+
+	public void setDurationUnit(String durationUnit) {
+		this.durationUnit = durationUnit;
+	}
+
+	public double getP50() {
+		return p50;
+	}
+
+	public void setP50(double p50) {
+		this.p50 = p50;
+	}
+
 	public double getP75() {
 		return p75;
 	}
@@ -72,4 +94,12 @@ public class TimerInfo extends MeterInfo {
 		this.p99 = p99;
 	}
 
+	public double getP999() {
+		return p999;
+	}
+
+	public void setP999(double p999) {
+		this.p999 = p999;
+	}
+
 }