Search in sources :

Example 6 with MetricsTag

use of org.apache.hadoop.metrics2.MetricsTag in project hadoop by apache.

the class GraphiteSink method putMetrics.

@Override
public void putMetrics(MetricsRecord record) {
    StringBuilder lines = new StringBuilder();
    StringBuilder metricsPathPrefix = new StringBuilder();
    // Configure the hierarchical place to display the graph.
    metricsPathPrefix.append(metricsPrefix).append(".").append(record.context()).append(".").append(record.name());
    for (MetricsTag tag : record.tags()) {
        if (tag.value() != null) {
            metricsPathPrefix.append(".");
            metricsPathPrefix.append(tag.name());
            metricsPathPrefix.append("=");
            metricsPathPrefix.append(tag.value());
        }
    }
    // The record timestamp is in milliseconds while Graphite expects an epoc time in seconds.
    long timestamp = record.timestamp() / 1000L;
    // Collect datapoints.
    for (AbstractMetric metric : record.metrics()) {
        lines.append(metricsPathPrefix.toString() + "." + metric.name().replace(' ', '.')).append(" ").append(metric.value()).append(" ").append(timestamp).append("\n");
    }
    try {
        graphite.write(lines.toString());
    } catch (Exception e) {
        LOG.warn("Error sending metrics to Graphite", e);
        try {
            graphite.close();
        } catch (Exception e1) {
            throw new MetricsException("Error closing connection to Graphite", e1);
        }
    }
}
Also used : AbstractMetric(org.apache.hadoop.metrics2.AbstractMetric) MetricsException(org.apache.hadoop.metrics2.MetricsException) MetricsTag(org.apache.hadoop.metrics2.MetricsTag) IOException(java.io.IOException) MetricsException(org.apache.hadoop.metrics2.MetricsException)

Example 7 with MetricsTag

use of org.apache.hadoop.metrics2.MetricsTag in project hadoop by apache.

the class MBeanInfoBuilder method get.

MBeanInfo get() {
    curRecNo = 0;
    for (MetricsRecordImpl rec : recs) {
        for (MetricsTag t : rec.tags()) {
            attrs.add(newAttrInfo("tag." + t.name(), t.description(), "java.lang.String"));
        }
        for (AbstractMetric m : rec.metrics()) {
            m.visit(this);
        }
        ++curRecNo;
    }
    MetricsSystemImpl.LOG.debug(attrs);
    MBeanAttributeInfo[] attrsArray = new MBeanAttributeInfo[attrs.size()];
    return new MBeanInfo(name, description, attrs.toArray(attrsArray), null, null, // no ops/ctors/notifications
    null);
}
Also used : MBeanInfo(javax.management.MBeanInfo) AbstractMetric(org.apache.hadoop.metrics2.AbstractMetric) MetricsTag(org.apache.hadoop.metrics2.MetricsTag) MBeanAttributeInfo(javax.management.MBeanAttributeInfo)

Example 8 with MetricsTag

use of org.apache.hadoop.metrics2.MetricsTag in project hadoop by apache.

the class DataNodeMetricHelper method getMetrics.

/**
   * Get metrics helper provides Helper function for
   * metrics2 interface to act as a Metric source
   *
   * @param collector Metrics Collector that is passed in
   * @param beanClass The Class that currently impliments the metric functions
   * @param context A string that idenitifies the context
   *
   * @throws IOException
   */
public static void getMetrics(MetricsCollector collector, FSDatasetMBean beanClass, String context) throws IOException {
    if (beanClass == null) {
        throw new IOException("beanClass cannot be null");
    }
    String className = beanClass.getClass().getName();
    collector.addRecord(className).setContext(context).addGauge(Interns.info("Capacity", "Total storage capacity"), beanClass.getCapacity()).addGauge(Interns.info("DfsUsed", "Total bytes used by dfs datanode"), beanClass.getDfsUsed()).addGauge(Interns.info("Remaining", "Total bytes of free storage"), beanClass.getRemaining()).add(new MetricsTag(Interns.info("StorageInfo", "Storage ID"), beanClass.getStorageInfo())).addGauge(Interns.info("NumFailedVolumes", "Number of failed Volumes" + " in the data Node"), beanClass.getNumFailedVolumes()).addGauge(Interns.info("LastVolumeFailureDate", "Last Volume failure in" + " milliseconds from epoch"), beanClass.getLastVolumeFailureDate()).addGauge(Interns.info("EstimatedCapacityLostTotal", "Total capacity lost" + " due to volume failure"), beanClass.getEstimatedCapacityLostTotal()).addGauge(Interns.info("CacheUsed", "Datanode cache used in bytes"), beanClass.getCacheUsed()).addGauge(Interns.info("CacheCapacity", "Datanode cache capacity"), beanClass.getCacheCapacity()).addGauge(Interns.info("NumBlocksCached", "Datanode number" + " of blocks cached"), beanClass.getNumBlocksCached()).addGauge(Interns.info("NumBlocksFailedToCache", "Datanode number of " + "blocks failed to cache"), beanClass.getNumBlocksFailedToCache()).addGauge(Interns.info("NumBlocksFailedToUnCache", "Datanode number of" + " blocks failed in cache eviction"), beanClass.getNumBlocksFailedToUncache());
}
Also used : IOException(java.io.IOException) MetricsTag(org.apache.hadoop.metrics2.MetricsTag)

Example 9 with MetricsTag

use of org.apache.hadoop.metrics2.MetricsTag in project hadoop by apache.

the class TestMetricsSourceAdapter method testGetMetricsAndJmx.

@Test
public void testGetMetricsAndJmx() throws Exception {
    // create test source with a single metric counter of value 0
    TestSource source = new TestSource("test");
    MetricsSourceBuilder sb = MetricsAnnotations.newSourceBuilder(source);
    final MetricsSource s = sb.build();
    List<MetricsTag> injectedTags = new ArrayList<MetricsTag>();
    MetricsSourceAdapter sa = new MetricsSourceAdapter("test", "test", "test desc", s, injectedTags, null, null, 1, false);
    // all metrics are initially assumed to have changed
    MetricsCollectorImpl builder = new MetricsCollectorImpl();
    Iterable<MetricsRecordImpl> metricsRecords = sa.getMetrics(builder, true);
    // Validate getMetrics and JMX initial values
    MetricsRecordImpl metricsRecord = metricsRecords.iterator().next();
    assertEquals(0L, metricsRecord.metrics().iterator().next().value().longValue());
    // skip JMX cache TTL
    Thread.sleep(100);
    assertEquals(0L, (Number) sa.getAttribute("C1"));
    // change metric value
    source.incrementCnt();
    // validate getMetrics and JMX
    builder = new MetricsCollectorImpl();
    metricsRecords = sa.getMetrics(builder, true);
    metricsRecord = metricsRecords.iterator().next();
    assertTrue(metricsRecord.metrics().iterator().hasNext());
    // skip JMX cache TTL
    Thread.sleep(100);
    assertEquals(1L, (Number) sa.getAttribute("C1"));
}
Also used : MetricsSource(org.apache.hadoop.metrics2.MetricsSource) MetricsSourceBuilder(org.apache.hadoop.metrics2.lib.MetricsSourceBuilder) ArrayList(java.util.ArrayList) MetricsTag(org.apache.hadoop.metrics2.MetricsTag) Test(org.junit.Test)

Example 10 with MetricsTag

use of org.apache.hadoop.metrics2.MetricsTag in project hadoop by apache.

the class TestMetricsSystemImpl method checkMetricsRecords.

private void checkMetricsRecords(List<MetricsRecord> recs) {
    LOG.debug(recs);
    MetricsRecord r = recs.get(0);
    assertEquals("name", "s1rec", r.name());
    assertEquals("tags", new MetricsTag[] { tag(MsInfo.Context, "test"), tag(MsInfo.Hostname, hostname) }, r.tags());
    assertEquals("metrics", MetricsLists.builder("").addCounter(info("C1", "C1 desc"), 1L).addGauge(info("G1", "G1 desc"), 2L).addCounter(info("S1NumOps", "Number of ops for s1"), 1L).addGauge(info("S1AvgTime", "Average time for s1"), 0.0).metrics(), r.metrics());
    r = recs.get(1);
    assertTrue("NumActiveSinks should be 3", Iterables.contains(r.metrics(), new MetricGaugeInt(MsInfo.NumActiveSinks, 3)));
}
Also used : MetricsRecord(org.apache.hadoop.metrics2.MetricsRecord)

Aggregations

MetricsTag (org.apache.hadoop.metrics2.MetricsTag)30 AbstractMetric (org.apache.hadoop.metrics2.AbstractMetric)19 MetricsRecord (org.apache.hadoop.metrics2.MetricsRecord)13 Test (org.junit.Test)13 ArrayList (java.util.ArrayList)11 IOException (java.io.IOException)7 HashSet (java.util.HashSet)7 GraphiteSink (org.apache.hadoop.metrics2.sink.GraphiteSink)4 MetricsException (org.apache.hadoop.metrics2.MetricsException)3 MetricsSourceBuilder (org.apache.hadoop.metrics2.lib.MetricsSourceBuilder)3 Matchers.anyString (org.mockito.Matchers.anyString)3 DatagramPacket (java.net.DatagramPacket)2 DatagramSocket (java.net.DatagramSocket)2 MBeanAttributeInfo (javax.management.MBeanAttributeInfo)2 MBeanInfo (javax.management.MBeanInfo)2 MetricsSource (org.apache.hadoop.metrics2.MetricsSource)2 StatsDSink (org.apache.hadoop.metrics2.sink.StatsDSink)2 StatsD (org.apache.hadoop.metrics2.sink.StatsDSink.StatsD)2 ProducerRecord (org.apache.kafka.clients.producer.ProducerRecord)2 RecordMetadata (org.apache.kafka.clients.producer.RecordMetadata)2