Search in sources :

Example 16 with MetricsTag

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

the class TestPatternFilter method mockMetricsRecord.

/**
   * Creates a mock MetricsRecord with the given name and tags.
   * 
   * @param name String name
   * @param tags List<MetricsTag> tags
   * @return MetricsRecord newly created mock
   */
private static MetricsRecord mockMetricsRecord(String name, List<MetricsTag> tags) {
    MetricsRecord record = mock(MetricsRecord.class);
    when(record.name()).thenReturn(name);
    when(record.tags()).thenReturn(tags);
    return record;
}
Also used : MetricsRecord(org.apache.hadoop.metrics2.MetricsRecord)

Example 17 with MetricsTag

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

the class TestGangliaMetrics method testTagsForPrefix.

@Test
public void testTagsForPrefix() throws Exception {
    ConfigBuilder cb = new ConfigBuilder().add(testNamePrefix + ".sink.ganglia.tagsForPrefix.all", "*").add(testNamePrefix + ".sink.ganglia.tagsForPrefix.some", "NumActiveSinks, " + "NumActiveSources").add(testNamePrefix + ".sink.ganglia.tagsForPrefix.none", "");
    GangliaSink30 sink = new GangliaSink30();
    sink.init(cb.subset(testNamePrefix + ".sink.ganglia"));
    List<MetricsTag> tags = new ArrayList<MetricsTag>();
    tags.add(new MetricsTag(MsInfo.Context, "all"));
    tags.add(new MetricsTag(MsInfo.NumActiveSources, "foo"));
    tags.add(new MetricsTag(MsInfo.NumActiveSinks, "bar"));
    tags.add(new MetricsTag(MsInfo.NumAllSinks, "haa"));
    tags.add(new MetricsTag(MsInfo.Hostname, "host"));
    Set<AbstractMetric> metrics = new HashSet<AbstractMetric>();
    MetricsRecord record = new MetricsRecordImpl(MsInfo.Context, (long) 1, tags, metrics);
    StringBuilder sb = new StringBuilder();
    sink.appendPrefix(record, sb);
    assertEquals(".NumActiveSources=foo.NumActiveSinks=bar.NumAllSinks=haa", sb.toString());
    tags.set(0, new MetricsTag(MsInfo.Context, "some"));
    sb = new StringBuilder();
    sink.appendPrefix(record, sb);
    assertEquals(".NumActiveSources=foo.NumActiveSinks=bar", sb.toString());
    tags.set(0, new MetricsTag(MsInfo.Context, "none"));
    sb = new StringBuilder();
    sink.appendPrefix(record, sb);
    assertEquals("", sb.toString());
    tags.set(0, new MetricsTag(MsInfo.Context, "nada"));
    sb = new StringBuilder();
    sink.appendPrefix(record, sb);
    assertEquals("", sb.toString());
}
Also used : MetricsRecord(org.apache.hadoop.metrics2.MetricsRecord) ArrayList(java.util.ArrayList) AbstractMetric(org.apache.hadoop.metrics2.AbstractMetric) GangliaSink30(org.apache.hadoop.metrics2.sink.ganglia.GangliaSink30) MetricsTag(org.apache.hadoop.metrics2.MetricsTag) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 18 with MetricsTag

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

the class TestGraphiteMetrics method testPutMetrics2.

@Test
public void testPutMetrics2() {
    GraphiteSink sink = new GraphiteSink();
    List<MetricsTag> tags = new ArrayList<MetricsTag>();
    tags.add(new MetricsTag(MsInfo.Context, "all"));
    tags.add(new MetricsTag(MsInfo.Hostname, null));
    Set<AbstractMetric> metrics = new HashSet<AbstractMetric>();
    metrics.add(makeMetric("foo1", 1));
    metrics.add(makeMetric("foo2", 2));
    MetricsRecord record = new MetricsRecordImpl(MsInfo.Context, (long) 10000, tags, metrics);
    ArgumentCaptor<String> argument = ArgumentCaptor.forClass(String.class);
    final GraphiteSink.Graphite mockGraphite = makeGraphite();
    Whitebox.setInternalState(sink, "graphite", mockGraphite);
    sink.putMetrics(record);
    try {
        verify(mockGraphite).write(argument.capture());
    } catch (IOException e) {
        e.printStackTrace();
    }
    String result = argument.getValue();
    assertEquals(true, result.equals("null.all.Context.Context=all.foo1 1 10\n" + "null.all.Context.Context=all.foo2 2 10\n") || result.equals("null.all.Context.Context=all.foo2 2 10\n" + "null.all.Context.Context=all.foo1 1 10\n"));
}
Also used : MetricsRecord(org.apache.hadoop.metrics2.MetricsRecord) ArrayList(java.util.ArrayList) AbstractMetric(org.apache.hadoop.metrics2.AbstractMetric) Matchers.anyString(org.mockito.Matchers.anyString) IOException(java.io.IOException) MetricsTag(org.apache.hadoop.metrics2.MetricsTag) GraphiteSink(org.apache.hadoop.metrics2.sink.GraphiteSink) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 19 with MetricsTag

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

the class TestGraphiteMetrics method testPutMetrics3.

/**
     * Assert that timestamps are converted correctly, ticket HADOOP-11182
     */
@Test
public void testPutMetrics3() {
    // setup GraphiteSink
    GraphiteSink sink = new GraphiteSink();
    final GraphiteSink.Graphite mockGraphite = makeGraphite();
    Whitebox.setInternalState(sink, "graphite", mockGraphite);
    // given two metrics records with timestamps 1000 milliseconds apart.
    List<MetricsTag> tags = Collections.emptyList();
    Set<AbstractMetric> metrics = new HashSet<AbstractMetric>();
    metrics.add(makeMetric("foo1", 1));
    MetricsRecord record1 = new MetricsRecordImpl(MsInfo.Context, 1000000000000L, tags, metrics);
    MetricsRecord record2 = new MetricsRecordImpl(MsInfo.Context, 1000000001000L, tags, metrics);
    sink.putMetrics(record1);
    sink.putMetrics(record2);
    sink.flush();
    try {
        sink.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    // then the timestamps in the graphite stream should differ by one second.
    try {
        verify(mockGraphite).write(eq("null.default.Context.foo1 1 1000000000\n"));
        verify(mockGraphite).write(eq("null.default.Context.foo1 1 1000000001\n"));
    } catch (IOException e) {
        e.printStackTrace();
    }
}
Also used : MetricsRecord(org.apache.hadoop.metrics2.MetricsRecord) AbstractMetric(org.apache.hadoop.metrics2.AbstractMetric) IOException(java.io.IOException) MetricsTag(org.apache.hadoop.metrics2.MetricsTag) GraphiteSink(org.apache.hadoop.metrics2.sink.GraphiteSink) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 20 with MetricsTag

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

the class TestMetricsSourceAdapter method testPurgeOldMetrics.

@Test
public void testPurgeOldMetrics() throws Exception {
    // create test source with a single metric counter of value 1
    PurgableSource source = new PurgableSource();
    MetricsSourceBuilder sb = MetricsAnnotations.newSourceBuilder(source);
    final MetricsSource s = sb.build();
    List<MetricsTag> injectedTags = new ArrayList<MetricsTag>();
    MetricsSourceAdapter sa = new MetricsSourceAdapter("tst", "tst", "testdesc", s, injectedTags, null, null, 1, false);
    MBeanInfo info = sa.getMBeanInfo();
    boolean sawIt = false;
    for (MBeanAttributeInfo mBeanAttributeInfo : info.getAttributes()) {
        sawIt |= mBeanAttributeInfo.getName().equals(source.lastKeyName);
    }
    ;
    assertTrue("The last generated metric is not exported to jmx", sawIt);
    // skip JMX cache TTL
    Thread.sleep(1000);
    info = sa.getMBeanInfo();
    sawIt = false;
    for (MBeanAttributeInfo mBeanAttributeInfo : info.getAttributes()) {
        sawIt |= mBeanAttributeInfo.getName().equals(source.lastKeyName);
    }
    ;
    assertTrue("The last generated metric is not exported to jmx", sawIt);
}
Also used : MetricsSource(org.apache.hadoop.metrics2.MetricsSource) MBeanInfo(javax.management.MBeanInfo) MetricsSourceBuilder(org.apache.hadoop.metrics2.lib.MetricsSourceBuilder) ArrayList(java.util.ArrayList) MetricsTag(org.apache.hadoop.metrics2.MetricsTag) MBeanAttributeInfo(javax.management.MBeanAttributeInfo) Test(org.junit.Test)

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