use of io.cdap.cdap.api.metrics.MetricValue in project cdap by caskdata.
the class DefaultMetricStore method add.
@Override
public void add(Collection<? extends MetricValues> metricValues) {
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();
if (metric.getType() == MetricType.DISTRIBUTION) {
// https://cdap.atlassian.net/browse/CDAP-18769
continue;
}
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 io.cdap.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) {
long now = System.currentTimeMillis();
long lastMetricTime = metricValues.peekLast().getTimestamp();
List<MetricValue> topicLevelDelays = new ArrayList<>();
// write topic level delay metrics
for (TopicProcessMeta topicProcessMeta : metadataHandler.getCache().values()) {
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));
metricsWriter.write(metricValues);
metricsProcessedCount += metricValues.size();
PROGRESS_LOG.debug("{} metrics persisted with {}. Last metric's timestamp: {}", metricsProcessedCount, metricsWriter.getID(), lastMetricTime);
}
use of io.cdap.cdap.api.metrics.MetricValue in project cdap by caskdata.
the class AbstractMetricsPublisher method publish.
/**
* Function that adds timestamp and tags to collection of {@link MetricValue} to
* convert it into a Collection of {@link MetricValues} and calls the
* overloaded publish method with the Collection of {@link MetricValues}.
*
* @param metrics List of {@link MetricValue} to be published.
* @param tags Map of tags that specify the context of the metrics that are published.
* @throws Exception When the publisher isn't initialized or an exception is raised during the publishing
* process.
*/
@Override
public void publish(Collection<MetricValue> metrics, Map<String, String> tags) throws Exception {
Collection<MetricValues> metricValues = new ArrayList<>();
for (MetricValue metric : metrics) {
long now = System.currentTimeMillis();
metricValues.add(new MetricValues(tags, metric.getName(), TimeUnit.MILLISECONDS.toSeconds(now), metric.getValue(), metric.getType()));
}
this.publish(metricValues);
}
use of io.cdap.cdap.api.metrics.MetricValue in project cdap by caskdata.
the class DistributionTest method testAggregatedEmitterConcurrency.
// TODO mark as slow test
@Test
public void testAggregatedEmitterConcurrency() throws InterruptedException {
AggregatedMetricsEmitter emitter = new AggregatedMetricsEmitter("ignore");
AtomicBoolean end = new AtomicBoolean(false);
LongAdder totalAdds = new LongAdder();
LongAdder totalEmitCount = new LongAdder();
Thread emittingThread = new Thread(() -> {
for (int i = 0; i < 10; i++) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
MetricValue metricValue = emitter.emit();
totalEmitCount.add(Arrays.stream(metricValue.getBucketCounts()).sum());
}
end.set(true);
});
Runnable runnable = () -> {
while (!end.get()) {
emitter.event(1);
totalAdds.add(1);
}
};
Thread addingThread1 = new Thread(runnable);
Thread addingThread2 = new Thread(runnable);
addingThread1.start();
addingThread2.start();
emittingThread.start();
emittingThread.join();
addingThread1.join();
addingThread2.join();
// final emit
MetricValue metricValue = emitter.emit();
totalEmitCount.add(Arrays.stream(metricValue.getBucketCounts()).sum());
Assert.assertEquals(totalAdds.longValue(), totalEmitCount.longValue());
System.out.println(String.format("totalAdds: %d", totalAdds.longValue()));
}
use of io.cdap.cdap.api.metrics.MetricValue in project cdap by caskdata.
the class JMXMetricsCollector method runOneIteration.
@Override
protected void runOneIteration() {
Collection<MetricValue> metrics = new ArrayList<>();
try (JMXConnector jmxConnector = JMXConnectorFactory.connect(serviceUrl, null)) {
MBeanServerConnection mBeanConn = jmxConnector.getMBeanServerConnection();
metrics.addAll(getMemoryMetrics(mBeanConn));
metrics.addAll(getCPUMetrics(mBeanConn));
metrics.addAll(getThreadMetrics(mBeanConn));
} catch (IOException e) {
LOG.error("Error occurred while connecting to JMX server.", e);
return;
}
try {
this.metricsPublisher.publish(metrics, this.metricTags);
} catch (Exception e) {
LOG.warn("Error occurred while publishing resource usage metrics.", e);
}
}
Aggregations