Search in sources :

Example 11 with MetricsTag

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

the class TestStatsDMetrics method testPutMetrics.

@Test(timeout = 3000)
public void testPutMetrics() throws IOException, InterruptedException {
    final StatsDSink sink = new StatsDSink();
    List<MetricsTag> tags = new ArrayList<MetricsTag>();
    tags.add(new MetricsTag(MsInfo.Hostname, "host"));
    tags.add(new MetricsTag(MsInfo.Context, "jvm"));
    tags.add(new MetricsTag(MsInfo.ProcessName, "process"));
    Set<AbstractMetric> metrics = new HashSet<AbstractMetric>();
    metrics.add(makeMetric("foo1", 1.25, MetricType.COUNTER));
    metrics.add(makeMetric("foo2", 2.25, MetricType.GAUGE));
    final MetricsRecord record = new MetricsRecordImpl(MsInfo.Context, (long) 10000, tags, metrics);
    try (DatagramSocket sock = new DatagramSocket()) {
        sock.setReceiveBufferSize(8192);
        final StatsDSink.StatsD mockStatsD = new StatsD(sock.getLocalAddress().getHostName(), sock.getLocalPort());
        Whitebox.setInternalState(sink, "statsd", mockStatsD);
        final DatagramPacket p = new DatagramPacket(new byte[8192], 8192);
        sink.putMetrics(record);
        sock.receive(p);
        String result = new String(p.getData(), 0, p.getLength(), Charset.forName("UTF-8"));
        assertTrue("Received data did not match data sent", result.equals("host.process.jvm.Context.foo1:1.25|c") || result.equals("host.process.jvm.Context.foo2:2.25|g"));
    } finally {
        sink.close();
    }
}
Also used : StatsDSink(org.apache.hadoop.metrics2.sink.StatsDSink) MetricsRecord(org.apache.hadoop.metrics2.MetricsRecord) ArrayList(java.util.ArrayList) AbstractMetric(org.apache.hadoop.metrics2.AbstractMetric) MetricsTag(org.apache.hadoop.metrics2.MetricsTag) StatsD(org.apache.hadoop.metrics2.sink.StatsDSink.StatsD) DatagramSocket(java.net.DatagramSocket) StatsD(org.apache.hadoop.metrics2.sink.StatsDSink.StatsD) DatagramPacket(java.net.DatagramPacket) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 12 with MetricsTag

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

the class TestStatsDMetrics method testPutMetrics2.

@Test(timeout = 3000)
public void testPutMetrics2() throws IOException {
    StatsDSink sink = new StatsDSink();
    List<MetricsTag> tags = new ArrayList<MetricsTag>();
    tags.add(new MetricsTag(MsInfo.Hostname, null));
    tags.add(new MetricsTag(MsInfo.Context, "jvm"));
    tags.add(new MetricsTag(MsInfo.ProcessName, "process"));
    Set<AbstractMetric> metrics = new HashSet<AbstractMetric>();
    metrics.add(makeMetric("foo1", 1, MetricType.COUNTER));
    metrics.add(makeMetric("foo2", 2, MetricType.GAUGE));
    MetricsRecord record = new MetricsRecordImpl(MsInfo.Context, (long) 10000, tags, metrics);
    try (DatagramSocket sock = new DatagramSocket()) {
        sock.setReceiveBufferSize(8192);
        final StatsDSink.StatsD mockStatsD = new StatsD(sock.getLocalAddress().getHostName(), sock.getLocalPort());
        Whitebox.setInternalState(sink, "statsd", mockStatsD);
        final DatagramPacket p = new DatagramPacket(new byte[8192], 8192);
        sink.putMetrics(record);
        sock.receive(p);
        String result = new String(p.getData(), 0, p.getLength(), Charset.forName("UTF-8"));
        assertTrue("Received data did not match data sent", result.equals("process.jvm.Context.foo1:1|c") || result.equals("process.jvm.Context.foo2:2|g"));
    } finally {
        sink.close();
    }
}
Also used : StatsDSink(org.apache.hadoop.metrics2.sink.StatsDSink) MetricsRecord(org.apache.hadoop.metrics2.MetricsRecord) ArrayList(java.util.ArrayList) AbstractMetric(org.apache.hadoop.metrics2.AbstractMetric) MetricsTag(org.apache.hadoop.metrics2.MetricsTag) StatsD(org.apache.hadoop.metrics2.sink.StatsDSink.StatsD) DatagramSocket(java.net.DatagramSocket) StatsD(org.apache.hadoop.metrics2.sink.StatsDSink.StatsD) DatagramPacket(java.net.DatagramPacket) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 13 with MetricsTag

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

the class TestGraphiteMetrics method testPutMetrics.

@Test
public void testPutMetrics() {
    GraphiteSink sink = new GraphiteSink();
    List<MetricsTag> tags = new ArrayList<MetricsTag>();
    tags.add(new MetricsTag(MsInfo.Context, "all"));
    tags.add(new MetricsTag(MsInfo.Hostname, "host"));
    Set<AbstractMetric> metrics = new HashSet<AbstractMetric>();
    metrics.add(makeMetric("foo1", 1.25));
    metrics.add(makeMetric("foo2", 2.25));
    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.Hostname=host.foo1 1.25 10\n" + "null.all.Context.Context=all.Hostname=host.foo2 2.25 10\n") || result.equals("null.all.Context.Context=all.Hostname=host.foo2 2.25 10\n" + "null.all.Context.Context=all.Hostname=host.foo1 1.25 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 14 with MetricsTag

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

the class TestGraphiteMetrics method testFailureAndPutMetrics.

@Test
public void testFailureAndPutMetrics() throws IOException {
    GraphiteSink sink = new GraphiteSink();
    List<MetricsTag> tags = new ArrayList<MetricsTag>();
    tags.add(new MetricsTag(MsInfo.Context, "all"));
    tags.add(new MetricsTag(MsInfo.Hostname, "host"));
    Set<AbstractMetric> metrics = new HashSet<AbstractMetric>();
    metrics.add(makeMetric("foo1", 1.25));
    metrics.add(makeMetric("foo2", 2.25));
    MetricsRecord record = new MetricsRecordImpl(MsInfo.Context, (long) 10000, tags, metrics);
    final GraphiteSink.Graphite mockGraphite = makeGraphite();
    Whitebox.setInternalState(sink, "graphite", mockGraphite);
    // throw exception when first try
    doThrow(new IOException("IO exception")).when(mockGraphite).write(anyString());
    sink.putMetrics(record);
    verify(mockGraphite).write(anyString());
    verify(mockGraphite).close();
    // reset mock and try again
    reset(mockGraphite);
    when(mockGraphite.isConnected()).thenReturn(false);
    ArgumentCaptor<String> argument = ArgumentCaptor.forClass(String.class);
    sink.putMetrics(record);
    verify(mockGraphite).write(argument.capture());
    String result = argument.getValue();
    assertEquals(true, result.equals("null.all.Context.Context=all.Hostname=host.foo1 1.25 10\n" + "null.all.Context.Context=all.Hostname=host.foo2 2.25 10\n") || result.equals("null.all.Context.Context=all.Hostname=host.foo2 2.25 10\n" + "null.all.Context.Context=all.Hostname=host.foo1 1.25 10\n"));
}
Also used : MetricsRecord(org.apache.hadoop.metrics2.MetricsRecord) ArrayList(java.util.ArrayList) AbstractMetric(org.apache.hadoop.metrics2.AbstractMetric) IOException(java.io.IOException) Matchers.anyString(org.mockito.Matchers.anyString) MetricsTag(org.apache.hadoop.metrics2.MetricsTag) GraphiteSink(org.apache.hadoop.metrics2.sink.GraphiteSink) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 15 with MetricsTag

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

the class TestInterns method testTagOverflow.

@Test
public void testTagOverflow() {
    MetricsTag t0 = tag("t0", "t desc", "t value");
    for (int i = 0; i < MAX_TAG_NAMES + 1; ++i) {
        tag("t" + i, "t desc", "t value");
        if (i < MAX_TAG_NAMES) {
            assertSame("t0 still there", t0, tag("t0", "t desc", "t value"));
        }
    }
    assertNotSame("t0 is gone", t0, tag("t0", "t desc", "t value"));
    MetricsTag t1 = tag("t1", "t desc", "t value");
    for (int i = 0; i < MAX_TAG_VALUES; ++i) {
        tag("t1", "t desc", "t value" + i);
        if (i < MAX_TAG_VALUES - 1) {
            assertSame("t1 is still there", t1, tag("t1", "t desc", "t value"));
        }
    }
    assertNotSame("t1 is gone", t1, tag("t1", "t desc", "t value"));
}
Also used : MetricsTag(org.apache.hadoop.metrics2.MetricsTag) 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