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