Java Code Examples for org.apache.hadoop.metrics2.MetricsRecord

The following examples show how to use org.apache.hadoop.metrics2.MetricsRecord. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: ambari-metrics   Source File: HadoopTimelineMetricsSink.java    License: Apache License 2.0 6 votes vote down vote up
private void emitContainerMetrics(MetricsRecord record) {

    ContainerMetric containerMetric = new ContainerMetric();
    containerMetric.setHostName(hostName);

    for (MetricsTag tag : record.tags()) {
      if (tag.name().equals("ContainerResource")) {
        containerMetric.setContainerId(tag.value());
      }
    }

    parseContainerMetrics(record, containerMetric);
    List<ContainerMetric> list = new ArrayList<>();
    list.add(containerMetric);
    String jsonData = null;
    try {
      jsonData = mapper.writeValueAsString(list);
    } catch (IOException e) {
      LOG.error("Unable to parse container metrics ", e);
    }
    if (jsonData != null) {
      String collectorHost = getCurrentCollectorHost();
      containerMetricsUri = constructContainerMetricUri(protocol, collectorHost, port);
      emitMetricsJson(containerMetricsUri, jsonData);
    }
  }
 
Example 2
Source Project: ambari-metrics   Source File: HadoopTimelineMetricsSink.java    License: Apache License 2.0 6 votes vote down vote up
@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())) {

        // the context is always skipped here because it is always added

        // the hostname is always skipped to avoid case-mismatches
        // from different DNSes.

        if (t.info() != MsInfo.Context && t.info() != MsInfo.Hostname && t.value() != null) {
          sb.append('.').append(t.name()).append('=').append(t.value());
        }
      }
    }
  }
}
 
Example 3
Source Project: hadoop   Source File: AzureBlobStorageTestAccount.java    License: Apache License 2.0 6 votes vote down vote up
public Number getLatestMetricValue(String metricName, Number defaultValue)
    throws IndexOutOfBoundsException{
  boolean found = false;
  Number ret = null;
  for (MetricsRecord currentRecord : allMetrics) {
    // First check if this record is coming for my file system.
    if (wasGeneratedByMe(currentRecord)) {
      for (AbstractMetric currentMetric : currentRecord.metrics()) {
        if (currentMetric.name().equalsIgnoreCase(metricName)) {
          found = true;
          ret = currentMetric.value();
          break;
        }
      }
    }
  }
  if (!found) {
    if (defaultValue != null) {
      return defaultValue;
    }
    throw new IndexOutOfBoundsException(metricName);
  }
  return ret;
}
 
Example 4
Source Project: hadoop   Source File: FileSink.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void putMetrics(MetricsRecord record) {
  writer.print(record.timestamp());
  writer.print(" ");
  writer.print(record.context());
  writer.print(".");
  writer.print(record.name());
  String separator = ": ";
  for (MetricsTag tag : record.tags()) {
    writer.print(separator);
    separator = ", ";
    writer.print(tag.name());
    writer.print("=");
    writer.print(tag.value());
  }
  for (AbstractMetric metric : record.metrics()) {
    writer.print(separator);
    separator = ", ";
    writer.print(metric.name());
    writer.print("=");
    writer.print(metric.value());
  }
  writer.println();
}
 
Example 5
Source Project: hadoop   Source File: GangliaSink30.java    License: Apache License 2.0 6 votes vote down vote up
@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())) {

        // the context is always skipped here because it is always added
        
        // the hostname is always skipped to avoid case-mismatches 
        // from different DNSes.

        if (t.info() != MsInfo.Context && t.info() != MsInfo.Hostname && t.value() != null) {
          sb.append('.').append(t.name()).append('=').append(t.value());
        }
      }
    }
  }          
}
 
Example 6
Source Project: hadoop   Source File: MetricsCache.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Update the cache and return the current cached record
 * @param mr the update record
 * @param includingTags cache tag values (for later lookup by name) if true
 * @return the updated cache record
 */
public Record update(MetricsRecord mr, boolean includingTags) {
  String name = mr.name();
  RecordCache recordCache = map.get(name);
  if (recordCache == null) {
    recordCache = new RecordCache();
    map.put(name, recordCache);
  }
  Collection<MetricsTag> tags = mr.tags();
  Record record = recordCache.get(tags);
  if (record == null) {
    record = new Record();
    recordCache.put(tags, record);
  }
  for (AbstractMetric m : mr.metrics()) {
    record.metrics.put(m.name(), m);
  }
  if (includingTags) {
    // mostly for some sinks that include tags as part of a dense schema
    for (MetricsTag t : mr.tags()) {
      record.tags.put(t.name(), t.value());
    }
  }
  return record;
}
 
Example 7
Source Project: hadoop   Source File: TestMetricsSystemImpl.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void putMetrics(MetricsRecord record) {
  final String prefix = "threadSourceRec";
  if (record.name().startsWith(prefix)) {
    final int recordNumber = Integer.parseInt(
        record.name().substring(prefix.length()));
    ArrayList<String> names = new ArrayList<String>();
    for (AbstractMetric m : record.metrics()) {
      if (m.name().equalsIgnoreCase("g1")) {
        collected[recordNumber].set(m.value().longValue());
        return;
      }
      names.add(m.name());
    }
  }
}
 
Example 8
Source Project: hadoop   Source File: TestMetricsSystemImpl.java    License: Apache License 2.0 6 votes vote down vote up
private void checkMetricsRecords(List<MetricsRecord> recs) {
  LOG.debug(recs);
  MetricsRecord r = recs.get(0);
  assertEquals("name", "s1rec", r.name());
  assertEquals("tags", new MetricsTag[] {
    tag(MsInfo.Context, "test"),
    tag(MsInfo.Hostname, hostname)}, r.tags());
  assertEquals("metrics", MetricsLists.builder("")
    .addCounter(info("C1", "C1 desc"), 1L)
    .addGauge(info("G1", "G1 desc"), 2L)
    .addCounter(info("S1NumOps", "Number of ops for s1"), 1L)
    .addGauge(info("S1AvgTime", "Average time for s1"), 0.0)
    .metrics(), r.metrics());

  r = recs.get(1);
  assertTrue("NumActiveSinks should be 3", Iterables.contains(r.metrics(),
             new MetricGaugeInt(MsInfo.NumActiveSinks, 3)));
}
 
Example 9
Source Project: big-c   Source File: AzureBlobStorageTestAccount.java    License: Apache License 2.0 6 votes vote down vote up
public Number getLatestMetricValue(String metricName, Number defaultValue)
    throws IndexOutOfBoundsException{
  boolean found = false;
  Number ret = null;
  for (MetricsRecord currentRecord : allMetrics) {
    // First check if this record is coming for my file system.
    if (wasGeneratedByMe(currentRecord)) {
      for (AbstractMetric currentMetric : currentRecord.metrics()) {
        if (currentMetric.name().equalsIgnoreCase(metricName)) {
          found = true;
          ret = currentMetric.value();
          break;
        }
      }
    }
  }
  if (!found) {
    if (defaultValue != null) {
      return defaultValue;
    }
    throw new IndexOutOfBoundsException(metricName);
  }
  return ret;
}
 
Example 10
Source Project: big-c   Source File: FileSink.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void putMetrics(MetricsRecord record) {
  writer.print(record.timestamp());
  writer.print(" ");
  writer.print(record.context());
  writer.print(".");
  writer.print(record.name());
  String separator = ": ";
  for (MetricsTag tag : record.tags()) {
    writer.print(separator);
    separator = ", ";
    writer.print(tag.name());
    writer.print("=");
    writer.print(tag.value());
  }
  for (AbstractMetric metric : record.metrics()) {
    writer.print(separator);
    separator = ", ";
    writer.print(metric.name());
    writer.print("=");
    writer.print(metric.value());
  }
  writer.println();
}
 
Example 11
Source Project: big-c   Source File: GangliaSink30.java    License: Apache License 2.0 6 votes vote down vote up
@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())) {

        // the context is always skipped here because it is always added
        
        // the hostname is always skipped to avoid case-mismatches 
        // from different DNSes.

        if (t.info() != MsInfo.Context && t.info() != MsInfo.Hostname && t.value() != null) {
          sb.append('.').append(t.name()).append('=').append(t.value());
        }
      }
    }
  }          
}
 
Example 12
Source Project: big-c   Source File: MetricsCache.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Update the cache and return the current cached record
 * @param mr the update record
 * @param includingTags cache tag values (for later lookup by name) if true
 * @return the updated cache record
 */
public Record update(MetricsRecord mr, boolean includingTags) {
  String name = mr.name();
  RecordCache recordCache = map.get(name);
  if (recordCache == null) {
    recordCache = new RecordCache();
    map.put(name, recordCache);
  }
  Collection<MetricsTag> tags = mr.tags();
  Record record = recordCache.get(tags);
  if (record == null) {
    record = new Record();
    recordCache.put(tags, record);
  }
  for (AbstractMetric m : mr.metrics()) {
    record.metrics.put(m.name(), m);
  }
  if (includingTags) {
    // mostly for some sinks that include tags as part of a dense schema
    for (MetricsTag t : mr.tags()) {
      record.tags.put(t.name(), t.value());
    }
  }
  return record;
}
 
Example 13
Source Project: big-c   Source File: TestMetricsSystemImpl.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void putMetrics(MetricsRecord record) {
  final String prefix = "threadSourceRec";
  if (record.name().startsWith(prefix)) {
    final int recordNumber = Integer.parseInt(
        record.name().substring(prefix.length()));
    ArrayList<String> names = new ArrayList<String>();
    for (AbstractMetric m : record.metrics()) {
      if (m.name().equalsIgnoreCase("g1")) {
        collected[recordNumber].set(m.value().longValue());
        return;
      }
      names.add(m.name());
    }
  }
}
 
Example 14
Source Project: big-c   Source File: TestMetricsSystemImpl.java    License: Apache License 2.0 6 votes vote down vote up
private void checkMetricsRecords(List<MetricsRecord> recs) {
  LOG.debug(recs);
  MetricsRecord r = recs.get(0);
  assertEquals("name", "s1rec", r.name());
  assertEquals("tags", new MetricsTag[] {
    tag(MsInfo.Context, "test"),
    tag(MsInfo.Hostname, hostname)}, r.tags());
  assertEquals("metrics", MetricsLists.builder("")
    .addCounter(info("C1", "C1 desc"), 1L)
    .addGauge(info("G1", "G1 desc"), 2L)
    .addCounter(info("S1NumOps", "Number of ops for s1"), 1L)
    .addGauge(info("S1AvgTime", "Average time for s1"), 0.0)
    .metrics(), r.metrics());

  r = recs.get(1);
  assertTrue("NumActiveSinks should be 3", Iterables.contains(r.metrics(),
             new MetricGaugeInt(MsInfo.NumActiveSinks, 3)));
}
 
Example 15
Source Project: phoenix   Source File: PhoenixTraceReaderIT.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void singleSpan() throws Exception {
    PhoenixMetricsSink sink = new PhoenixMetricsSink();
    Properties props = new Properties(TEST_PROPERTIES);
    Connection conn = DriverManager.getConnection(getUrl(), props);
    sink.initForTesting(conn);

    // create a simple metrics record
    long traceid = 987654;
    MetricsRecord record =
            createAndFlush(sink, traceid, Span.ROOT_SPAN_ID, 10, "root", 12, 13,
                "host-name.value", "test annotation for a span");

    // start a reader
    validateTraces(Collections.singletonList(record), conn, traceid);
}
 
Example 16
Source Project: phoenix   Source File: LoggingSink.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void putMetrics(MetricsRecord record) {
    // we could wait until flush, but this is a really lightweight process, so we just write
    // them
    // as soon as we get them
    if (!LOGGER.isDebugEnabled()) {
        return;
    }
    LOGGER.debug("Found record:" + record.name());
    for (AbstractMetric metric : record.metrics()) {
        // just print the metric we care about
        if (metric.name().startsWith(TracingUtils.METRIC_SOURCE_KEY)) {
            LOGGER.debug("\t metric:" + metric);
        }
    }
}
 
Example 17
Source Project: phoenix   Source File: LoggingSink.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void putMetrics(MetricsRecord record) {
    // we could wait until flush, but this is a really lightweight process, so we just write
    // them
    // as soon as we get them
    if (!LOG.isDebugEnabled()) {
        return;
    }
    LOG.debug("Found record:" + record.name());
    for (AbstractMetric metric : record.metrics()) {
        // just print the metric we care about
        if (metric.name().startsWith(TracingUtils.METRIC_SOURCE_KEY)) {
            LOG.debug("\t metric:" + metric);
        }
    }
}
 
Example 18
Source Project: hadoop   Source File: TestContainerMetrics.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testContainerMetricsLimit() throws InterruptedException {
  final String ERR = "Error in number of records";

  MetricsSystem system = mock(MetricsSystem.class);
  doReturn(this).when(system).register(anyString(), anyString(), any());

  MetricsCollectorImpl collector = new MetricsCollectorImpl();
  ContainerId containerId = mock(ContainerId.class);
  ContainerMetrics metrics = ContainerMetrics.forContainer(containerId, 100);

  int anyPmemLimit = 1024;
  int anyVmemLimit = 2048;
  int anyVcores = 10;
  int anyGcores = 10;
  String anyProcessId = "1234";

  metrics.recordResourceLimit(anyVmemLimit, anyPmemLimit, anyVcores, anyGcores);
  metrics.recordProcessId(anyProcessId);

  Thread.sleep(110);
  metrics.getMetrics(collector, true);
  assertEquals(ERR, 1, collector.getRecords().size());
  MetricsRecord record = collector.getRecords().get(0);

  MetricsRecords.assertTag(record, ContainerMetrics.PROCESSID_INFO.name(),
      anyProcessId);

  MetricsRecords.assertMetric(record, ContainerMetrics
      .PMEM_LIMIT_METRIC_NAME, anyPmemLimit);
  MetricsRecords.assertMetric(record, ContainerMetrics.VMEM_LIMIT_METRIC_NAME, anyVmemLimit);
  MetricsRecords.assertMetric(record, ContainerMetrics.VCORE_LIMIT_METRIC_NAME, anyVcores);

  collector.clear();
}
 
Example 19
Source Project: hadoop   Source File: AzureBlobStorageTestAccount.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Checks if the given record was generated by my WASB file system instance.
 * @param currentRecord The metrics record to check.
 * @return
 */
private boolean wasGeneratedByMe(MetricsRecord currentRecord) {
  String myFsId = fs.getInstrumentation().getFileSystemInstanceId().toString();
  for (MetricsTag currentTag : currentRecord.tags()) {
    if (currentTag.name().equalsIgnoreCase("wasbFileSystemId")) {
      return currentTag.value().equals(myFsId);
    }
  }
  return false;
}
 
Example 20
Source Project: hadoop   Source File: AbstractMetricsRecord.java    License: Apache License 2.0 5 votes vote down vote up
@Override public boolean equals(Object obj) {
  if (obj instanceof MetricsRecord) {
    final MetricsRecord other = (MetricsRecord) obj;
    return Objects.equal(timestamp(), other.timestamp()) &&
           Objects.equal(name(), other.name()) &&
           Objects.equal(description(), other.description()) &&
           Objects.equal(tags(), other.tags()) &&
           Iterables.elementsEqual(metrics(), other.metrics());
  }
  return false;
}
 
Example 21
Source Project: hadoop   Source File: TestMetricsCache.java    License: Apache License 2.0 5 votes vote down vote up
@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",
      Arrays.asList(makeTag("t", "tv3")), // different tag value
      Arrays.asList(makeMetric("m3", 3)));
  cr = cache.update(mr3); // should get a new record
  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);
}
 
Example 22
Source Project: hadoop   Source File: TestMetricsCache.java    License: Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example 23
Source Project: hadoop   Source File: TestMetricsCache.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * 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"));
}
 
Example 24
Source Project: hadoop   Source File: TestMetricsCache.java    License: Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 25
Source Project: hadoop   Source File: MetricsRecords.java    License: Apache License 2.0 5 votes vote down vote up
public static void assertTag(MetricsRecord record, String tagName,
    String expectedValue) {
  MetricsTag processIdTag = getFirstTagByName(record,
      tagName);
  assertNotNull(processIdTag);
  assertEquals(expectedValue, processIdTag.value());
}
 
Example 26
Source Project: hadoop   Source File: MetricsRecords.java    License: Apache License 2.0 5 votes vote down vote up
public static void assertMetric(MetricsRecord record,
    String metricName,
    Number expectedValue) {
  AbstractMetric resourceLimitMetric = getFirstMetricByName(
      record, metricName);
  assertNotNull(resourceLimitMetric);
  assertEquals(expectedValue, resourceLimitMetric.value());
}
 
Example 27
Source Project: hadoop   Source File: TestMetricsSystemImpl.java    License: Apache License 2.0 5 votes vote down vote up
@Test public void testInitFirstVerifyCallBacks() throws Exception {
  DefaultMetricsSystem.shutdown(); 
  new ConfigBuilder().add("*.period", 8)
      //.add("test.sink.plugin.urls", getPluginUrlsAsString())
      .add("test.sink.test.class", TestSink.class.getName())
      .add("test.*.source.filter.exclude", "s0")
      .add("test.source.s1.metric.filter.exclude", "X*")
      .add("test.sink.sink1.metric.filter.exclude", "Y*")
      .add("test.sink.sink2.metric.filter.exclude", "Y*")
      .save(TestMetricsConfig.getTestFilename("hadoop-metrics2-test"));
  MetricsSystemImpl ms = new MetricsSystemImpl("Test");
  ms.start();
  ms.register("s0", "s0 desc", new TestSource("s0rec"));
  TestSource s1 = ms.register("s1", "s1 desc", new TestSource("s1rec"));
  s1.c1.incr();
  s1.xxx.incr();
  s1.g1.set(2);
  s1.yyy.incr(2);
  s1.s1.add(0);
  MetricsSink sink1 = mock(MetricsSink.class);
  MetricsSink sink2 = mock(MetricsSink.class);
  ms.registerSink("sink1", "sink1 desc", sink1);
  ms.registerSink("sink2", "sink2 desc", sink2);
  ms.publishMetricsNow(); // publish the metrics

  try {
    verify(sink1, timeout(200).times(2)).putMetrics(r1.capture());
    verify(sink2, timeout(200).times(2)).putMetrics(r2.capture());
  } finally {
    ms.stop();
    ms.shutdown();
  }
  //When we call stop, at most two sources will be consumed by each sink thread.
  List<MetricsRecord> mr1 = r1.getAllValues();
  List<MetricsRecord> mr2 = r2.getAllValues();
  checkMetricsRecords(mr1);
  assertEquals("output", mr1, mr2);

}
 
Example 28
Source Project: hadoop   Source File: TestMetricsSystemImpl.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void putMetrics(MetricsRecord record) {
  // No need to hang every time, just the first record.
  if (!firstTime) {
    gotCalledSecondTime = true;
    return;
  }
  firstTime = false;
  try {
    Thread.sleep(10 * 1000);
  } catch (InterruptedException ex) {
    interrupted = true;
  }
}
 
Example 29
Source Project: hadoop   Source File: TestGangliaMetrics.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testTagsForPrefix() throws Exception {
  ConfigBuilder cb = new ConfigBuilder()
    .add("test.sink.ganglia.tagsForPrefix.all", "*")
    .add("test.sink.ganglia.tagsForPrefix.some", "NumActiveSinks, " +
            "NumActiveSources")
    .add("test.sink.ganglia.tagsForPrefix.none", "");
  GangliaSink30 sink = new GangliaSink30();
  sink.init(cb.subset("test.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());
}
 
Example 30
Source Project: hadoop   Source File: TestGraphiteMetrics.java    License: Apache License 2.0 5 votes vote down vote up
@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"));
}