org.apache.hadoop.metrics2.MetricsTag Java Examples

The following examples show how to use org.apache.hadoop.metrics2.MetricsTag. 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 File: MetricsSourceAdapter.java    From hadoop with Apache License 2.0 6 votes vote down vote up
Iterable<MetricsRecordImpl> getMetrics(MetricsCollectorImpl builder,
                                       boolean all) {
  builder.setRecordFilter(recordFilter).setMetricFilter(metricFilter);
  synchronized(this) {
    if (lastRecs == null && jmxCacheTS == 0) {
      all = true; // Get all the metrics to populate the sink caches
    }
  }
  try {
    source.getMetrics(builder, all);
  }
  catch (Exception e) {
    LOG.error("Error getting metrics from source "+ name, e);
  }
  for (MetricsRecordBuilderImpl rb : builder) {
    for (MetricsTag t : injectedTags) {
      rb.add(t);
    }
  }
  synchronized(this) {
    lastRecs = builder.getRecords();
    return lastRecs;
  }
}
 
Example #2
Source File: MetricsCache.java    From hadoop with 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 #3
Source File: MBeanInfoBuilder.java    From big-c with Apache License 2.0 6 votes vote down vote up
MBeanInfo get() {
  curRecNo = 0;
  for (MetricsRecordImpl rec : recs) {
    for (MetricsTag t : rec.tags()) {
      attrs.add(newAttrInfo("tag."+ t.name(), t.description(),
                "java.lang.String"));
    }
    for (AbstractMetric m : rec.metrics()) {
      m.visit(this);
    }
    ++curRecNo;
  }
  MetricsSystemImpl.LOG.debug(attrs);
  MBeanAttributeInfo[] attrsArray = new MBeanAttributeInfo[attrs.size()];
  return new MBeanInfo(name, description, attrs.toArray(attrsArray),
                       null, null, null); // no ops/ctors/notifications
}
 
Example #4
Source File: MBeanInfoBuilder.java    From hadoop with Apache License 2.0 6 votes vote down vote up
MBeanInfo get() {
  curRecNo = 0;
  for (MetricsRecordImpl rec : recs) {
    for (MetricsTag t : rec.tags()) {
      attrs.add(newAttrInfo("tag."+ t.name(), t.description(),
                "java.lang.String"));
    }
    for (AbstractMetric m : rec.metrics()) {
      m.visit(this);
    }
    ++curRecNo;
  }
  MetricsSystemImpl.LOG.debug(attrs);
  MBeanAttributeInfo[] attrsArray = new MBeanAttributeInfo[attrs.size()];
  return new MBeanInfo(name, description, attrs.toArray(attrsArray),
                       null, null, null); // no ops/ctors/notifications
}
 
Example #5
Source File: MetricsSourceAdapter.java    From big-c with Apache License 2.0 6 votes vote down vote up
Iterable<MetricsRecordImpl> getMetrics(MetricsCollectorImpl builder,
                                       boolean all) {
  builder.setRecordFilter(recordFilter).setMetricFilter(metricFilter);
  synchronized(this) {
    if (lastRecs == null && jmxCacheTS == 0) {
      all = true; // Get all the metrics to populate the sink caches
    }
  }
  try {
    source.getMetrics(builder, all);
  }
  catch (Exception e) {
    LOG.error("Error getting metrics from source "+ name, e);
  }
  for (MetricsRecordBuilderImpl rb : builder) {
    for (MetricsTag t : injectedTags) {
      rb.add(t);
    }
  }
  synchronized(this) {
    lastRecs = builder.getRecords();
    return lastRecs;
  }
}
 
Example #6
Source File: MetricsSourceAdapter.java    From big-c with Apache License 2.0 6 votes vote down vote up
private int updateAttrCache() {
  LOG.debug("Updating attr cache...");
  int recNo = 0;
  int numMetrics = 0;
  for (MetricsRecordImpl record : lastRecs) {
    for (MetricsTag t : record.tags()) {
      setAttrCacheTag(t, recNo);
      ++numMetrics;
    }
    for (AbstractMetric m : record.metrics()) {
      setAttrCacheMetric(m, recNo);
      ++numMetrics;
    }
    ++recNo;
  }
  LOG.debug("Done. # tags & metrics="+ numMetrics);
  return numMetrics;
}
 
Example #7
Source File: GangliaSink30.java    From hadoop with 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 #8
Source File: FileSink.java    From hadoop with 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 #9
Source File: TestMetricsSystemImpl.java    From hadoop with 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 #10
Source File: GangliaSink30.java    From big-c with 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 #11
Source File: MetricsCache.java    From big-c with 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 #12
Source File: HadoopTimelineMetricsSink.java    From ambari-metrics with 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 #13
Source File: AbstractPatternFilter.java    From big-c with Apache License 2.0 6 votes vote down vote up
@Override
public boolean accepts(MetricsTag tag) {
  // Accept if whitelisted
  Pattern ipat = includeTagPatterns.get(tag.name());
  if (ipat != null && ipat.matcher(tag.value()).matches()) {
    return true;
  }
  // Reject if blacklisted
  Pattern epat = excludeTagPatterns.get(tag.name());
  if (epat != null && epat.matcher(tag.value()).matches()) {
    return false;
  }
  // Reject if no match in whitelist only mode
  if (!includeTagPatterns.isEmpty() && excludeTagPatterns.isEmpty()) {
    return false;
  }
  return true;
}
 
Example #14
Source File: TestInterns.java    From big-c with Apache License 2.0 6 votes vote down vote up
@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"));
}
 
Example #15
Source File: MetricsRecords.java    From hadoop with 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 #16
Source File: TestPatternFilter.java    From big-c with Apache License 2.0 5 votes vote down vote up
private static void shouldAcceptImpl(final boolean expectAcceptList,  
    SubsetConfiguration conf, List<MetricsTag> tags, boolean[] expectedAcceptedSpec) {
  final MetricsFilter globFilter = newGlobFilter(conf);
  final MetricsFilter regexFilter = newRegexFilter(conf);
  
  // Test acceptance of the tag list:  
  assertEquals("accepts "+ tags, expectAcceptList, globFilter.accepts(tags));
  assertEquals("accepts "+ tags, expectAcceptList, regexFilter.accepts(tags));
  
  // Test results on each of the individual tags:
  int acceptedCount = 0;
  for (int i=0; i<tags.size(); i++) {
    MetricsTag tag = tags.get(i);
    boolean actGlob = globFilter.accepts(tag);
    boolean actRegex = regexFilter.accepts(tag);
    assertEquals("accepts "+tag, expectedAcceptedSpec[i], actGlob);
    // Both the filters should give the same result:
    assertEquals(actGlob, actRegex);
    if (actGlob) {
      acceptedCount++;
    }
  }
  if (expectAcceptList) {
    // At least one individual tag should be accepted:
    assertTrue("No tag of the following accepted: " + tags, acceptedCount > 0);
  } else {
    // At least one individual tag should be rejected: 
    assertTrue("No tag of the following rejected: " + tags, acceptedCount < tags.size());
  }
}
 
Example #17
Source File: MetricsRegistry.java    From big-c with Apache License 2.0 5 votes vote down vote up
/**
 * Sample all the mutable metrics and put the snapshot in the builder
 * @param builder to contain the metrics snapshot
 * @param all get all the metrics even if the values are not changed.
 */
public synchronized void snapshot(MetricsRecordBuilder builder, boolean all) {
  for (MetricsTag tag : tags()) {
    builder.add(tag);
  }
  for (MutableMetric metric : metrics()) {
    metric.snapshot(builder, all);
  }
}
 
Example #18
Source File: DynamicMetricsRegistry.java    From hbase with Apache License 2.0 5 votes vote down vote up
/**
 * Sample all the mutable metrics and put the snapshot in the builder
 * @param builder to contain the metrics snapshot
 * @param all get all the metrics even if the values are not changed.
 */
public void snapshot(MetricsRecordBuilder builder, boolean all) {
  for (MetricsTag tag : tags()) {
    builder.add(tag);
  }
  for (MutableMetric metric : metrics()) {
    metric.snapshot(builder, all);
  }
}
 
Example #19
Source File: TestMetricsCache.java    From big-c with Apache License 2.0 5 votes vote down vote up
@Test public void testOverflow() {
  MetricsCache cache = new MetricsCache();
  MetricsCache.Record cr;
  Collection<MetricsTag> t0 = Arrays.asList(makeTag("t0", "0"));
  for (int i = 0; i < MetricsCache.MAX_RECS_PER_NAME_DEFAULT + 1; ++i) {
    cr = cache.update(makeRecord("r",
        Arrays.asList(makeTag("t"+ i, ""+ i)),
        Arrays.asList(makeMetric("m", i))));
    checkMetricValue("new metric value", cr, "m", i);
    if (i < MetricsCache.MAX_RECS_PER_NAME_DEFAULT) {
      assertNotNull("t0 is still there", cache.get("r", t0));
    }
  }
  assertNull("t0 is gone", cache.get("r", t0));
}
 
Example #20
Source File: TestMetricsCache.java    From hadoop with Apache License 2.0 5 votes vote down vote up
@Test public void testOverflow() {
  MetricsCache cache = new MetricsCache();
  MetricsCache.Record cr;
  Collection<MetricsTag> t0 = Arrays.asList(makeTag("t0", "0"));
  for (int i = 0; i < MetricsCache.MAX_RECS_PER_NAME_DEFAULT + 1; ++i) {
    cr = cache.update(makeRecord("r",
        Arrays.asList(makeTag("t"+ i, ""+ i)),
        Arrays.asList(makeMetric("m", i))));
    checkMetricValue("new metric value", cr, "m", i);
    if (i < MetricsCache.MAX_RECS_PER_NAME_DEFAULT) {
      assertNotNull("t0 is still there", cache.get("r", t0));
    }
  }
  assertNull("t0 is gone", cache.get("r", t0));
}
 
Example #21
Source File: TestMetricsCache.java    From hadoop with 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 #22
Source File: PhoenixMetricsSink.java    From phoenix with Apache License 2.0 5 votes vote down vote up
private void addDynamicEntry(List<String> keys, List<Object> values,
        List<String> variableValues, String family, MetricsTag tag,
        MetricInfo metric, int count) {
    // <family><.dynColumn><count> <VARCHAR>
    keys.add(getDynamicColumnName(family, metric.columnName, count) + " VARCHAR");

    // build the annotation value
    String val = tag.description() + " - " + tag.value();
    values.add(VARIABLE_VALUE);
    variableValues.add(val);
}
 
Example #23
Source File: TestMetricsSourceAdapter.java    From big-c with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetMetricsAndJmx() throws Exception {
  // create test source with a single metric counter of value 0
  TestSource source = new TestSource("test");
  MetricsSourceBuilder sb = MetricsAnnotations.newSourceBuilder(source);
  final MetricsSource s = sb.build();

  List<MetricsTag> injectedTags = new ArrayList<MetricsTag>();
  MetricsSourceAdapter sa = new MetricsSourceAdapter(
      "test", "test", "test desc", s, injectedTags, null, null, 1, false);

  // all metrics are initially assumed to have changed
  MetricsCollectorImpl builder = new MetricsCollectorImpl();
  Iterable<MetricsRecordImpl> metricsRecords = sa.getMetrics(builder, true);

  // Validate getMetrics and JMX initial values
  MetricsRecordImpl metricsRecord = metricsRecords.iterator().next();
  assertEquals(0L,
      metricsRecord.metrics().iterator().next().value().longValue());

  Thread.sleep(100); // skip JMX cache TTL
  assertEquals(0L, (Number)sa.getAttribute("C1"));

  // change metric value
  source.incrementCnt();

  // validate getMetrics and JMX
  builder = new MetricsCollectorImpl();
  metricsRecords = sa.getMetrics(builder, true);
  metricsRecord = metricsRecords.iterator().next();
  assertTrue(metricsRecord.metrics().iterator().hasNext());
  Thread.sleep(100); // skip JMX cache TTL
  assertEquals(1L, (Number)sa.getAttribute("C1"));
}
 
Example #24
Source File: MetricsRecords.java    From big-c with 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 #25
Source File: AzureBlobStorageTestAccount.java    From big-c with 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 #26
Source File: TestGangliaMetrics.java    From hadoop with 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 #27
Source File: TestMetricsSourceAdapter.java    From hadoop with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetMetricsAndJmx() throws Exception {
  // create test source with a single metric counter of value 0
  TestSource source = new TestSource("test");
  MetricsSourceBuilder sb = MetricsAnnotations.newSourceBuilder(source);
  final MetricsSource s = sb.build();

  List<MetricsTag> injectedTags = new ArrayList<MetricsTag>();
  MetricsSourceAdapter sa = new MetricsSourceAdapter(
      "test", "test", "test desc", s, injectedTags, null, null, 1, false);

  // all metrics are initially assumed to have changed
  MetricsCollectorImpl builder = new MetricsCollectorImpl();
  Iterable<MetricsRecordImpl> metricsRecords = sa.getMetrics(builder, true);

  // Validate getMetrics and JMX initial values
  MetricsRecordImpl metricsRecord = metricsRecords.iterator().next();
  assertEquals(0L,
      metricsRecord.metrics().iterator().next().value().longValue());

  Thread.sleep(100); // skip JMX cache TTL
  assertEquals(0L, (Number)sa.getAttribute("C1"));

  // change metric value
  source.incrementCnt();

  // validate getMetrics and JMX
  builder = new MetricsCollectorImpl();
  metricsRecords = sa.getMetrics(builder, true);
  metricsRecord = metricsRecords.iterator().next();
  assertTrue(metricsRecord.metrics().iterator().hasNext());
  Thread.sleep(100); // skip JMX cache TTL
  assertEquals(1L, (Number)sa.getAttribute("C1"));
}
 
Example #28
Source File: PhoenixMetricsSink.java    From phoenix with Apache License 2.0 5 votes vote down vote up
private void addDynamicEntry(List<String> keys, List<Object> values,
        List<String> variableValues, String family, MetricsTag tag,
        MetricInfo metric, int count) {
    // <family><.dynColumn><count> <VARCHAR>
    keys.add(getDynamicColumnName(family, metric.columnName, count) + " VARCHAR");

    // build the annotation value
    String val = tag.description() + " - " + tag.value();
    values.add(VARIABLE_VALUE);
    variableValues.add(val);
}
 
Example #29
Source File: TestGraphiteMetrics.java    From hadoop with Apache License 2.0 5 votes vote down vote up
@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"));
}
 
Example #30
Source File: TestPatternFilter.java    From hadoop with Apache License 2.0 5 votes vote down vote up
/**
 * 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;
}