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();
}
}
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);
}
}
};
}
};
}
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
}
}
}
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);
}
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();
}
Aggregations