Search in sources :

Example 16 with MetricsContext

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

the class HttpHandlerGeneratorTest method testContentConsumer.

@Test
public void testContentConsumer() throws Exception {
    MetricsContext noOpsMetricsContext = new NoOpMetricsCollectionService().getContext(new HashMap<String, String>());
    HttpHandlerFactory factory = new HttpHandlerFactory("/content", noOpsMetricsContext);
    // Create the file upload handler and starts a netty server with it
    final File outputDir = TEMP_FOLDER.newFolder();
    HttpHandler httpHandler = factory.createHttpHandler(TypeToken.of(FileHandler.class), new AbstractDelegatorContext<FileHandler>() {

        @Override
        protected FileHandler createHandler() {
            return new FileHandler(outputDir);
        }
    });
    // Creates a Netty http server with 1K request buffer
    NettyHttpService service = NettyHttpService.builder().addHttpHandlers(ImmutableList.of(httpHandler)).setHttpChunkLimit(1024).build();
    service.startAndWait();
    try {
        InetSocketAddress bindAddress = service.getBindAddress();
        testUpload(outputDir, bindAddress, "");
        testUpload(outputDir, bindAddress, "-no-tx");
    } finally {
        service.stopAndWait();
    }
}
Also used : HttpHandler(co.cask.http.HttpHandler) InetSocketAddress(java.net.InetSocketAddress) MetricsContext(co.cask.cdap.api.metrics.MetricsContext) NettyHttpService(co.cask.http.NettyHttpService) NoOpMetricsCollectionService(co.cask.cdap.common.metrics.NoOpMetricsCollectionService) File(java.io.File) Test(org.junit.Test)

Example 17 with MetricsContext

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

the class StreamHandler method createStreamMetricsCollectorFactory.

private StreamMetricsCollectorFactory createStreamMetricsCollectorFactory() {
    return new StreamMetricsCollectorFactory() {

        @Override
        public StreamMetricsCollector createMetricsCollector(final StreamId streamId) {
            MetricsContext streamMetricsContext = streamMetricsCollectors.getUnchecked(streamId.getParent());
            final MetricsContext childCollector = streamMetricsContext.childContext(Constants.Metrics.Tag.STREAM, streamId.getEntityName());
            return new StreamMetricsCollector() {

                @Override
                public void emitMetrics(long bytesWritten, long eventsWritten) {
                    if (bytesWritten > 0) {
                        childCollector.increment("collect.bytes", bytesWritten);
                        sizeCollector.received(streamId, bytesWritten);
                    }
                    if (eventsWritten > 0) {
                        childCollector.increment("collect.events", eventsWritten);
                    }
                }
            };
        }
    };
}
Also used : StreamId(co.cask.cdap.proto.id.StreamId) MetricsContext(co.cask.cdap.api.metrics.MetricsContext)

Example 18 with MetricsContext

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

the class HBaseDatasetMetricsReporter method report.

private void report(Map<TableId, HBaseTableUtil.TableStats> tableStats) throws IOException {
    Map<String, String> reverseNamespaceMap = hBaseTableUtil.getHBaseToCDAPNamespaceMap();
    for (Map.Entry<TableId, HBaseTableUtil.TableStats> statEntry : tableStats.entrySet()) {
        String hbaseNamespace = statEntry.getKey().getNamespace();
        String cdapNamespace = reverseNamespaceMap.get(hbaseNamespace);
        // tableNames that doesn't start with user are ignored
        if (NamespaceId.SYSTEM.getNamespace().equals(cdapNamespace)) {
            continue;
        }
        String tableName = statEntry.getKey().getTableName();
        try {
            Collection<DatasetSpecificationSummary> instances = dsFramework.getInstances(new NamespaceId(cdapNamespace));
            for (DatasetSpecificationSummary spec : instances) {
                DatasetSpecification specification = dsFramework.getDatasetSpec(new DatasetId(cdapNamespace, spec.getName()));
                if (specification.isParent(tableName)) {
                    MetricsContext collector = metricsService.getContext(ImmutableMap.of(Constants.Metrics.Tag.NAMESPACE, cdapNamespace, Constants.Metrics.Tag.DATASET, spec.getName()));
                    collector.gauge("dataset.size.mb", statEntry.getValue().getTotalSizeMB());
                    break;
                }
            }
        } catch (DatasetManagementException | ServiceUnavailableException e) {
        // No op
        }
    }
}
Also used : TableId(co.cask.cdap.data2.util.TableId) MetricsContext(co.cask.cdap.api.metrics.MetricsContext) DatasetSpecification(co.cask.cdap.api.dataset.DatasetSpecification) ServiceUnavailableException(co.cask.cdap.common.ServiceUnavailableException) DatasetSpecificationSummary(co.cask.cdap.proto.DatasetSpecificationSummary) DatasetId(co.cask.cdap.proto.id.DatasetId) DatasetManagementException(co.cask.cdap.api.dataset.DatasetManagementException) NamespaceId(co.cask.cdap.proto.id.NamespaceId) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 19 with MetricsContext

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

the class AbstractHttpHandlerDelegator method wrapResponder.

/**
   * Returns a new instance of {@link DelayedHttpServiceResponder} that wraps around the given {@link HttpResponder}
   * object. This method is called from handler class generated by {@link HttpHandlerGenerator}.
   */
@SuppressWarnings("unused")
protected final DelayedHttpServiceResponder wrapResponder(HttpResponder responder) {
    MetricsContext collector = this.metricsContext;
    HttpServiceContext serviceContext = context.getServiceContext();
    Preconditions.checkState(serviceContext instanceof TransactionalHttpServiceContext, "This instance of HttpServiceContext does not support transactions.");
    if (serviceContext.getSpecification() != null) {
        collector = metricsContext.childContext(Constants.Metrics.Tag.HANDLER, serviceContext.getSpecification().getName());
    }
    return new DelayedHttpServiceResponder(responder, new BodyProducerFactory() {

        @Override
        public BodyProducer create(HttpContentProducer contentProducer, TransactionalHttpServiceContext serviceContext) {
            final ClassLoader programContextClassLoader = new CombineClassLoader(null, ImmutableList.of(contentProducer.getClass().getClassLoader(), getClass().getClassLoader()));
            // Capture the context since we need to keep it until the end of the content producing.
            // We don't need to worry about double capturing of the context when HttpContentConsumer is used.
            // This is because when HttpContentConsumer is used, the responder constructed here will get closed and this
            // BodyProducerFactory won't be used.
            final Cancellable contextReleaser = context.capture();
            return new BodyProducerAdapter(contentProducer, serviceContext, programContextClassLoader, contextReleaser);
        }
    }, (TransactionalHttpServiceContext) serviceContext, collector);
}
Also used : BodyProducer(co.cask.http.BodyProducer) CombineClassLoader(co.cask.cdap.common.lang.CombineClassLoader) HttpServiceContext(co.cask.cdap.api.service.http.HttpServiceContext) Cancellable(org.apache.twill.common.Cancellable) MetricsContext(co.cask.cdap.api.metrics.MetricsContext) HttpContentProducer(co.cask.cdap.api.service.http.HttpContentProducer) CombineClassLoader(co.cask.cdap.common.lang.CombineClassLoader)

Example 20 with MetricsContext

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

the class HttpHandlerGenerator method generateConstructor.

/**
   * Generates the constructor. The constructor generated has signature {@code (DelegatorContext, MetricsContext)}.
   */
private void generateConstructor(TypeToken<? extends HttpServiceHandler> delegateType, ClassWriter classWriter) {
    Method constructor = Methods.getMethod(void.class, "<init>", DelegatorContext.class, MetricsContext.class);
    String signature = Signatures.getMethodSignature(constructor, TypeToken.of(void.class), getContextType(delegateType), TypeToken.of(MetricsContext.class));
    // Constructor(DelegatorContext, MetricsContext)
    GeneratorAdapter mg = new GeneratorAdapter(Opcodes.ACC_PUBLIC, constructor, signature, null, classWriter);
    // super(context, metricsContext);
    mg.loadThis();
    mg.loadArg(0);
    mg.loadArg(1);
    mg.invokeConstructor(Type.getType(AbstractHttpHandlerDelegator.class), Methods.getMethod(void.class, "<init>", DelegatorContext.class, MetricsContext.class));
    mg.returnValue();
    mg.endMethod();
}
Also used : MetricsContext(co.cask.cdap.api.metrics.MetricsContext) GeneratorAdapter(org.objectweb.asm.commons.GeneratorAdapter) Method(org.objectweb.asm.commons.Method)

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