Search in sources :

Example 26 with MetricValues

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

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;
                    }
                    if (metricValue.getType() == MetricType.DISTRIBUTION && metricValue.getBucketCounts().length == 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(io.cdap.cdap.api.metrics.MetricValues) MetricValue(io.cdap.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 27 with MetricValues

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

the class MessagingMetricsCollectionService method publish.

@Override
protected void publish(Iterator<MetricValues> metrics) throws Exception {
    int size = topicPayloads.size();
    while (metrics.hasNext()) {
        encoderOutputStream.reset();
        MetricValues metricValues = metrics.next();
        // Encode MetricValues into bytes
        recordWriter.encode(metricValues, encoder);
        TopicPayload topicPayload = topicPayloads.get(Math.abs(metricValues.getTags().hashCode() % size));
        // Calculate the topic number with the hashcode of MetricValues' tags and store the encoded payload in the
        // corresponding list of the topic number
        topicPayload.addPayload(encoderOutputStream.toByteArray(), metricValues.getTags(), metricValues.getMetrics().size());
    }
    publishMetric(topicPayloads.values());
}
Also used : MetricValues(io.cdap.cdap.api.metrics.MetricValues)

Example 28 with MetricValues

use of io.cdap.cdap.api.metrics.MetricValues 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 29 with MetricValues

use of io.cdap.cdap.api.metrics.MetricValues 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 30 with MetricValues

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

the class TaskWorkerMetricsTest method testWrappedRequest.

@Test
public void testWrappedRequest() throws IOException {
    String taskClassName = TaskWorkerServiceTest.TestRunnableClass.class.getName();
    String wrappedClassName = "testClassName";
    RunnableTaskRequest req = RunnableTaskRequest.getBuilder(taskClassName).withParam("100").withEmbeddedTaskRequest(RunnableTaskRequest.getBuilder(wrappedClassName).build()).build();
    String reqBody = GSON.toJson(req);
    HttpResponse response = HttpRequests.execute(HttpRequest.post(uri.resolve("/v3Internal/worker/run").toURL()).withBody(reqBody).build(), new DefaultHttpRequestConfig(false));
    TaskWorkerTestUtil.waitForServiceCompletion(taskWorkerStateFuture);
    Assert.assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());
    Assert.assertSame(1, published.size());
    // check the metrics are present
    MetricValues metricValues = published.get(0);
    Assert.assertTrue(hasMetric(metricValues, Constants.Metrics.TaskWorker.REQUEST_COUNT));
    Assert.assertTrue(hasMetric(metricValues, Constants.Metrics.TaskWorker.REQUEST_LATENCY_MS));
    // check the clz tag is set correctly
    Assert.assertEquals(wrappedClassName, metricValues.getTags().get(Constants.Metrics.Tag.CLASS));
}
Also used : DefaultHttpRequestConfig(io.cdap.cdap.common.http.DefaultHttpRequestConfig) HttpResponse(io.cdap.common.http.HttpResponse) MetricValues(io.cdap.cdap.api.metrics.MetricValues) RunnableTaskRequest(io.cdap.cdap.api.service.worker.RunnableTaskRequest) Test(org.junit.Test)

Aggregations

MetricValues (io.cdap.cdap.api.metrics.MetricValues)56 Test (org.junit.Test)28 MetricValue (io.cdap.cdap.api.metrics.MetricValue)20 RunnableTaskRequest (io.cdap.cdap.api.service.worker.RunnableTaskRequest)12 ArrayList (java.util.ArrayList)12 Map (java.util.Map)10 ImmutableMap (com.google.common.collect.ImmutableMap)8 DefaultHttpRequestConfig (io.cdap.cdap.common.http.DefaultHttpRequestConfig)6 HttpResponse (io.cdap.common.http.HttpResponse)6 Iterator (java.util.Iterator)6 MetricsContext (io.cdap.cdap.api.metrics.MetricsContext)4 CConfiguration (io.cdap.cdap.common.conf.CConfiguration)4 AggregatedMetricsCollectionService (io.cdap.cdap.metrics.collect.AggregatedMetricsCollectionService)4 IOException (java.io.IOException)4 HashMap (java.util.HashMap)4 LinkedBlockingQueue (java.util.concurrent.LinkedBlockingQueue)4 Before (org.junit.Before)4 Category (org.junit.experimental.categories.Category)4 MetricDeleteQuery (io.cdap.cdap.api.metrics.MetricDeleteQuery)3 LinkedHashMap (java.util.LinkedHashMap)3