Search in sources :

Example 1 with MetricValue

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

Example 2 with MetricValue

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);
}
Also used : MetricValue(io.cdap.cdap.api.metrics.MetricValue) ArrayList(java.util.ArrayList) MetricValues(io.cdap.cdap.api.metrics.MetricValues)

Example 3 with MetricValue

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);
}
Also used : MetricValue(io.cdap.cdap.api.metrics.MetricValue) ArrayList(java.util.ArrayList) MetricValues(io.cdap.cdap.api.metrics.MetricValues)

Example 4 with MetricValue

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()));
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) MetricValue(io.cdap.cdap.api.metrics.MetricValue) LongAdder(java.util.concurrent.atomic.LongAdder) Test(org.junit.Test)

Example 5 with MetricValue

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);
    }
}
Also used : MetricValue(io.cdap.cdap.api.metrics.MetricValue) JMXConnector(javax.management.remote.JMXConnector) ArrayList(java.util.ArrayList) IOException(java.io.IOException) MBeanServerConnection(javax.management.MBeanServerConnection) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException)

Aggregations

MetricValue (io.cdap.cdap.api.metrics.MetricValue)16 MetricValues (io.cdap.cdap.api.metrics.MetricValues)9 ArrayList (java.util.ArrayList)7 ImmutableMap (com.google.common.collect.ImmutableMap)3 Map (java.util.Map)3 LoadingCache (com.google.common.cache.LoadingCache)1 AbstractIterator (com.google.common.collect.AbstractIterator)1 CubeFact (io.cdap.cdap.api.dataset.lib.cube.CubeFact)1 MeasureType (io.cdap.cdap.api.dataset.lib.cube.MeasureType)1 Measurement (io.cdap.cdap.api.dataset.lib.cube.Measurement)1 IOException (java.io.IOException)1 MemoryMXBean (java.lang.management.MemoryMXBean)1 MemoryUsage (java.lang.management.MemoryUsage)1 OperatingSystemMXBean (java.lang.management.OperatingSystemMXBean)1 ThreadMXBean (java.lang.management.ThreadMXBean)1 MalformedURLException (java.net.MalformedURLException)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 TreeMap (java.util.TreeMap)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1