Search in sources :

Example 41 with Record

use of org.apache.hadoop.metrics2.util.MetricsCache.Record in project hadoop by apache.

the class MetricsRecords method assertTag.

public static void assertTag(MetricsRecord record, String tagName, String expectedValue) {
    MetricsTag processIdTag = getFirstTagByName(record, tagName);
    assertNotNull(processIdTag);
    assertEquals(expectedValue, processIdTag.value());
}
Also used : MetricsTag(org.apache.hadoop.metrics2.MetricsTag)

Example 42 with Record

use of org.apache.hadoop.metrics2.util.MetricsCache.Record 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 43 with Record

use of org.apache.hadoop.metrics2.util.MetricsCache.Record in project hadoop by apache.

the class TestMetricsCache method testGet.

@SuppressWarnings("deprecation")
@Test
public void testGet() {
    MetricsCache cache = new MetricsCache();
    assertNull("empty", cache.get("r", Arrays.asList(makeTag("t", "t"))));
    MetricsRecord mr = makeRecord("r", Arrays.asList(makeTag("t", "t")), Arrays.asList(makeMetric("m", 1)));
    cache.update(mr);
    MetricsCache.Record cr = cache.get("r", mr.tags());
    LOG.debug("tags=" + mr.tags() + " cr=" + cr);
    assertNotNull("Got record", cr);
    assertEquals("contains 1 metric", 1, cr.metrics().size());
    checkMetricValue("new metric value", cr, "m", 1);
}
Also used : MetricsRecord(org.apache.hadoop.metrics2.MetricsRecord) Test(org.junit.Test)

Example 44 with Record

use of org.apache.hadoop.metrics2.util.MetricsCache.Record 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 45 with Record

use of org.apache.hadoop.metrics2.util.MetricsCache.Record in project hadoop by apache.

the class GangliaSink30 method appendPrefix.

@InterfaceAudience.Private
public void appendPrefix(MetricsRecord record, StringBuilder sb) {
    String contextName = record.context();
    Collection<MetricsTag> tags = record.tags();
    if (useTagsMap.containsKey(contextName)) {
        Set<String> useTags = useTagsMap.get(contextName);
        for (MetricsTag t : tags) {
            if (useTags == null || useTags.contains(t.name())) {
                if (t.info() != MsInfo.Context && t.info() != MsInfo.Hostname && t.value() != null) {
                    sb.append('.').append(t.name()).append('=').append(t.value());
                }
            }
        }
    }
}
Also used : MetricsTag(org.apache.hadoop.metrics2.MetricsTag)

Aggregations

AbstractMetric (org.apache.hadoop.metrics2.AbstractMetric)27 MetricsTag (org.apache.hadoop.metrics2.MetricsTag)20 MetricsRecord (org.apache.hadoop.metrics2.MetricsRecord)18 Test (org.junit.Test)16 MetricsRecordBuilder (org.apache.hadoop.metrics2.MetricsRecordBuilder)10 ArrayList (java.util.ArrayList)9 HashSet (java.util.HashSet)7 IOException (java.io.IOException)6 MetricsCollectorImpl (org.apache.hadoop.metrics2.impl.MetricsCollectorImpl)4 Matchers.anyString (org.mockito.Matchers.anyString)4 MetricsException (org.apache.hadoop.metrics2.MetricsException)3 MetricsSystem (org.apache.hadoop.metrics2.MetricsSystem)3 GraphiteSink (org.apache.hadoop.metrics2.sink.GraphiteSink)3 DatagramPacket (java.net.DatagramPacket)2 DatagramSocket (java.net.DatagramSocket)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 Entry (java.util.Map.Entry)2 Configuration (org.apache.hadoop.conf.Configuration)2 StatsDSink (org.apache.hadoop.metrics2.sink.StatsDSink)2