@Override
protected void publish(MetricsScope scope, Iterator<MetricsRecord> metrics) throws Exception {
while (metrics.hasNext()) {
MetricsRecord record = metrics.next();
String context = record.getContext();
// Context is expected to look like appId.b.programId.[m|r].[taskId]
String counterGroup;
String contextParts[] = splitPattern.split(context);
//TODO: Refactor to support any context
if (context.equals(Constants.Metrics.DATASET_CONTEXT)) {
counterGroup = "cdap.dataset";
} else if ("m".equals(contextParts[3])) {
counterGroup = "cdap.mapper";
} else if ("r".equals(contextParts[3])) {
counterGroup = "cdap.reducer";
} else {
LOG.error("could not determine if the metric is a map or reduce metric from context {}, skipping...", context);
continue;
}
counterGroup += "." + scope.name();
String counterName = getCounterName(record.getName());
taskContext.getCounter(counterGroup, counterName).increment(record.getValue());
for (TagMetric tag : record.getTags()) {
counterName = getCounterName(record.getName(), tag.getTag());
if (counterName != null) {
taskContext.getCounter(counterGroup, counterName).increment(tag.getValue());
}
}
}