Search in sources :

Example 76 with Metrics

use of org.apache.hadoop.metrics2.annotation.Metrics in project hadoop by apache.

the class TestFileSink method testFileSink.

@Test(timeout = 6000)
public void testFileSink() throws IOException {
    outFile = getTestTempFile("test-file-sink-", ".out");
    final String outPath = outFile.getAbsolutePath();
    // NB: specify large period to avoid multiple metrics snapshotting: 
    new ConfigBuilder().add("*.period", 10000).add("test.sink.mysink0.class", FileSink.class.getName()).add("test.sink.mysink0.filename", outPath).add("test.sink.mysink0.context", "test1").save(TestMetricsConfig.getTestFilename("hadoop-metrics2-test"));
    MetricsSystemImpl ms = new MetricsSystemImpl("test");
    ms.start();
    final MyMetrics1 mm1 = new MyMetrics1().registerWith(ms);
    new MyMetrics2().registerWith(ms);
    mm1.testMetric1.incr();
    mm1.testMetric2.incr(2);
    // publish the metrics
    ms.publishMetricsNow();
    ms.stop();
    ms.shutdown();
    InputStream is = null;
    ByteArrayOutputStream baos = null;
    String outFileContent = null;
    try {
        is = new FileInputStream(outFile);
        baos = new ByteArrayOutputStream((int) outFile.length());
        IOUtils.copyBytes(is, baos, 1024, true);
        outFileContent = new String(baos.toByteArray(), "UTF-8");
    } finally {
        IOUtils.cleanup(null, baos, is);
    }
    // Check the out file content. Should be something like the following:
    //1360244820087 test1.testRecord1: Context=test1, testTag1=testTagValue1, testTag2=testTagValue2, Hostname=myhost, testMetric1=1, testMetric2=2
    //1360244820089 test1.testRecord2: Context=test1, testTag22=testTagValue22, Hostname=myhost
    // Note that in the below expression we allow tags and metrics to go in arbitrary order.  
    Pattern expectedContentPattern = Pattern.compile(// line #1:
    "^\\d+\\s+test1.testRecord1:\\s+Context=test1,\\s+" + "(testTag1=testTagValue1,\\s+testTag2=testTagValue2|testTag2=testTagValue2,\\s+testTag1=testTagValue1)," + "\\s+Hostname=.*,\\s+(testMetric1=1,\\s+testMetric2=2|testMetric2=2,\\s+testMetric1=1)" + // line #2:
    "$[\\n\\r]*^\\d+\\s+test1.testRecord2:\\s+Context=test1," + "\\s+testTag22=testTagValue22,\\s+Hostname=.*$[\\n\\r]*", Pattern.MULTILINE);
    assertTrue(expectedContentPattern.matcher(outFileContent).matches());
}
Also used : Pattern(java.util.regex.Pattern) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ConfigBuilder(org.apache.hadoop.metrics2.impl.ConfigBuilder) ByteArrayOutputStream(java.io.ByteArrayOutputStream) MetricsSystemImpl(org.apache.hadoop.metrics2.impl.MetricsSystemImpl) FileInputStream(java.io.FileInputStream) Test(org.junit.Test)

Example 77 with Metrics

use of org.apache.hadoop.metrics2.annotation.Metrics in project hadoop by apache.

the class TestMetricsCache method testUpdate.

@SuppressWarnings("deprecation")
@Test
public void testUpdate() {
    MetricsCache cache = new MetricsCache();
    MetricsRecord mr = makeRecord("r", Arrays.asList(makeTag("t", "tv")), Arrays.asList(makeMetric("m", 0), makeMetric("m1", 1)));
    MetricsCache.Record cr = cache.update(mr);
    verify(mr).name();
    verify(mr).tags();
    verify(mr).metrics();
    assertEquals("same record size", cr.metrics().size(), ((Collection<AbstractMetric>) mr.metrics()).size());
    assertEquals("same metric value", 0, cr.getMetric("m"));
    MetricsRecord mr2 = makeRecord("r", Arrays.asList(makeTag("t", "tv")), Arrays.asList(makeMetric("m", 2), makeMetric("m2", 42)));
    cr = cache.update(mr2);
    assertEquals("contains 3 metric", 3, cr.metrics().size());
    checkMetricValue("updated metric value", cr, "m", 2);
    checkMetricValue("old metric value", cr, "m1", 1);
    checkMetricValue("new metric value", cr, "m2", 42);
    MetricsRecord mr3 = makeRecord("r", // different tag value
    Arrays.asList(makeTag("t", "tv3")), Arrays.asList(makeMetric("m3", 3)));
    // should get a new record
    cr = cache.update(mr3);
    assertEquals("contains 1 metric", 1, cr.metrics().size());
    checkMetricValue("updated metric value", cr, "m3", 3);
    // tags cache should be empty so far
    assertEquals("no tags", 0, cr.tags().size());
    // until now
    cr = cache.update(mr3, true);
    assertEquals("Got 1 tag", 1, cr.tags().size());
    assertEquals("Tag value", "tv3", cr.getTag("t"));
    checkMetricValue("Metric value", cr, "m3", 3);
}
Also used : MetricsRecord(org.apache.hadoop.metrics2.MetricsRecord) AbstractMetric(org.apache.hadoop.metrics2.AbstractMetric) Test(org.junit.Test)

Example 78 with Metrics

use of org.apache.hadoop.metrics2.annotation.Metrics in project hadoop by apache.

the class TestMetricsCache method testNullTag.

/**
   * Make sure metrics tag has a sane hashCode impl
   */
@Test
public void testNullTag() {
    MetricsCache cache = new MetricsCache();
    MetricsRecord mr = makeRecord("r", Arrays.asList(makeTag("t", null)), Arrays.asList(makeMetric("m", 0), makeMetric("m1", 1)));
    MetricsCache.Record cr = cache.update(mr);
    assertTrue("t value should be null", null == cr.getTag("t"));
}
Also used : MetricsRecord(org.apache.hadoop.metrics2.MetricsRecord) Test(org.junit.Test)

Example 79 with Metrics

use of org.apache.hadoop.metrics2.annotation.Metrics in project hadoop by apache.

the class TestMetricsCache method makeRecord.

private MetricsRecord makeRecord(String name, Collection<MetricsTag> tags, Collection<AbstractMetric> metrics) {
    MetricsRecord mr = mock(MetricsRecord.class);
    when(mr.name()).thenReturn(name);
    when(mr.tags()).thenReturn(tags);
    when(mr.metrics()).thenReturn(metrics);
    return mr;
}
Also used : MetricsRecord(org.apache.hadoop.metrics2.MetricsRecord)

Example 80 with Metrics

use of org.apache.hadoop.metrics2.annotation.Metrics in project hadoop by apache.

the class TestRollingFileSystemSinkWithLocal method testSilentFailedWrite.

/**
   * Test that writing fails silently when the directory is not writable.
   */
@Test
public void testSilentFailedWrite() {
    String path = methodDir.getAbsolutePath();
    MetricsSystem ms = initMetricsSystem(path, true, false);
    new MyMetrics1().registerWith(ms);
    methodDir.setWritable(false);
    MockSink.errored = false;
    try {
        // publish the metrics
        ms.publishMetricsNow();
        assertFalse("An exception was generated while writing metrics " + "when the target directory was not writable, even though the " + "sink is set to ignore errors", MockSink.errored);
        ms.stop();
        ms.shutdown();
    } finally {
        // Make sure the dir is writable again so we can delete it at the end
        methodDir.setWritable(true);
    }
}
Also used : MetricsSystem(org.apache.hadoop.metrics2.MetricsSystem) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)67 MetricsRecordBuilder (org.apache.hadoop.metrics2.MetricsRecordBuilder)30 MetricsRecord (org.apache.hadoop.metrics2.MetricsRecord)20 AbstractMetric (org.apache.hadoop.metrics2.AbstractMetric)19 MetricsSystem (org.apache.hadoop.metrics2.MetricsSystem)19 MetricsTag (org.apache.hadoop.metrics2.MetricsTag)18 MetricsSource (org.apache.hadoop.metrics2.MetricsSource)16 ArrayList (java.util.ArrayList)11 IOException (java.io.IOException)10 HashSet (java.util.HashSet)8 Path (org.apache.hadoop.fs.Path)8 MetricsException (org.apache.hadoop.metrics2.MetricsException)8 MetricsCollectorImpl (org.apache.hadoop.metrics2.impl.MetricsCollectorImpl)7 DefaultMetricsSystem (org.apache.hadoop.metrics2.lib.DefaultMetricsSystem)7 Configuration (org.apache.hadoop.conf.Configuration)5 Map (java.util.Map)4 FileSystem (org.apache.hadoop.fs.FileSystem)4 MetricsSink (org.apache.hadoop.metrics2.MetricsSink)4 MetricsSystemImpl (org.apache.hadoop.metrics2.impl.MetricsSystemImpl)4 GraphiteSink (org.apache.hadoop.metrics2.sink.GraphiteSink)4