Example usage for org.apache.hadoop.metrics2.lib Interns info

List of usage examples for org.apache.hadoop.metrics2.lib Interns info

Introduction

In this page you can find the example usage for org.apache.hadoop.metrics2.lib Interns info.

Prototype

public static MetricsInfo info(String name, String description) 

Source Link

Document

Get a metric info object.

Usage

From source file:com.github.joshelser.dropwizard.metrics.hadoop.HadoopMetrics2Reporter.java

License:Apache License

private HadoopMetrics2Reporter(MetricRegistry registry, TimeUnit rateUnit, TimeUnit durationUnit,
        MetricFilter filter, MetricsSystem metrics2System, String jmxContext, String description,
        String recordName, String context) {
    super(registry, "hadoop-metrics2-reporter", filter, rateUnit, durationUnit);
    this.metrics2Registry = new MetricsRegistry(Interns.info(jmxContext, description));
    this.metrics2System = metrics2System;
    this.recordName = recordName;
    this.context = context;

    // These could really be Collection.emptyMap(), but this makes testing a bit easier.
    this.dropwizardGauges = EMPTY_GAUGE_MAP;
    this.dropwizardCounters = EMPTY_COUNTER_MAP;
    this.dropwizardHistograms = EMPTY_HISTOGRAM_MAP;
    this.dropwizardMeters = EMPTY_METER_MAP;
    this.dropwizardTimers = EMPTY_TIMER_MAP;

    // Register this source with the Metrics2 system.
    // Make sure this is the last thing done as getMetrics() can be called at any time after.
    this.metrics2System.register(Objects.requireNonNull(jmxContext), Objects.requireNonNull(description), this);
}

From source file:com.github.joshelser.dropwizard.metrics.hadoop.HadoopMetrics2Reporter.java

License:Apache License

/**
 * Consumes the current metrics collected by dropwizard and adds them to the {@code builder}.
 *
 * @param builder A record builder/*from w w w.  j a va 2 s .  c o  m*/
 */
void snapshotAllMetrics(MetricsRecordBuilder builder) {
    try {
        // Pass through the gauges
        @SuppressWarnings("rawtypes")
        Iterator<Entry<String, Gauge>> gaugeIterator = dropwizardGauges.entrySet().iterator();
        while (gaugeIterator.hasNext()) {
            @SuppressWarnings("rawtypes")
            Entry<String, Gauge> gauge = gaugeIterator.next();
            final MetricsInfo info = Interns.info(gauge.getKey(), EMPTY_STRING);
            final Object o = gauge.getValue().getValue();

            // Figure out which gauge types metrics2 supports and call the right method
            if (o instanceof Integer) {
                builder.addGauge(info, (int) o);
            } else if (o instanceof Long) {
                builder.addGauge(info, (long) o);
            } else if (o instanceof Float) {
                builder.addGauge(info, (float) o);
            } else if (o instanceof Double) {
                builder.addGauge(info, (double) o);
            } else {
                LOG.trace("Ignoring Gauge ({}) with unhandled type: {}", gauge.getKey(), o.getClass());
            }
        }
    } finally {
        dropwizardGauges = EMPTY_GAUGE_MAP;
    }

    try {
        // Pass through the counters
        Iterator<Entry<String, Counter>> counterIterator = dropwizardCounters.entrySet().iterator();
        while (counterIterator.hasNext()) {
            Entry<String, Counter> counter = counterIterator.next();
            MetricsInfo info = Interns.info(counter.getKey(), EMPTY_STRING);
            builder.addCounter(info, counter.getValue().getCount());
        }
    } finally {
        dropwizardCounters = EMPTY_COUNTER_MAP;
    }

    try {
        // Pass through the histograms
        Iterator<Entry<String, Histogram>> histogramIterator = dropwizardHistograms.entrySet().iterator();
        while (histogramIterator.hasNext()) {
            final Entry<String, Histogram> entry = histogramIterator.next();
            final String name = entry.getKey();
            final Histogram histogram = entry.getValue();

            addSnapshot(builder, name, EMPTY_STRING, histogram.getSnapshot(), histogram.getCount());
        }
    } finally {
        dropwizardHistograms = EMPTY_HISTOGRAM_MAP;
    }

    try {
        // Pass through the meter values
        Iterator<Entry<String, Meter>> meterIterator = dropwizardMeters.entrySet().iterator();
        while (meterIterator.hasNext()) {
            final Entry<String, Meter> meterEntry = meterIterator.next();
            final String name = meterEntry.getKey();
            final Meter meter = meterEntry.getValue();

            addMeter(builder, name, EMPTY_STRING, meter.getCount(), meter.getMeanRate(),
                    meter.getOneMinuteRate(), meter.getFiveMinuteRate(), meter.getFifteenMinuteRate());
        }
    } finally {
        dropwizardMeters = EMPTY_METER_MAP;
    }

    try {
        // Pass through the timers (meter + histogram)
        Iterator<Entry<String, Timer>> timerIterator = dropwizardTimers.entrySet().iterator();
        while (timerIterator.hasNext()) {
            final Entry<String, Timer> timerEntry = timerIterator.next();
            final String name = timerEntry.getKey();
            final Timer timer = timerEntry.getValue();
            final Snapshot snapshot = timer.getSnapshot();

            // Add the meter info (mean rate and rate over time windows)
            addMeter(builder, name, EMPTY_STRING, timer.getCount(), timer.getMeanRate(),
                    timer.getOneMinuteRate(), timer.getFiveMinuteRate(), timer.getFifteenMinuteRate());

            // Count was already added via the meter
            addSnapshot(builder, name, EMPTY_STRING, snapshot);
        }
    } finally {
        dropwizardTimers = EMPTY_TIMER_MAP;
    }

    // Add in metadata about what the units the reported metrics are displayed using.
    builder.tag(RATE_UNIT_LABEL, getRateUnit());
    builder.tag(DURATION_UNIT_LABEL, getDurationUnit());
}

From source file:com.github.joshelser.dropwizard.metrics.hadoop.HadoopMetrics2Reporter.java

License:Apache License

/**
 * Add Dropwizard-Metrics rate information to a Hadoop-Metrics2 record builder, converting the
 * rates to the appropriate unit./*from  ww  w  .  ja va  2  s .  c om*/
 *
 * @param builder A Hadoop-Metrics2 record builder.
 * @param name A base name for this record.
 * @param desc A description for the record.
 * @param count The number of measured events.
 * @param meanRate The average measured rate.
 * @param oneMinuteRate The measured rate over the past minute.
 * @param fiveMinuteRate The measured rate over the past five minutes
 * @param fifteenMinuteRate The measured rate over the past fifteen minutes.
 */
private void addMeter(MetricsRecordBuilder builder, String name, String desc, long count, double meanRate,
        double oneMinuteRate, double fiveMinuteRate, double fifteenMinuteRate) {
    builder.addGauge(Interns.info(name + "_count", EMPTY_STRING), count);
    builder.addGauge(Interns.info(name + "_mean_rate", EMPTY_STRING), convertRate(meanRate));
    builder.addGauge(Interns.info(name + "_1min_rate", EMPTY_STRING), convertRate(oneMinuteRate));
    builder.addGauge(Interns.info(name + "_5min_rate", EMPTY_STRING), convertRate(fiveMinuteRate));
    builder.addGauge(Interns.info(name + "_15min_rate", EMPTY_STRING), convertRate(fifteenMinuteRate));
}

From source file:com.github.joshelser.dropwizard.metrics.hadoop.HadoopMetrics2Reporter.java

License:Apache License

/**
 * Add Dropwizard-Metrics value-distribution data to a Hadoop-Metrics2 record building, converting
 * the durations to the appropriate unit.
 *
 * @param builder A Hadoop-Metrics2 record builder.
 * @param name A base name for this record.
 * @param desc A description for this record.
 * @param snapshot The distribution of measured values.
 * @param count The number of values which were measured.
 *//*w w w  .jav  a 2 s.  com*/
private void addSnapshot(MetricsRecordBuilder builder, String name, String desc, Snapshot snapshot,
        long count) {
    builder.addGauge(Interns.info(name + "_count", desc), count);
    addSnapshot(builder, name, desc, snapshot);
}

From source file:com.github.joshelser.dropwizard.metrics.hadoop.HadoopMetrics2Reporter.java

License:Apache License

/**
 * Add Dropwizard-Metrics value-distribution data to a Hadoop-Metrics2 record building, converting
 * the durations to the appropriate unit.
 *
 * @param builder A Hadoop-Metrics2 record builder.
 * @param name A base name for this record.
 * @param desc A description for this record.
 * @param snapshot The distribution of measured values.
 *///from  w  ww.jav  a2  s  . c  o  m
private void addSnapshot(MetricsRecordBuilder builder, String name, String desc, Snapshot snapshot) {
    builder.addGauge(Interns.info(name + "_mean", desc), convertDuration(snapshot.getMean()));
    builder.addGauge(Interns.info(name + "_min", desc), convertDuration(snapshot.getMin()));
    builder.addGauge(Interns.info(name + "_max", desc), convertDuration(snapshot.getMax()));
    builder.addGauge(Interns.info(name + "_median", desc), convertDuration(snapshot.getMedian()));
    builder.addGauge(Interns.info(name + "_stddev", desc), convertDuration(snapshot.getStdDev()));

    builder.addGauge(Interns.info(name + "_75thpercentile", desc),
            convertDuration(snapshot.get75thPercentile()));
    builder.addGauge(Interns.info(name + "_95thpercentile", desc),
            convertDuration(snapshot.get95thPercentile()));
    builder.addGauge(Interns.info(name + "_98thpercentile", desc),
            convertDuration(snapshot.get98thPercentile()));
    builder.addGauge(Interns.info(name + "_99thpercentile", desc),
            convertDuration(snapshot.get99thPercentile()));
    builder.addGauge(Interns.info(name + "_999thpercentile", desc),
            convertDuration(snapshot.get999thPercentile()));
}

From source file:com.github.joshelser.dropwizard.metrics.hadoop.HadoopMetrics2ReporterTest.java

License:Apache License

@Test
public void testGaugeReporting() {
    final AtomicLong gaugeValue = new AtomicLong(0L);
    @SuppressWarnings("rawtypes")
    final Gauge gauge = new Gauge<Long>() {
        @Override/*from  www . j a va2  s. c  o m*/
        public Long getValue() {
            return gaugeValue.get();
        }
    };

    @SuppressWarnings("rawtypes")
    TreeMap<String, Gauge> gauges = new TreeMap<>();
    gauges.put("my_gauge", gauge);
    // Add the metrics objects to the internal "queues" by hand
    metrics2Reporter.setDropwizardGauges(gauges);

    // Set some values
    gaugeValue.set(5L);

    MetricsCollector collector = mock(MetricsCollector.class);
    MetricsRecordBuilder recordBuilder = mock(MetricsRecordBuilder.class);

    Mockito.when(collector.addRecord(recordName)).thenReturn(recordBuilder);

    // Make sure a value of 5 gets reported
    metrics2Reporter.getMetrics(collector, true);

    verify(recordBuilder).addGauge(Interns.info("my_gauge", ""), gaugeValue.get());
    verifyRecordBuilderUnits(recordBuilder);

    // Should not be the same instance we gave before. Our map should have gotten swapped out.
    assertTrue("Should not be the same map instance after collection",
            gauges != metrics2Reporter.getDropwizardGauges());
}

From source file:com.github.joshelser.dropwizard.metrics.hadoop.HadoopMetrics2ReporterTest.java

License:Apache License

@Test
public void testCounterReporting() {
    final Counter counter = new Counter();

    TreeMap<String, Counter> counters = new TreeMap<>();
    counters.put("my_counter", counter);
    // Add the metrics objects to the internal "queues" by hand
    metrics2Reporter.setDropwizardCounters(counters);

    // Set some values
    counter.inc(5L);/*  w ww.ja  v a 2  s  .co  m*/

    MetricsCollector collector = mock(MetricsCollector.class);
    MetricsRecordBuilder recordBuilder = mock(MetricsRecordBuilder.class);

    Mockito.when(collector.addRecord(recordName)).thenReturn(recordBuilder);

    metrics2Reporter.getMetrics(collector, true);

    verify(recordBuilder).addCounter(Interns.info("my_counter", ""), 5L);
    verifyRecordBuilderUnits(recordBuilder);

    // Should not be the same instance we gave before. Our map should have gotten swapped out.
    assertTrue("Should not be the same map instance after collection",
            counters != metrics2Reporter.getDropwizardCounters());
}

From source file:com.github.joshelser.dropwizard.metrics.hadoop.HadoopMetrics2ReporterTest.java

License:Apache License

@Test
public void testHistogramReporting() {
    final String metricName = "my_histogram";
    final Histogram histogram = mock(Histogram.class);
    final Snapshot snapshot = mock(Snapshot.class);

    long count = 10L;
    double percentile75 = 75;
    double percentile95 = 95;
    double percentile98 = 98;
    double percentile99 = 99;
    double percentile999 = 999;
    double median = 50;
    double mean = 60;
    long min = 1L;
    long max = 100L;
    double stddev = 10;

    when(snapshot.get75thPercentile()).thenReturn(percentile75);
    when(snapshot.get95thPercentile()).thenReturn(percentile95);
    when(snapshot.get98thPercentile()).thenReturn(percentile98);
    when(snapshot.get99thPercentile()).thenReturn(percentile99);
    when(snapshot.get999thPercentile()).thenReturn(percentile999);
    when(snapshot.getMedian()).thenReturn(median);
    when(snapshot.getMean()).thenReturn(mean);
    when(snapshot.getMin()).thenReturn(min);
    when(snapshot.getMax()).thenReturn(max);
    when(snapshot.getStdDev()).thenReturn(stddev);

    when(histogram.getCount()).thenReturn(count);
    when(histogram.getSnapshot()).thenReturn(snapshot);

    MetricsCollector collector = mock(MetricsCollector.class);
    MetricsRecordBuilder recordBuilder = mock(MetricsRecordBuilder.class);

    Mockito.when(collector.addRecord(recordName)).thenReturn(recordBuilder);

    TreeMap<String, Histogram> histograms = new TreeMap<>();
    histograms.put(metricName, histogram);
    // Add the metrics objects to the internal "queues" by hand
    metrics2Reporter.setDropwizardHistograms(histograms);

    metrics2Reporter.getMetrics(collector, true);

    verify(recordBuilder).addGauge(Interns.info(metricName + "_max", ""),
            metrics2Reporter.convertDuration(max));
    verify(recordBuilder).addGauge(Interns.info(metricName + "_min", ""),
            metrics2Reporter.convertDuration(min));
    verify(recordBuilder).addGauge(Interns.info(metricName + "_median", ""),
            metrics2Reporter.convertDuration(median));
    verify(recordBuilder).addGauge(Interns.info(metricName + "_count", ""), count);
    verify(recordBuilder).addGauge(Interns.info(metricName + "_stddev", ""),
            metrics2Reporter.convertDuration(stddev));

    verify(recordBuilder).addGauge(Interns.info(metricName + "_75thpercentile", ""),
            metrics2Reporter.convertDuration(percentile75));
    verify(recordBuilder).addGauge(Interns.info(metricName + "_95thpercentile", ""),
            metrics2Reporter.convertDuration(percentile95));
    verify(recordBuilder).addGauge(Interns.info(metricName + "_98thpercentile", ""),
            metrics2Reporter.convertDuration(percentile98));
    verify(recordBuilder).addGauge(Interns.info(metricName + "_99thpercentile", ""),
            metrics2Reporter.convertDuration(percentile99));
    verify(recordBuilder).addGauge(Interns.info(metricName + "_999thpercentile", ""),
            metrics2Reporter.convertDuration(percentile999));

    verifyRecordBuilderUnits(recordBuilder);

    // Should not be the same instance we gave before. Our map should have gotten swapped out.
    assertTrue("Should not be the same map instance after collection",
            histograms != metrics2Reporter.getDropwizardHistograms());
}

From source file:com.github.joshelser.dropwizard.metrics.hadoop.HadoopMetrics2ReporterTest.java

License:Apache License

@Test
public void testTimerReporting() {
    final String metricName = "my_timer";
    final Timer timer = mock(Timer.class);
    final Snapshot snapshot = mock(Snapshot.class);

    TreeMap<String, Timer> timers = new TreeMap<>();
    timers.put(metricName, timer);//  w  ww . ja v  a  2s . c o m
    // Add the metrics objects to the internal "queues" by hand
    metrics2Reporter.setDropwizardTimers(timers);

    long count = 10L;
    double meanRate = 1.0;
    double oneMinRate = 2.0;
    double fiveMinRate = 5.0;
    double fifteenMinRate = 10.0;

    when(timer.getCount()).thenReturn(count);
    when(timer.getMeanRate()).thenReturn(meanRate);
    when(timer.getOneMinuteRate()).thenReturn(oneMinRate);
    when(timer.getFiveMinuteRate()).thenReturn(fiveMinRate);
    when(timer.getFifteenMinuteRate()).thenReturn(fifteenMinRate);
    when(timer.getSnapshot()).thenReturn(snapshot);

    double percentile75 = 75;
    double percentile95 = 95;
    double percentile98 = 98;
    double percentile99 = 99;
    double percentile999 = 999;
    double median = 50;
    double mean = 60;
    long min = 1L;
    long max = 100L;
    double stddev = 10;

    when(snapshot.get75thPercentile()).thenReturn(percentile75);
    when(snapshot.get95thPercentile()).thenReturn(percentile95);
    when(snapshot.get98thPercentile()).thenReturn(percentile98);
    when(snapshot.get99thPercentile()).thenReturn(percentile99);
    when(snapshot.get999thPercentile()).thenReturn(percentile999);
    when(snapshot.getMedian()).thenReturn(median);
    when(snapshot.getMean()).thenReturn(mean);
    when(snapshot.getMin()).thenReturn(min);
    when(snapshot.getMax()).thenReturn(max);
    when(snapshot.getStdDev()).thenReturn(stddev);

    MetricsCollector collector = mock(MetricsCollector.class);
    MetricsRecordBuilder recordBuilder = mock(MetricsRecordBuilder.class);

    Mockito.when(collector.addRecord(recordName)).thenReturn(recordBuilder);

    metrics2Reporter.getMetrics(collector, true);

    // We get the count from the meter and histogram
    verify(recordBuilder).addGauge(Interns.info(metricName + "_count", ""), count);

    // Verify the rates
    verify(recordBuilder).addGauge(Interns.info(metricName + "_mean_rate", ""),
            metrics2Reporter.convertRate(meanRate));
    verify(recordBuilder).addGauge(Interns.info(metricName + "_1min_rate", ""),
            metrics2Reporter.convertRate(oneMinRate));
    verify(recordBuilder).addGauge(Interns.info(metricName + "_5min_rate", ""),
            metrics2Reporter.convertRate(fiveMinRate));
    verify(recordBuilder).addGauge(Interns.info(metricName + "_15min_rate", ""),
            metrics2Reporter.convertRate(fifteenMinRate));

    // Verify the histogram
    verify(recordBuilder).addGauge(Interns.info(metricName + "_max", ""),
            metrics2Reporter.convertDuration(max));
    verify(recordBuilder).addGauge(Interns.info(metricName + "_min", ""),
            metrics2Reporter.convertDuration(min));
    verify(recordBuilder).addGauge(Interns.info(metricName + "_median", ""),
            metrics2Reporter.convertDuration(median));
    verify(recordBuilder).addGauge(Interns.info(metricName + "_stddev", ""),
            metrics2Reporter.convertDuration(stddev));

    verify(recordBuilder).addGauge(Interns.info(metricName + "_75thpercentile", ""),
            metrics2Reporter.convertDuration(percentile75));
    verify(recordBuilder).addGauge(Interns.info(metricName + "_95thpercentile", ""),
            metrics2Reporter.convertDuration(percentile95));
    verify(recordBuilder).addGauge(Interns.info(metricName + "_98thpercentile", ""),
            metrics2Reporter.convertDuration(percentile98));
    verify(recordBuilder).addGauge(Interns.info(metricName + "_99thpercentile", ""),
            metrics2Reporter.convertDuration(percentile99));
    verify(recordBuilder).addGauge(Interns.info(metricName + "_999thpercentile", ""),
            metrics2Reporter.convertDuration(percentile999));

    verifyRecordBuilderUnits(recordBuilder);

    // Should not be the same instance we gave before. Our map should have gotten swapped out.
    assertTrue("Should not be the same map instance after collection",
            timers != metrics2Reporter.getDropwizardTimers());
}

From source file:com.github.joshelser.dropwizard.metrics.hadoop.HadoopMetrics2ReporterTest.java

License:Apache License

@Test
public void testMeterReporting() {
    final String metricName = "my_meter";
    final Meter meter = mock(Meter.class);

    TreeMap<String, Meter> meters = new TreeMap<>();
    meters.put(metricName, meter);// w ww .  j a  va 2s. co  m
    // Add the metrics objects to the internal "queues" by hand
    metrics2Reporter.setDropwizardMeters(meters);

    // Set some values
    long count = 10L;
    double meanRate = 1.0;
    double oneMinRate = 2.0;
    double fiveMinRate = 5.0;
    double fifteenMinRate = 10.0;

    when(meter.getCount()).thenReturn(count);
    when(meter.getMeanRate()).thenReturn(meanRate);
    when(meter.getOneMinuteRate()).thenReturn(oneMinRate);
    when(meter.getFiveMinuteRate()).thenReturn(fiveMinRate);
    when(meter.getFifteenMinuteRate()).thenReturn(fifteenMinRate);

    MetricsCollector collector = mock(MetricsCollector.class);
    MetricsRecordBuilder recordBuilder = mock(MetricsRecordBuilder.class);

    Mockito.when(collector.addRecord(recordName)).thenReturn(recordBuilder);

    metrics2Reporter.getMetrics(collector, true);

    // Verify the rates
    verify(recordBuilder).addGauge(Interns.info(metricName + "_count", ""), count);
    verify(recordBuilder).addGauge(Interns.info(metricName + "_mean_rate", ""),
            metrics2Reporter.convertRate(meanRate));
    verify(recordBuilder).addGauge(Interns.info(metricName + "_1min_rate", ""),
            metrics2Reporter.convertRate(oneMinRate));
    verify(recordBuilder).addGauge(Interns.info(metricName + "_5min_rate", ""),
            metrics2Reporter.convertRate(fiveMinRate));
    verify(recordBuilder).addGauge(Interns.info(metricName + "_15min_rate", ""),
            metrics2Reporter.convertRate(fifteenMinRate));

    // Verify the units
    verifyRecordBuilderUnits(recordBuilder);

    // Should not be the same instance we gave before. Our map should have gotten swapped out.
    assertTrue("Should not be the same map instance after collection",
            meters != metrics2Reporter.getDropwizardMeters());
}