Search in sources :

Example 1 with MetricsContext

use of co.cask.cdap.api.metrics.MetricsContext in project cdap by caskdata.

the class MapReduceMetricsWriter method reportReduceTaskMetrics.

private void reportReduceTaskMetrics(TaskReport taskReport) {
    Counters counters = taskReport.getTaskCounters();
    MetricsContext metricsContext = reduceTaskMetricsCollectors.getUnchecked(taskReport.getTaskId());
    metricsContext.gauge(MapReduceMetrics.METRIC_TASK_INPUT_RECORDS, getTaskCounter(counters, TaskCounter.REDUCE_INPUT_RECORDS));
    metricsContext.gauge(MapReduceMetrics.METRIC_TASK_OUTPUT_RECORDS, getTaskCounter(counters, TaskCounter.REDUCE_OUTPUT_RECORDS));
    metricsContext.gauge(MapReduceMetrics.METRIC_TASK_COMPLETION, (long) (taskReport.getProgress() * 100));
}
Also used : MetricsContext(co.cask.cdap.api.metrics.MetricsContext) Counters(org.apache.hadoop.mapreduce.Counters)

Example 2 with MetricsContext

use of co.cask.cdap.api.metrics.MetricsContext in project cdap by caskdata.

the class FlowletProgramRunner method outputEmitterFactory.

private OutputEmitterFactory outputEmitterFactory(final BasicFlowletContext flowletContext, final String flowletName, final QueueClientFactory queueClientFactory, final ImmutableList.Builder<ProducerSupplier> producerBuilder, final Table<Node, String, Set<QueueSpecification>> queueSpecs) {
    return new OutputEmitterFactory() {

        @Override
        public <T> OutputEmitter<T> create(String outputName, TypeToken<T> type) {
            try {
                // first iterate over all queue specifications to find the queue name and all consumer flowlet ids
                QueueName queueName = null;
                List<String> consumerFlowlets = Lists.newLinkedList();
                Node flowlet = Node.flowlet(flowletName);
                Schema schema = schemaGenerator.generate(type.getType());
                for (Map.Entry<String, Set<QueueSpecification>> entry : queueSpecs.row(flowlet).entrySet()) {
                    for (QueueSpecification queueSpec : entry.getValue()) {
                        if (queueSpec.getQueueName().getSimpleName().equals(outputName) && queueSpec.getOutputSchema().equals(schema)) {
                            queueName = queueSpec.getQueueName();
                            consumerFlowlets.add(entry.getKey());
                            break;
                        }
                    }
                }
                if (queueName == null) {
                    throw new IllegalArgumentException(String.format("No queue specification found for %s, %s", flowletName, type));
                }
                // create a metric collector for this queue, and also one for each consumer flowlet
                final MetricsContext metrics = flowletContext.getProgramMetrics().childContext(Constants.Metrics.Tag.FLOWLET_QUEUE, outputName);
                final MetricsContext producerMetrics = metrics.childContext(Constants.Metrics.Tag.PRODUCER, flowletContext.getFlowletId());
                final Iterable<MetricsContext> consumerMetrics = Iterables.transform(consumerFlowlets, new Function<String, MetricsContext>() {

                    @Override
                    public MetricsContext apply(String consumer) {
                        return producerMetrics.childContext(Constants.Metrics.Tag.CONSUMER, consumer);
                    }
                });
                // create a queue metrics emitter that emit to all of the above collectors
                ProducerSupplier producerSupplier = new ProducerSupplier(queueName, queueClientFactory, new QueueMetrics() {

                    @Override
                    public void emitEnqueue(int count) {
                        metrics.increment("process.events.out", count);
                        for (MetricsContext collector : consumerMetrics) {
                            collector.increment("queue.pending", count);
                        }
                    }

                    @Override
                    public void emitEnqueueBytes(int bytes) {
                    // no-op
                    }
                });
                producerBuilder.add(producerSupplier);
                return new DatumOutputEmitter<>(producerSupplier, schema, datumWriterFactory.create(type, schema));
            } catch (Exception e) {
                throw Throwables.propagate(e);
            }
        }
    };
}
Also used : Set(java.util.Set) ImmutableSet(com.google.common.collect.ImmutableSet) Node(co.cask.cdap.app.queue.QueueSpecificationGenerator.Node) Schema(co.cask.cdap.api.data.schema.Schema) MetricsContext(co.cask.cdap.api.metrics.MetricsContext) UnsupportedTypeException(co.cask.cdap.api.data.schema.UnsupportedTypeException) IOException(java.io.IOException) QueueMetrics(co.cask.cdap.data2.transaction.queue.QueueMetrics) TypeToken(com.google.common.reflect.TypeToken) QueueSpecification(co.cask.cdap.app.queue.QueueSpecification) QueueName(co.cask.cdap.common.queue.QueueName) Map(java.util.Map)

Example 3 with MetricsContext

use of co.cask.cdap.api.metrics.MetricsContext in project cdap by caskdata.

the class HttpHandlerGeneratorTest method testHttpHeaders.

@Test
public void testHttpHeaders() throws Exception {
    MetricsContext noOpsMetricsContext = new NoOpMetricsCollectionService().getContext(new HashMap<String, String>());
    HttpHandlerFactory factory = new HttpHandlerFactory("/prefix", noOpsMetricsContext);
    HttpHandler httpHandler = factory.createHttpHandler(TypeToken.of(MyHttpHandler.class), new AbstractDelegatorContext<MyHttpHandler>() {

        @Override
        protected MyHttpHandler createHandler() {
            return new MyHttpHandler();
        }
    });
    NettyHttpService service = NettyHttpService.builder().addHttpHandlers(ImmutableList.of(httpHandler)).build();
    service.startAndWait();
    try {
        InetSocketAddress bindAddress = service.getBindAddress();
        // Make a request with headers that the response should carry first value for each header name
        HttpURLConnection urlConn = (HttpURLConnection) new URL(String.format("http://%s:%d/prefix/p2/echo/firstHeaders", bindAddress.getHostName(), bindAddress.getPort())).openConnection();
        urlConn.addRequestProperty("k1", "v1");
        urlConn.addRequestProperty("k1", "v2");
        urlConn.addRequestProperty("k2", "v2");
        Assert.assertEquals(200, urlConn.getResponseCode());
        Map<String, List<String>> headers = urlConn.getHeaderFields();
        Assert.assertEquals(ImmutableList.of("v1"), headers.get("k1"));
        Assert.assertEquals(ImmutableList.of("v2"), headers.get("k2"));
        // Make a request with headers that the response should carry all values for each header name
        urlConn = (HttpURLConnection) new URL(String.format("http://%s:%d/prefix/p2/echo/allHeaders", bindAddress.getHostName(), bindAddress.getPort())).openConnection();
        urlConn.addRequestProperty("k1", "v1");
        urlConn.addRequestProperty("k1", "v2");
        urlConn.addRequestProperty("k1", "v3");
        urlConn.addRequestProperty("k2", "v2");
        Assert.assertEquals(200, urlConn.getResponseCode());
        headers = urlConn.getHeaderFields();
        // URLConnection always reverse the ordering of the header values.
        Assert.assertEquals(ImmutableList.of("v3", "v2", "v1"), headers.get("k1"));
        Assert.assertEquals(ImmutableList.of("v2"), headers.get("k2"));
    } finally {
        service.stopAndWait();
    }
}
Also used : HttpHandler(co.cask.http.HttpHandler) InetSocketAddress(java.net.InetSocketAddress) MetricsContext(co.cask.cdap.api.metrics.MetricsContext) NoOpMetricsCollectionService(co.cask.cdap.common.metrics.NoOpMetricsCollectionService) URL(java.net.URL) HttpURLConnection(java.net.HttpURLConnection) NettyHttpService(co.cask.http.NettyHttpService) List(java.util.List) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) Test(org.junit.Test)

Example 4 with MetricsContext

use of co.cask.cdap.api.metrics.MetricsContext in project cdap by caskdata.

the class LevelDBDatasetMetricsReporter method report.

private void report(Map<TableId, LevelDBTableService.TableStats> datasetStat) throws DatasetManagementException {
    for (Map.Entry<TableId, LevelDBTableService.TableStats> statEntry : datasetStat.entrySet()) {
        String namespace = statEntry.getKey().getNamespace();
        // emit metrics for only user datasets, tables in system namespace are ignored
        if (NamespaceId.SYSTEM.getNamespace().equals(namespace)) {
            continue;
        }
        String tableName = statEntry.getKey().getTableName();
        Collection<DatasetSpecificationSummary> instances = dsFramework.getInstances(new NamespaceId(namespace));
        for (DatasetSpecificationSummary spec : instances) {
            DatasetSpecification specification = dsFramework.getDatasetSpec(new DatasetId(namespace, spec.getName()));
            if (specification.isParent(tableName)) {
                MetricsContext collector = metricsService.getContext(ImmutableMap.of(Constants.Metrics.Tag.NAMESPACE, namespace, Constants.Metrics.Tag.DATASET, spec.getName()));
                int sizeInMb = (int) (statEntry.getValue().getDiskSizeBytes() / BYTES_IN_MB);
                collector.gauge("dataset.size.mb", sizeInMb);
                break;
            }
        }
    }
}
Also used : TableId(co.cask.cdap.data2.util.TableId) MetricsContext(co.cask.cdap.api.metrics.MetricsContext) DatasetSpecification(co.cask.cdap.api.dataset.DatasetSpecification) NamespaceId(co.cask.cdap.proto.id.NamespaceId) DatasetSpecificationSummary(co.cask.cdap.proto.DatasetSpecificationSummary) ImmutableMap(com.google.common.collect.ImmutableMap) Map(java.util.Map) DatasetId(co.cask.cdap.proto.id.DatasetId)

Example 5 with MetricsContext

use of co.cask.cdap.api.metrics.MetricsContext in project cdap by caskdata.

the class MetricsReporterHook method preCall.

@Override
public boolean preCall(HttpRequest request, HttpResponder responder, HandlerInfo handlerInfo) {
    if (metricsCollectionService != null) {
        try {
            MetricsContext collector = collectorCache.get(createContext(handlerInfo));
            collector.increment("request.received", 1);
        } catch (Throwable e) {
            LOG.error("Got exception while getting collector", e);
        }
    }
    return true;
}
Also used : MetricsContext(co.cask.cdap.api.metrics.MetricsContext)

Aggregations

MetricsContext (co.cask.cdap.api.metrics.MetricsContext)20 NoOpMetricsCollectionService (co.cask.cdap.common.metrics.NoOpMetricsCollectionService)5 Test (org.junit.Test)5 HttpHandler (co.cask.http.HttpHandler)4 NettyHttpService (co.cask.http.NettyHttpService)4 InetSocketAddress (java.net.InetSocketAddress)4 Map (java.util.Map)4 ImmutableMap (com.google.common.collect.ImmutableMap)3 HttpURLConnection (java.net.HttpURLConnection)3 URL (java.net.URL)3 DatasetSpecification (co.cask.cdap.api.dataset.DatasetSpecification)2 QueueSpecification (co.cask.cdap.app.queue.QueueSpecification)2 QueueName (co.cask.cdap.common.queue.QueueName)2 TableId (co.cask.cdap.data2.util.TableId)2 DatasetSpecificationSummary (co.cask.cdap.proto.DatasetSpecificationSummary)2 DatasetId (co.cask.cdap.proto.id.DatasetId)2 NamespaceId (co.cask.cdap.proto.id.NamespaceId)2 TypeToken (com.google.common.reflect.TypeToken)2 File (java.io.File)2 HashMap (java.util.HashMap)2