Search in sources :

Example 1 with MetricValue

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);
}
Also used : MetricValue(co.cask.cdap.api.metrics.MetricValue) MetricType(co.cask.cdap.api.metrics.MetricType)

Example 2 with MetricValue

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);
}
Also used : Measurement(co.cask.cdap.api.dataset.lib.cube.Measurement) CubeFact(co.cask.cdap.api.dataset.lib.cube.CubeFact) MetricValue(co.cask.cdap.api.metrics.MetricValue) MeasureType(co.cask.cdap.api.dataset.lib.cube.MeasureType) MetricValues(co.cask.cdap.api.metrics.MetricValues)

Example 3 with MetricValue

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();
        }
    };
}
Also used : MetricValues(co.cask.cdap.api.metrics.MetricValues) MetricValue(co.cask.cdap.api.metrics.MetricValue) LoadingCache(com.google.common.cache.LoadingCache) AbstractIterator(com.google.common.collect.AbstractIterator) ImmutableMap(com.google.common.collect.ImmutableMap) Map(java.util.Map)

Example 4 with MetricValue

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);
}
Also used : MetricValue(co.cask.cdap.api.metrics.MetricValue) ArrayList(java.util.ArrayList) MetricValues(co.cask.cdap.api.metrics.MetricValues) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap)

Example 5 with MetricValue

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);
}
Also used : MetricValue(co.cask.cdap.api.metrics.MetricValue) MetricValues(co.cask.cdap.api.metrics.MetricValues)

Aggregations

MetricValue (co.cask.cdap.api.metrics.MetricValue)7 MetricValues (co.cask.cdap.api.metrics.MetricValues)6 Map (java.util.Map)3 ImmutableMap (com.google.common.collect.ImmutableMap)2 CubeFact (co.cask.cdap.api.dataset.lib.cube.CubeFact)1 MeasureType (co.cask.cdap.api.dataset.lib.cube.MeasureType)1 Measurement (co.cask.cdap.api.dataset.lib.cube.Measurement)1 MetricType (co.cask.cdap.api.metrics.MetricType)1 LoadingCache (com.google.common.cache.LoadingCache)1 AbstractIterator (com.google.common.collect.AbstractIterator)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 ConcurrentMap (java.util.concurrent.ConcurrentMap)1