use of co.cask.cdap.api.metrics.MetricsContext in project cdap by caskdata.
the class DefaultServiceConfigurer method createHttpHandler.
private <T extends HttpServiceHandler> HttpHandler createHttpHandler(T handler) {
MetricsContext noOpsMetricsContext = new NoOpMetricsCollectionService().getContext(new HashMap<String, String>());
HttpHandlerFactory factory = new HttpHandlerFactory("", noOpsMetricsContext);
@SuppressWarnings("unchecked") TypeToken<T> type = (TypeToken<T>) TypeToken.of(handler.getClass());
return factory.createHttpHandler(type, new VerificationDelegateContext<>(handler));
}
use of co.cask.cdap.api.metrics.MetricsContext in project cdap by caskdata.
the class MetricsHandlerTestRun method setupMetrics.
private static void setupMetrics() throws Exception {
// Adding metrics for app "WordCount1" in namespace "myspace", "WCount1" in "yourspace"
MetricsContext collector = collectionService.getContext(getFlowletContext("myspace", "WordCount1", "WordCounter", "run1", "splitter"));
collector.increment("reads", 1);
collector.increment("writes", 1);
collector = collectionService.getContext(getFlowletContext("yourspace", "WCount1", "WordCounter", "run1", "splitter"));
collector.increment("reads", 1);
collector = collectionService.getContext(getFlowletContext("yourspace", "WCount1", "WCounter", "run1", "splitter"));
emitTs = System.currentTimeMillis();
// we want to emit in two different seconds
// todo : figure out why we need this
TimeUnit.SECONDS.sleep(1);
collector.increment("reads", 1);
TimeUnit.MILLISECONDS.sleep(2000);
collector.increment("reads", 2);
collector = collectionService.getContext(getFlowletContext("yourspace", "WCount1", "WCounter", "run1", "counter"));
collector.increment("reads", 1);
collector = collectionService.getContext(getMapReduceTaskContext("yourspace", "WCount1", "ClassicWordCount", MapReduceMetrics.TaskType.Mapper, "run1", "task1"));
collector.increment("reads", 1);
collector = collectionService.getContext(getMapReduceTaskContext("yourspace", "WCount1", "ClassicWordCount", MapReduceMetrics.TaskType.Reducer, "run1", "task2"));
collector.increment("reads", 1);
collector = collectionService.getContext(getFlowletContext("myspace", "WordCount1", "WordCounter", "run1", "splitter"));
collector.increment("reads", 1);
collector.increment("writes", 1);
collector = collectionService.getContext(getFlowletContext("myspace", "WordCount1", "WordCounter", "run1", "collector"));
collector.increment("aa", 1);
collector.increment("zz", 1);
collector.increment("ab", 1);
collector = collectionService.getContext(getWorkerContext("yourspace", "WCount1", "WorkerWordCount", "run1", "task1"));
collector.increment("workerreads", 5);
collector.increment("workerwrites", 6);
collector = collectionService.getContext(getWorkerContext("yourspace", "WCount1", "WorkerWordCount", "run2", "task1"));
collector.increment("workerreads", 5);
collector.increment("workerwrites", 6);
// also: user metrics
Metrics userMetrics = new ProgramUserMetrics(collectionService.getContext(getFlowletContext("myspace", "WordCount1", "WordCounter", "run1", "splitter")));
userMetrics.count("reads", 1);
userMetrics.count("writes", 2);
collector = collectionService.getContext(new HashMap<String, String>());
collector.increment("resources.total.storage", 10);
// need a better way to do this
TimeUnit.SECONDS.sleep(2);
}
use of co.cask.cdap.api.metrics.MetricsContext in project cdap by caskdata.
the class MapReduceMetricsWriter method reportMapTaskMetrics.
private void reportMapTaskMetrics(TaskReport taskReport) {
Counters counters = taskReport.getTaskCounters();
MetricsContext metricsContext = mapTaskMetricsCollectors.getUnchecked(taskReport.getTaskId());
metricsContext.gauge(MapReduceMetrics.METRIC_TASK_INPUT_RECORDS, getTaskCounter(counters, TaskCounter.MAP_INPUT_RECORDS));
metricsContext.gauge(MapReduceMetrics.METRIC_TASK_OUTPUT_RECORDS, getTaskCounter(counters, TaskCounter.MAP_OUTPUT_RECORDS));
metricsContext.gauge(MapReduceMetrics.METRIC_TASK_BYTES, getTaskCounter(counters, TaskCounter.MAP_OUTPUT_BYTES));
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 HttpHandlerGeneratorTest method testContentProducer.
@Test
public void testContentProducer() 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);
}
});
NettyHttpService service = NettyHttpService.builder().addHttpHandlers(ImmutableList.of(httpHandler)).build();
service.startAndWait();
try {
// Generate a 100K file
File file = TEMP_FOLDER.newFile();
Files.write(Strings.repeat("0123456789", 10240).getBytes(Charsets.UTF_8), file);
InetSocketAddress bindAddress = service.getBindAddress();
// Upload the generated file
URL uploadURL = new URL(String.format("http://%s:%d/content/upload/test.txt", bindAddress.getHostName(), bindAddress.getPort()));
HttpURLConnection urlConn = (HttpURLConnection) uploadURL.openConnection();
try {
urlConn.setDoOutput(true);
urlConn.setRequestMethod("PUT");
Files.copy(file, urlConn.getOutputStream());
Assert.assertEquals(200, urlConn.getResponseCode());
} finally {
urlConn.disconnect();
}
// Download the file
File downloadFile = TEMP_FOLDER.newFile();
urlConn = (HttpURLConnection) new URL(String.format("http://%s:%d/content/download/test.txt", bindAddress.getHostName(), bindAddress.getPort())).openConnection();
try {
ByteStreams.copy(urlConn.getInputStream(), Files.newOutputStreamSupplier(downloadFile));
} finally {
urlConn.disconnect();
}
// Compare if the file content are the same
Assert.assertTrue(Files.equal(file, downloadFile));
// Download a file that doesn't exist
urlConn = (HttpURLConnection) new URL(String.format("http://%s:%d/content/download/test2.txt", bindAddress.getHostName(), bindAddress.getPort())).openConnection();
try {
Assert.assertEquals(500, urlConn.getResponseCode());
} finally {
urlConn.disconnect();
}
// Upload the file to the POST endpoint. The endpoint should response with the same file content
downloadFile = TEMP_FOLDER.newFile();
urlConn = (HttpURLConnection) uploadURL.openConnection();
try {
urlConn.setDoOutput(true);
urlConn.setRequestMethod("POST");
Files.copy(file, urlConn.getOutputStream());
ByteStreams.copy(urlConn.getInputStream(), Files.newOutputStreamSupplier(downloadFile));
Assert.assertEquals(200, urlConn.getResponseCode());
Assert.assertTrue(Files.equal(file, downloadFile));
} finally {
urlConn.disconnect();
}
} finally {
service.stopAndWait();
}
}
use of co.cask.cdap.api.metrics.MetricsContext in project cdap by caskdata.
the class HttpHandlerGeneratorTest method testHttpHandlerGenerator.
@Test
public void testHttpHandlerGenerator() 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();
}
});
HttpHandler httpHandlerWithoutAnnotation = factory.createHttpHandler(TypeToken.of(NoAnnotationHandler.class), new AbstractDelegatorContext<NoAnnotationHandler>() {
@Override
protected NoAnnotationHandler createHandler() {
return new NoAnnotationHandler();
}
});
NettyHttpService service = NettyHttpService.builder().addHttpHandlers(ImmutableList.of(httpHandler, httpHandlerWithoutAnnotation)).build();
service.startAndWait();
try {
InetSocketAddress bindAddress = service.getBindAddress();
// Make a GET call
URLConnection urlConn = new URL(String.format("http://%s:%d/prefix/p2/handle", bindAddress.getHostName(), bindAddress.getPort())).openConnection();
urlConn.setReadTimeout(2000);
Assert.assertEquals("Hello World", new String(ByteStreams.toByteArray(urlConn.getInputStream()), Charsets.UTF_8));
// Make a POST call
urlConn = new URL(String.format("http://%s:%d/prefix/p2/echo/test", bindAddress.getHostName(), bindAddress.getPort())).openConnection();
urlConn.setReadTimeout(2000);
urlConn.setDoOutput(true);
ByteStreams.copy(ByteStreams.newInputStreamSupplier("Hello".getBytes(Charsets.UTF_8)), urlConn.getOutputStream());
Assert.assertEquals("Hello test", new String(ByteStreams.toByteArray(urlConn.getInputStream()), Charsets.UTF_8));
// Ensure that even though the handler did not have a class-level annotation, we still prefix the path that it
// handles by "/prefix"
urlConn = new URL(String.format("http://%s:%d/prefix/ping", bindAddress.getHostName(), bindAddress.getPort())).openConnection();
urlConn.setReadTimeout(2000);
Assert.assertEquals("OK", new String(ByteStreams.toByteArray(urlConn.getInputStream()), Charsets.UTF_8));
} finally {
service.stopAndWait();
}
}
Aggregations