Search in sources :

Example 11 with MetricValue

use of io.cdap.cdap.api.metrics.MetricValue in project cdap by cdapio.

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 12 with MetricValue

use of io.cdap.cdap.api.metrics.MetricValue in project cdap by cdapio.

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 13 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)

Example 14 with MetricValue

use of io.cdap.cdap.api.metrics.MetricValue in project cdap by caskdata.

the class JMXMetricsCollector method getMemoryMetrics.

Collection<MetricValue> getMemoryMetrics(MBeanServerConnection mBeanConn) throws IOException {
    MemoryMXBean mxBean = ManagementFactory.newPlatformMXBeanProxy(mBeanConn, ManagementFactory.MEMORY_MXBEAN_NAME, MemoryMXBean.class);
    MemoryUsage heapMemoryUsage = mxBean.getHeapMemoryUsage();
    Collection<MetricValue> metrics = new ArrayList<>();
    metrics.add(new MetricValue(Constants.Metrics.JVMResource.HEAP_USED_MB, MetricType.GAUGE, heapMemoryUsage.getUsed() / MEGA_BYTE));
    if (heapMemoryUsage.getMax() >= 0) {
        metrics.add(new MetricValue(Constants.Metrics.JVMResource.HEAP_MAX_MB, MetricType.GAUGE, heapMemoryUsage.getMax() / MEGA_BYTE));
    }
    return metrics;
}
Also used : MetricValue(io.cdap.cdap.api.metrics.MetricValue) MemoryMXBean(java.lang.management.MemoryMXBean) ArrayList(java.util.ArrayList) MemoryUsage(java.lang.management.MemoryUsage)

Example 15 with MetricValue

use of io.cdap.cdap.api.metrics.MetricValue in project cdap by caskdata.

the class JMXMetricsCollector method getCPUMetrics.

Collection<MetricValue> getCPUMetrics(MBeanServerConnection conn) throws IOException {
    OperatingSystemMXBean mxBean = ManagementFactory.newPlatformMXBeanProxy(conn, ManagementFactory.OPERATING_SYSTEM_MXBEAN_NAME, OperatingSystemMXBean.class);
    Collection<MetricValue> metrics = new ArrayList<>();
    double systemLoad = mxBean.getSystemLoadAverage();
    if (systemLoad < 0) {
        LOG.info("CPU load for JVM process is not yet available");
    } else {
        double processorCount = mxBean.getAvailableProcessors();
        double systemLoadPerProcessorScaled = (systemLoad * SYSTEM_LOAD_SCALING_FACTOR) / processorCount;
        metrics.add(new MetricValue(Constants.Metrics.JVMResource.SYSTEM_LOAD_PER_PROCESSOR_SCALED, MetricType.GAUGE, (long) systemLoadPerProcessorScaled));
    }
    return metrics;
}
Also used : MetricValue(io.cdap.cdap.api.metrics.MetricValue) ArrayList(java.util.ArrayList) OperatingSystemMXBean(java.lang.management.OperatingSystemMXBean)

Aggregations

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