public MetricValues(Map<String, String> tags, String name, long timestamp, long value, MetricType type) { this.tags = tags; this.timestamp = timestamp; this.metrics = ImmutableList.of(new MetricValue(name, type, value)); }
@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); }
@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); }
/** * Persist metrics into metric store * * @param metricValues a non-empty deque of {@link MetricValues} */ private void persistMetrics(Deque<MetricValues> metricValues, Map<TopicIdMetaKey, TopicProcessMeta> topicProcessMetaMap) { 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); }
/** * Persist metrics into metric store * * @param metricValues a non-empty deque of {@link MetricValues} */ private void persistMetrics(Deque<MetricValues> metricValues, Map<TopicIdMetaKey, TopicProcessMeta> topicProcessMetaMap) { 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); }
@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(); } };
@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(); } };