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

The following examples show how to use org.apache.hadoop.metrics2.AbstractMetric. 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: 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 2
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 3
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 4
Source Project: hadoop   Source File: MBeanInfoBuilder.java    License: 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 Project: hadoop   Source File: MetricsSourceAdapter.java    License: 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 6
Source Project: hadoop   Source File: MetricsRecordFiltered.java    License: Apache License 2.0 6 votes vote down vote up
@Override public Iterable<AbstractMetric> metrics() {
  return new Iterable<AbstractMetric>() {
    final Iterator<AbstractMetric> it = delegate.metrics().iterator();
    @Override public Iterator<AbstractMetric> iterator() {
      return new AbstractIterator<AbstractMetric>() {
        @Override public AbstractMetric computeNext() {
          while (it.hasNext()) {
            AbstractMetric next = it.next();
            if (filter.accepts(next.name())) {
              return next;
            }
          }
          return endOfData();
        }
      };
    }
  };
}
 
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: 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 9
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 10
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 11
Source Project: big-c   Source File: MBeanInfoBuilder.java    License: 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 12
Source Project: big-c   Source File: MetricsSourceAdapter.java    License: 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 13
Source Project: big-c   Source File: MetricsRecordFiltered.java    License: Apache License 2.0 6 votes vote down vote up
@Override public Iterable<AbstractMetric> metrics() {
  return new Iterable<AbstractMetric>() {
    final Iterator<AbstractMetric> it = delegate.metrics().iterator();
    @Override public Iterator<AbstractMetric> iterator() {
      return new AbstractIterator<AbstractMetric>() {
        @Override public AbstractMetric computeNext() {
          while (it.hasNext()) {
            AbstractMetric next = it.next();
            if (filter.accepts(next.name())) {
              return next;
            }
          }
          return endOfData();
        }
      };
    }
  };
}
 
Example 14
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 15
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 16
Source Project: phoenix   Source File: PhoenixMetricsIT.java    License: Apache License 2.0 6 votes vote down vote up
private boolean verifyMetricsFromSinkOnce(Map<String, Long> expectedMetrics) {
    synchronized (GlobalPhoenixMetricsTestSink.lock) {
        for (AbstractMetric metric : GlobalPhoenixMetricsTestSink.metrics) {
            if (expectedMetrics.containsKey(metric.name())) {
                Long value = expectedMetrics.get(metric.name());
                if (value != null) {
                    long expectedValue = value;
                    long actualValue = metric.value().longValue();
                    if (expectedValue != actualValue) {
                        LOGGER.warn("Metric from Hadoop Sink: "
                                + metric.name() + " didn't match expected.");
                        return false;
                    }
                    expectedMetrics.remove(metric.name());
                }
            }
        }
    }
    assertTrue("Metric expected but not present in Hadoop Metrics Sink (GlobalPhoenixMetricsTestSink)",
            expectedMetrics.size() == 0);
    return true;
}
 
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 (!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 18
Source Project: hadoop   Source File: MetricsCache.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * @deprecated use metricsEntrySet() instead
 * @return entry set of metrics
 */
@Deprecated
public Set<Map.Entry<String, Number>> metrics() {
  Map<String, Number> map = new LinkedHashMap<String, Number>(
      metrics.size());
  for (Map.Entry<String, AbstractMetric> mapEntry : metrics.entrySet()) {
    map.put(mapEntry.getKey(), mapEntry.getValue().value());
  }
  return map.entrySet();
}
 
Example 19
Source Project: hadoop   Source File: MetricsRecordImpl.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Construct a metrics record
 * @param info  {@link MetricInfo} of the record
 * @param timestamp of the record
 * @param tags  of the record
 * @param metrics of the record
 */
public MetricsRecordImpl(MetricsInfo info, long timestamp,
                         List<MetricsTag> tags,
                         Iterable<AbstractMetric> metrics) {
  this.timestamp = checkArg(timestamp, timestamp > 0, "timestamp");
  this.info = checkNotNull(info, "info");
  this.tags = checkNotNull(tags, "tags");
  this.metrics = checkNotNull(metrics, "metrics");
}
 
Example 20
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 21
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 22
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 23
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 24
Source Project: hadoop   Source File: TestMetricsVisitor.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Test the common use cases
 */
@Test public void testCommon() {
  MetricsVisitor visitor = mock(MetricsVisitor.class);
  MetricsRegistry registry = new MetricsRegistry("test");
  List<AbstractMetric> metrics = MetricsLists.builder("test")
      .addCounter(info("c1", "int counter"), 1)
      .addCounter(info("c2", "long counter"), 2L)
      .addGauge(info("g1", "int gauge"), 5)
      .addGauge(info("g2", "long gauge"), 6L)
      .addGauge(info("g3", "float gauge"), 7f)
      .addGauge(info("g4", "double gauge"), 8d)
      .metrics();

  for (AbstractMetric metric : metrics) {
    metric.visit(visitor);
  }

  verify(visitor).counter(c1.capture(), eq(1));
  assertEquals("c1 name", "c1", c1.getValue().name());
  assertEquals("c1 description", "int counter", c1.getValue().description());
  verify(visitor).counter(c2.capture(), eq(2L));
  assertEquals("c2 name", "c2", c2.getValue().name());
  assertEquals("c2 description", "long counter", c2.getValue().description());
  verify(visitor).gauge(g1.capture(), eq(5));
  assertEquals("g1 name", "g1", g1.getValue().name());
  assertEquals("g1 description", "int gauge", g1.getValue().description());
  verify(visitor).gauge(g2.capture(), eq(6L));
  assertEquals("g2 name", "g2", g2.getValue().name());
  assertEquals("g2 description", "long gauge", g2.getValue().description());
  verify(visitor).gauge(g3.capture(), eq(7f));
  assertEquals("g3 name", "g3", g3.getValue().name());
  assertEquals("g3 description", "float gauge", g3.getValue().description());
  verify(visitor).gauge(g4.capture(), eq(8d));
  assertEquals("g4 name", "g4", g4.getValue().name());
  assertEquals("g4 description", "double gauge", g4.getValue().description());
}
 
Example 25
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"));
}
 
Example 26
Source Project: hadoop   Source File: TestGraphiteMetrics.java    License: Apache License 2.0 5 votes vote down vote up
@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"));
}
 
Example 27
Source Project: hadoop   Source File: TestGraphiteMetrics.java    License: 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 28
Source Project: big-c   Source File: MetricsCache.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * @deprecated use metricsEntrySet() instead
 * @return entry set of metrics
 */
@Deprecated
public Set<Map.Entry<String, Number>> metrics() {
  Map<String, Number> map = new LinkedHashMap<String, Number>(
      metrics.size());
  for (Map.Entry<String, AbstractMetric> mapEntry : metrics.entrySet()) {
    map.put(mapEntry.getKey(), mapEntry.getValue().value());
  }
  return map.entrySet();
}
 
Example 29
Source Project: big-c   Source File: MetricsRecordImpl.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Construct a metrics record
 * @param info  {@link MetricInfo} of the record
 * @param timestamp of the record
 * @param tags  of the record
 * @param metrics of the record
 */
public MetricsRecordImpl(MetricsInfo info, long timestamp,
                         List<MetricsTag> tags,
                         Iterable<AbstractMetric> metrics) {
  this.timestamp = checkArg(timestamp, timestamp > 0, "timestamp");
  this.info = checkNotNull(info, "info");
  this.tags = checkNotNull(tags, "tags");
  this.metrics = checkNotNull(metrics, "metrics");
}
 
Example 30
Source Project: big-c   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);
}