|
| 1 | +package com.yammer.metrics; |
| 2 | + |
| 3 | +import java.io.PrintStream; |
| 4 | +import java.text.DateFormat; |
| 5 | +import java.util.*; |
| 6 | + |
| 7 | +// TODO: 3/10/13 <coda> -- write tests |
| 8 | +// TODO: 3/10/13 <coda> -- write docs |
| 9 | + |
| 10 | +public class ConsoleReporter extends AbstractPollingReporter { |
| 11 | + private static final int CONSOLE_WIDTH = 80; |
| 12 | + |
| 13 | + private final PrintStream output; |
| 14 | + private final Locale locale; |
| 15 | + private final Clock clock; |
| 16 | + private final DateFormat dateFormat; |
| 17 | + |
| 18 | + public ConsoleReporter(MetricRegistry registry, |
| 19 | + PrintStream output, |
| 20 | + Locale locale, |
| 21 | + Clock clock, |
| 22 | + TimeZone timeZone) { |
| 23 | + super(registry, "console-reporter"); |
| 24 | + this.output = output; |
| 25 | + this.locale = locale; |
| 26 | + this.clock = clock; |
| 27 | + this.dateFormat = DateFormat.getDateTimeInstance(DateFormat.SHORT, |
| 28 | + DateFormat.MEDIUM, |
| 29 | + locale); |
| 30 | + dateFormat.setTimeZone(timeZone); |
| 31 | + } |
| 32 | + |
| 33 | + @Override |
| 34 | + public void report(SortedMap<String, Gauge> gauges, |
| 35 | + SortedMap<String, Counter> counters, |
| 36 | + SortedMap<String, Histogram> histograms, |
| 37 | + SortedMap<String, Meter> meters, |
| 38 | + SortedMap<String, Timer> timers) { |
| 39 | + final String dateTime = dateFormat.format(new Date(clock.getTime())); |
| 40 | + printWithBanner(dateTime, '='); |
| 41 | + output.println(); |
| 42 | + |
| 43 | + printWithBanner("-- Gauges", '-'); |
| 44 | + for (Map.Entry<String, Gauge> entry : gauges.entrySet()) { |
| 45 | + output.println(entry.getKey()); |
| 46 | + printGauge(entry); |
| 47 | + } |
| 48 | + output.println(); |
| 49 | + |
| 50 | + printWithBanner("-- Counters", '-'); |
| 51 | + for (Map.Entry<String, Counter> entry : counters.entrySet()) { |
| 52 | + output.println(entry.getKey()); |
| 53 | + printCounter(entry); |
| 54 | + } |
| 55 | + output.println(); |
| 56 | + |
| 57 | + printWithBanner("-- Histograms", '-'); |
| 58 | + for (Map.Entry<String, Histogram> entry : histograms.entrySet()) { |
| 59 | + output.println(entry.getKey()); |
| 60 | + printHistogram(entry.getValue()); |
| 61 | + } |
| 62 | + output.println(); |
| 63 | + |
| 64 | + printWithBanner("-- Meters", '-'); |
| 65 | + for (Map.Entry<String, Meter> entry : meters.entrySet()) { |
| 66 | + output.println(entry.getKey()); |
| 67 | + printMetered(entry.getValue()); |
| 68 | + } |
| 69 | + output.println(); |
| 70 | + |
| 71 | + printWithBanner("-- Timers", '-'); |
| 72 | + for (Map.Entry<String, Timer> entry : timers.entrySet()) { |
| 73 | + output.println(entry.getKey()); |
| 74 | + printTimer(entry.getValue()); |
| 75 | + } |
| 76 | + output.println(); |
| 77 | + |
| 78 | + output.println(); |
| 79 | + output.flush(); |
| 80 | + } |
| 81 | + |
| 82 | + private void printCounter(Map.Entry<String, Counter> entry) { |
| 83 | + output.printf(locale, " count = %d%n", entry.getValue().getCount()); |
| 84 | + } |
| 85 | + |
| 86 | + private void printGauge(Map.Entry<String, Gauge> entry) { |
| 87 | + output.printf(locale, " value = %s%n", entry.getValue().getValue()); |
| 88 | + } |
| 89 | + |
| 90 | + private void printHistogram(Histogram histogram) { |
| 91 | + output.printf(locale, " count = %d%n", histogram.getCount()); |
| 92 | + output.printf(locale, " min = %2.2f%n", histogram.getMin()); |
| 93 | + output.printf(locale, " max = %2.2f%n", histogram.getMax()); |
| 94 | + output.printf(locale, " mean = %2.2f%n", histogram.getMean()); |
| 95 | + output.printf(locale, " stddev = %2.2f%n", histogram.getStdDev()); |
| 96 | + printSnapshot(histogram.getSnapshot()); |
| 97 | + } |
| 98 | + |
| 99 | + private void printTimer(Timer timer) { |
| 100 | + final Snapshot snapshot = timer.getSnapshot(); |
| 101 | + printMetered(timer); |
| 102 | + output.printf(locale, " min = %2.2f%n", timer.getMin()); |
| 103 | + output.printf(locale, " max = %2.2f%n", timer.getMax()); |
| 104 | + output.printf(locale, " mean = %2.2f%n", timer.getMean()); |
| 105 | + output.printf(locale, " stddev = %2.2f%n", timer.getStdDev()); |
| 106 | + printSnapshot(snapshot); |
| 107 | + } |
| 108 | + |
| 109 | + private void printSnapshot(Snapshot snapshot) { |
| 110 | + output.printf(locale, " median = %2.2f%n", snapshot.getMedian()); |
| 111 | + output.printf(locale, " 75%% <= %2.2f%n", snapshot.get75thPercentile()); |
| 112 | + output.printf(locale, " 95%% <= %2.2f%n", snapshot.get95thPercentile()); |
| 113 | + output.printf(locale, " 98%% <= %2.2f%n", snapshot.get98thPercentile()); |
| 114 | + output.printf(locale, " 99%% <= %2.2f%n", snapshot.get99thPercentile()); |
| 115 | + output.printf(locale, " 99.9%% <= %2.2f%n", snapshot.get999thPercentile()); |
| 116 | + } |
| 117 | + |
| 118 | + private void printMetered(Metered timer) { |
| 119 | + output.printf(locale, " count = %d%n", timer.getCount()); |
| 120 | + output.printf(locale, " mean rate = %2.2f%n", timer.getMeanRate()); |
| 121 | + output.printf(locale, " 1-minute rate = %2.2f%n", timer.getOneMinuteRate()); |
| 122 | + output.printf(locale, " 5-minute rate = %2.2f%n", timer.getFiveMinuteRate()); |
| 123 | + output.printf(locale, " 15-minute rate = %2.2f%n", timer.getFifteenMinuteRate()); |
| 124 | + } |
| 125 | + |
| 126 | + private void printWithBanner(String s, char c) { |
| 127 | + output.print(s); |
| 128 | + output.print(' '); |
| 129 | + for (int i = 0; i < (CONSOLE_WIDTH - s.length() - 1); i++) { |
| 130 | + output.print(c); |
| 131 | + } |
| 132 | + output.println(); |
| 133 | + } |
| 134 | +} |
0 commit comments