Hi,

Here is the first PR to support metric tag (protobuf and collector):
https://github.com/apache/incubator-heron/pull/3750/files

There are still a few things to dig out to finalize the Metric classes, but
this is what in my mind right now.

public class CountMetricWithTag implements IMetric<CountMetric> {
  private Map<String, CountMetric> taggedCounts = new HashMap<>();

  public CountMetricWithTag() {
  }

  public void incr(String... tags) {
        incrBy(1, tags);
  }

  /**
   * Increment the metrics with optional tags.
   * Tags are comma separated strings: "tagName:tagValue".
   * For example: "device:ios", and "endpoint:v2".
   * Normally tag names and values should have limited values,
   * otherwise, like "id:192.168.0.123", the memory utililization
   * and the cost of tracking metrics could increase dramatically.
   * @param tags optional comma separated tags, like "device:ios",
"endpoint:v2".
   *   Normally tag names and values should have limited values,
   */
  public void incrBy(long incrementBy, String... tags) {
    if (tags.length > 0) {
      for (String tag : tags) {
        incrTaggedCountBy(tag, incrementBy);
      }
    } else {
        incrTaggedCountBy("", incrementBy);
    }
  }

  private void incrTaggedCountBy(String tag, long incrementBy) {
    if (!taggedCounts.containsKey(tag)) {
        taggedCounts.put(tag, new CountMetric());
    }
    taggedCounts.get(tag).incrBy(incrementBy);
  }

  @Override
  public CountMetric getValueAndReset() {
    return null; // Not needed. `getTaggedMetricsAndReset` should be used
instead.
  }

  @Override
  public Map<String, CountMetric> getTaggedMetricsAndReset() {
    Map<String, CountMetric> ret = taggedCounts;
    taggedCounts = new HashMap<>();
    return ret;
  }
}

Reply via email to