Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.yammer.metrics.reporting;
  
  
 import java.util.Date;
A simple reporters which prints out application metrics to a java.io.PrintStream periodically.
 
 public class ConsoleReporter extends AbstractPollingReporter implements
                                                              MetricProcessor<PrintStream> {
     private static final int CONSOLE_WIDTH = 80;

    
Enables the console reporter for the default metrics registry, and causes it to print to STDOUT with the specified period.

Parameters:
period the period between successive outputs
unit the time unit of period
 
     public static void enable(long periodTimeUnit unit) {
         enable(Metrics.defaultRegistry(), periodunit);
     }

    
Enables the console reporter for the given metrics registry, and causes it to print to STDOUT with the specified period and unrestricted output.

Parameters:
metricsRegistry the metrics registry
period the period between successive outputs
unit the time unit of period
 
     public static void enable(MetricsRegistry metricsRegistrylong periodTimeUnit unit) {
         final ConsoleReporter reporter = new ConsoleReporter(metricsRegistry,
                                                              .,
                                                              .);
         reporter.start(periodunit);
     }
 
     private final PrintStream out;
     private final MetricPredicate predicate;
     private final Clock clock;
     private final TimeZone timeZone;
     private final Locale locale;

    
Creates a new ConsoleReporter for the default metrics registry, with unrestricted output.

Parameters:
out the java.io.PrintStream to which output will be written
 
     public ConsoleReporter(PrintStream out) {
         this(Metrics.defaultRegistry(), out.);
     }

    
Creates a new ConsoleReporter for a given metrics registry.

Parameters:
metricsRegistry the metrics registry
out the java.io.PrintStream to which output will be written
predicate the com.yammer.metrics.core.MetricPredicate used to determine whether a metric will be output
 
     public ConsoleReporter(MetricsRegistry metricsRegistryPrintStream outMetricPredicate predicate) {
         this(metricsRegistryoutpredicate, Clock.defaultClock(), TimeZone.getDefault());
     }

    
Creates a new ConsoleReporter for a given metrics registry.

Parameters:
metricsRegistry the metrics registry
out the java.io.PrintStream to which output will be written
predicate the com.yammer.metrics.core.MetricPredicate used to determine whether a metric will be output
clock the com.yammer.metrics.core.Clock used to print time
timeZone the java.util.TimeZone used to print time
 
     public ConsoleReporter(MetricsRegistry metricsRegistry,
                            PrintStream out,
                            MetricPredicate predicate,
                            Clock clock,
                            TimeZone timeZone) {
         this(metricsRegistryoutpredicateclocktimeZone, Locale.getDefault());
     }

    
Creates a new ConsoleReporter for a given metrics registry.

Parameters:
metricsRegistry the metrics registry
out the java.io.PrintStream to which output will be written
predicate the com.yammer.metrics.core.MetricPredicate used to determine whether a metric will be output
clock the com.yammer.metrics.core.Clock used to print time
timeZone the java.util.TimeZone used to print time
locale the java.util.Locale used to print values
    public ConsoleReporter(MetricsRegistry metricsRegistry,
                           PrintStream out,
                           MetricPredicate predicate,
                           Clock clock,
                           TimeZone timeZoneLocale locale) {
        super(metricsRegistry"console-reporter");
        this. = out;
        this. = predicate;
        this. = clock;
        this. = timeZone;
        this. = locale;
    }
    @Override
    public void run() {
        try {
            final DateFormat format = DateFormat.getDateTimeInstance(.,
                                                                     .,
                                                                     );
            format.setTimeZone();
            final String dateTime = format.format(new Date(.time()));
            .print(dateTime);
            .print(' ');
            for (int i = 0; i < ( - dateTime.length() - 1); i++) {
                .print('=');
            }
            .println();
            for (Entry<StringSortedMap<MetricNameMetric>> entry : getMetricsRegistry().groupedMetrics(
                    ).entrySet()) {
                .print(entry.getKey());
                .println(':');
                for (Entry<MetricNameMetricsubEntry : entry.getValue().entrySet()) {
                    .print("  ");
                    .print(subEntry.getKey().getName());
                    .println(':');
                    subEntry.getValue().processWith(thissubEntry.getKey(), );
                    .println();
                }
                .println();
            }
            .println();
            .flush();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    @Override
    public void processGauge(MetricName nameGauge<?> gaugePrintStream stream) {
        stream.printf("    value = %s\n"gauge.value());
    }
    @Override
    public void processCounter(MetricName nameCounter counterPrintStream stream) {
        stream.printf("    count = %d\n"counter.count());
    }
    @Override
    public void processMeter(MetricName nameMetered meterPrintStream stream) {
        final String unit = abbrev(meter.rateUnit());
        stream.printf("             count = %d\n"meter.count());
        stream.printf("         mean rate = %2.2f %s/%s\n",
                      meter.meanRate(),
                      meter.eventType(),
                      unit);
        stream.printf("     1-minute rate = %2.2f %s/%s\n",
                      meter.oneMinuteRate(),
                      meter.eventType(),
                      unit);
        stream.printf("     5-minute rate = %2.2f %s/%s\n",
                      meter.fiveMinuteRate(),
                      meter.eventType(),
                      unit);
        stream.printf("    15-minute rate = %2.2f %s/%s\n",
                      meter.fifteenMinuteRate(),
                      meter.eventType(),
                      unit);
    }
    @Override
    public void processHistogram(MetricName nameHistogram histogramPrintStream stream) {
        final Snapshot snapshot = histogram.getSnapshot();
        stream.printf("               min = %2.2f\n"histogram.min());
        stream.printf("               max = %2.2f\n"histogram.max());
        stream.printf("              mean = %2.2f\n"histogram.mean());
        stream.printf("            stddev = %2.2f\n"histogram.stdDev());
        stream.printf("            median = %2.2f\n"snapshot.getMedian());
        stream.printf("              75%% <= %2.2f\n"snapshot.get75thPercentile());
        stream.printf("              95%% <= %2.2f\n"snapshot.get95thPercentile());
        stream.printf("              98%% <= %2.2f\n"snapshot.get98thPercentile());
        stream.printf("              99%% <= %2.2f\n"snapshot.get99thPercentile());
        stream.printf("            99.9%% <= %2.2f\n"snapshot.get999thPercentile());
    }
    @Override
    public void processTimer(MetricName nameTimer timerPrintStream stream) {
        processMeter(nametimerstream);
        final String durationUnit = abbrev(timer.durationUnit());
        final Snapshot snapshot = timer.getSnapshot();
        stream.printf("               min = %2.2f%s\n"timer.min(), durationUnit);
        stream.printf("               max = %2.2f%s\n"timer.max(), durationUnit);
        stream.printf("              mean = %2.2f%s\n"timer.mean(), durationUnit);
        stream.printf("            stddev = %2.2f%s\n"timer.stdDev(), durationUnit);
        stream.printf("            median = %2.2f%s\n"snapshot.getMedian(), durationUnit);
        stream.printf("              75%% <= %2.2f%s\n"snapshot.get75thPercentile(), durationUnit);
        stream.printf("              95%% <= %2.2f%s\n"snapshot.get95thPercentile(), durationUnit);
        stream.printf("              98%% <= %2.2f%s\n"snapshot.get98thPercentile(), durationUnit);
        stream.printf("              99%% <= %2.2f%s\n"snapshot.get99thPercentile(), durationUnit);
        stream.printf("            99.9%% <= %2.2f%s\n"snapshot.get999thPercentile(), durationUnit);
    }
    private String abbrev(TimeUnit unit) {
        switch (unit) {
            case :
                return "ns";
            case :
                return "us";
            case :
                return "ms";
            case :
                return "s";
            case :
                return "m";
            case :
                return "h";
            case :
                return "d";
            default:
                throw new IllegalArgumentException("Unrecognized TimeUnit: " + unit);
        }
    }
New to GrepCode? Check out our FAQ X