use of co.cask.cdap.api.metrics.MetricValue in project cdap by caskdata.
the class AggregatedMetricsEmitter method emit.
@Override
public MetricValue emit() {
// todo CDAP-2195 - potential race condition , reseting value and type has to be done together
long value = this.value.getAndSet(0);
MetricType type = gaugeUsed.getAndSet(false) ? MetricType.GAUGE : MetricType.COUNTER;
return new MetricValue(name, type, value);
}
use of co.cask.cdap.api.metrics.MetricValue in project cdap by caskdata.
the class DefaultMetricStore method add.
@Override
public void add(Collection<? extends MetricValues> metricValues) throws Exception {
List<CubeFact> facts = Lists.newArrayListWithCapacity(metricValues.size());
for (MetricValues metricValue : metricValues) {
String scope = metricValue.getTags().get(Constants.Metrics.Tag.SCOPE);
List<Measurement> metrics = Lists.newArrayList();
// todo improve this logic?
for (MetricValue metric : metricValue.getMetrics()) {
String measureName = (scope == null ? "system." : scope + ".") + metric.getName();
MeasureType type = metric.getType() == MetricType.COUNTER ? MeasureType.COUNTER : MeasureType.GAUGE;
metrics.add(new Measurement(measureName, type, metric.getValue()));
}
CubeFact fact = new CubeFact(metricValue.getTimestamp()).addDimensionValues(metricValue.getTags()).addMeasurements(metrics);
facts.add(fact);
}
cube.get().add(facts);
}
use of co.cask.cdap.api.metrics.MetricValue in project cdap by caskdata.
the class AggregatedMetricsCollectionService method getMetrics.
private Iterator<MetricValues> getMetrics(final long timestamp) {
// NOTE : emitters.asMap does not reset the access time in cache,
// so it's the preferred way to access the cache entries. as we access and emit metrics every second.
final Iterator<Map.Entry<Map<String, String>, LoadingCache<String, AggregatedMetricsEmitter>>> iterator = emitters.asMap().entrySet().iterator();
return new AbstractIterator<MetricValues>() {
@Override
protected MetricValues computeNext() {
while (iterator.hasNext()) {
Map.Entry<Map<String, String>, LoadingCache<String, AggregatedMetricsEmitter>> entry = iterator.next();
Map<String, AggregatedMetricsEmitter> metricEmitters = entry.getValue().asMap();
// +1 because we add extra metric about how many metric values did we emit in this context (see below)
List<MetricValue> metricValues = Lists.newArrayListWithCapacity(metricEmitters.size() + 1);
for (Map.Entry<String, AggregatedMetricsEmitter> emitterEntry : metricEmitters.entrySet()) {
MetricValue metricValue = emitterEntry.getValue().emit();
// skip increment by 0
if (metricValue.getType() == MetricType.COUNTER && metricValue.getValue() == 0) {
continue;
}
metricValues.add(metricValue);
}
if (metricValues.isEmpty()) {
// skip if there are no metric values to send
continue;
}
// number of emitted metrics
metricValues.add(new MetricValue("metrics.emitted.count", MetricType.COUNTER, metricValues.size() + 1));
LOG.trace("Emit metric {}", metricValues);
return new MetricValues(entry.getKey(), timestamp, metricValues);
}
return endOfData();
}
};
}
use of co.cask.cdap.api.metrics.MetricValue in project cdap by caskdata.
the class MessagingMetricsProcessorService method persistMetrics.
/**
* Persist metrics into metric store
*
* @param metricValues a non-empty deque of {@link MetricValues}
*/
private void persistMetrics(Deque<MetricValues> metricValues, Map<TopicIdMetaKey, TopicProcessMeta> topicProcessMetaMap) throws Exception {
long now = System.currentTimeMillis();
long lastMetricTime = metricValues.peekLast().getTimestamp();
List<MetricValue> topicLevelDelays = new ArrayList<>();
// add topic level delay metrics
for (Map.Entry<TopicIdMetaKey, TopicProcessMeta> entry : topicProcessMetaMap.entrySet()) {
TopicProcessMeta topicProcessMeta = entry.getValue();
long delay = now - TimeUnit.SECONDS.toMillis(topicProcessMeta.getOldestMetricsTimestamp());
topicLevelDelays.add(new MetricValue(topicProcessMeta.getOldestMetricsTimestampMetricName(), MetricType.GAUGE, delay));
delay = now - TimeUnit.SECONDS.toMillis(topicProcessMeta.getLatestMetricsTimestamp());
topicLevelDelays.add(new MetricValue(topicProcessMeta.getLatestMetricsTimestampMetricName(), MetricType.GAUGE, delay));
}
List<MetricValue> processorMetrics = new ArrayList<>(topicLevelDelays);
processorMetrics.add(new MetricValue(processMetricName, MetricType.COUNTER, metricValues.size()));
metricValues.add(new MetricValues(metricsContextMap, TimeUnit.MILLISECONDS.toSeconds(now), processorMetrics));
metricStore.add(metricValues);
metricsProcessedCount += metricValues.size();
PROGRESS_LOG.debug("{} metrics persisted. Last metric's timestamp: {}", metricsProcessedCount, lastMetricTime);
}
use of co.cask.cdap.api.metrics.MetricValue in project cdap by caskdata.
the class MessagingMetricsProcessorService method persistMetrics.
/**
* Persist metrics into metric store
*
* @param metricValues a non-empty deque of {@link MetricValues}
*/
private void persistMetrics(Deque<MetricValues> metricValues) throws Exception {
long now = System.currentTimeMillis();
long lastMetricTime = metricValues.peekLast().getTimestamp();
long delay = now - TimeUnit.SECONDS.toMillis(lastMetricTime);
metricValues.add(new MetricValues(metricsContextMap, TimeUnit.MILLISECONDS.toSeconds(now), ImmutableList.of(new MetricValue(processMetricName, MetricType.COUNTER, metricValues.size()), new MetricValue(delayMetricName, MetricType.GAUGE, delay))));
metricStore.add(metricValues);
metricsProcessedCount += metricValues.size();
PROGRESS_LOG.debug("{} metrics metrics persisted. Last metric metric's timestamp: {}. " + "Metrics process delay: {}ms", metricsProcessedCount, lastMetricTime, delay);
}
Aggregations