Search in sources :

Example 1 with GraphiteSink

use of org.apache.hadoop.metrics2.sink.GraphiteSink 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 2 with GraphiteSink

use of org.apache.hadoop.metrics2.sink.GraphiteSink 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 3 with GraphiteSink

use of org.apache.hadoop.metrics2.sink.GraphiteSink 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 4 with GraphiteSink

use of org.apache.hadoop.metrics2.sink.GraphiteSink 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 5 with GraphiteSink

use of org.apache.hadoop.metrics2.sink.GraphiteSink in project hadoop by apache.

the class TestGraphiteMetrics method testClose.

@Test
public void testClose() {
    GraphiteSink sink = new GraphiteSink();
    final GraphiteSink.Graphite mockGraphite = makeGraphite();
    Whitebox.setInternalState(sink, "graphite", mockGraphite);
    try {
        sink.close();
    } catch (IOException ioe) {
        ioe.printStackTrace();
    }
    try {
        verify(mockGraphite).close();
    } catch (IOException ioe) {
        ioe.printStackTrace();
    }
}
Also used : IOException(java.io.IOException) GraphiteSink(org.apache.hadoop.metrics2.sink.GraphiteSink) Test(org.junit.Test)

Aggregations

IOException (java.io.IOException)5 GraphiteSink (org.apache.hadoop.metrics2.sink.GraphiteSink)5 Test (org.junit.Test)5 HashSet (java.util.HashSet)4 AbstractMetric (org.apache.hadoop.metrics2.AbstractMetric)4 MetricsRecord (org.apache.hadoop.metrics2.MetricsRecord)4 MetricsTag (org.apache.hadoop.metrics2.MetricsTag)4 ArrayList (java.util.ArrayList)3 Matchers.anyString (org.mockito.Matchers.anyString)3