Search in sources :

Example 1 with NoopMetricsContext

use of io.cdap.cdap.api.metrics.NoopMetricsContext in project cdap by caskdata.

the class ProgramRunners method createProgramMetricsContext.

/**
 * Create a {@link MetricsContext} for emitting program metrics.
 * @param programRunId the {@link ProgramRunId} of the current execution
 * @param metricsTags a set of extra tags to be used for creating the {@link MetricsContext}
 * @param metricsCollectionService the underlying service for metrics publishing or {@code null} to suppress metrics
 *                                 publishing
 * @return a {@link MetricsContext} for emitting metrics for the current program context.
 */
public static MetricsContext createProgramMetricsContext(ProgramRunId programRunId, Map<String, String> metricsTags, @Nullable MetricsCollectionService metricsCollectionService) {
    Map<String, String> tags = Maps.newHashMap(metricsTags);
    tags.put(Constants.Metrics.Tag.NAMESPACE, programRunId.getNamespace());
    tags.put(Constants.Metrics.Tag.APP, programRunId.getApplication());
    tags.put(ProgramTypeMetricTag.getTagName(programRunId.getType()), programRunId.getProgram());
    tags.put(Constants.Metrics.Tag.RUN_ID, programRunId.getRun());
    return metricsCollectionService == null ? new NoopMetricsContext(tags) : metricsCollectionService.getContext(tags);
}
Also used : NoopMetricsContext(io.cdap.cdap.api.metrics.NoopMetricsContext)

Example 2 with NoopMetricsContext

use of io.cdap.cdap.api.metrics.NoopMetricsContext in project cdap by caskdata.

the class HttpHandlerGeneratorTest method testHttpHandlerGenerator.

@Test
public void testHttpHandlerGenerator() throws Exception {
    HttpHandlerFactory factory = new HttpHandlerFactory("/prefix", TransactionControl.IMPLICIT);
    HttpHandler httpHandler = factory.createHttpHandler(TypeToken.of(MyHttpHandler.class), new AbstractDelegatorContext<MyHttpHandler>() {

        @Override
        protected MyHttpHandler createHandler() {
            return new MyHttpHandler();
        }
    }, new NoopMetricsContext());
    HttpHandler httpHandlerWithoutAnnotation = factory.createHttpHandler(TypeToken.of(NoAnnotationHandler.class), new AbstractDelegatorContext<NoAnnotationHandler>() {

        @Override
        protected NoAnnotationHandler createHandler() {
            return new NoAnnotationHandler();
        }
    }, new NoopMetricsContext());
    NettyHttpService service = NettyHttpService.builder("test-handler-generator").setHttpHandlers(httpHandler, httpHandlerWithoutAnnotation).build();
    service.start();
    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));
        // Call to a method that raise exception
        urlConn = new URL(String.format("http://%s:%d/prefix/p2/exception", bindAddress.getHostName(), bindAddress.getPort())).openConnection();
        Assert.assertEquals(500, ((HttpURLConnection) urlConn).getResponseCode());
        Assert.assertEquals("Exception occurred while handling request: exception", new String(ByteStreams.toByteArray(((HttpURLConnection) urlConn).getErrorStream()), "UTF-8"));
        urlConn = new URL(String.format("http://%s:%d/prefix/p2/exceptionNoTx", bindAddress.getHostName(), bindAddress.getPort())).openConnection();
        Assert.assertEquals(500, ((HttpURLConnection) urlConn).getResponseCode());
        Assert.assertEquals("Exception occurred while handling request: exceptionNoTx", new String(ByteStreams.toByteArray(((HttpURLConnection) urlConn).getErrorStream()), "UTF-8"));
    } finally {
        service.stop();
    }
}
Also used : HttpHandler(io.cdap.http.HttpHandler) InetSocketAddress(java.net.InetSocketAddress) NoopMetricsContext(io.cdap.cdap.api.metrics.NoopMetricsContext) HttpURLConnection(java.net.HttpURLConnection) URLConnection(java.net.URLConnection) URL(java.net.URL) NettyHttpService(io.cdap.http.NettyHttpService) Test(org.junit.Test)

Example 3 with NoopMetricsContext

use of io.cdap.cdap.api.metrics.NoopMetricsContext in project cdap by caskdata.

the class MetricsWritersMetricsPublisher method initializeMetricWriters.

private void initializeMetricWriters(Map<String, MetricsWriter> metricsWriters, CConfiguration cConf) {
    for (Map.Entry<String, MetricsWriter> entry : metricsWriters.entrySet()) {
        MetricsWriter writer = entry.getValue();
        // Metrics context used by MetricsStoreMetricsWriter only, which we don't use here
        // So we can pass no-op context
        DefaultMetricsWriterContext metricsWriterContext = new DefaultMetricsWriterContext(new NoopMetricsContext(), cConf, writer.getID());
        writer.initialize(metricsWriterContext);
    }
}
Also used : MetricsWriter(io.cdap.cdap.api.metrics.MetricsWriter) DefaultMetricsWriterContext(io.cdap.cdap.metrics.process.DefaultMetricsWriterContext) NoopMetricsContext(io.cdap.cdap.api.metrics.NoopMetricsContext) Map(java.util.Map)

Example 4 with NoopMetricsContext

use of io.cdap.cdap.api.metrics.NoopMetricsContext in project cdap by caskdata.

the class MessagingMetricsProcessorManagerServiceTest method persistMetricsTests.

@Test
public void persistMetricsTests() throws Exception {
    injector.getInstance(TransactionManager.class).startAndWait();
    StoreDefinition.createAllTables(injector.getInstance(StructuredTableAdmin.class));
    injector.getInstance(DatasetOpExecutorService.class).startAndWait();
    injector.getInstance(DatasetService.class).startAndWait();
    Set<Integer> partitions = IntStream.range(0, cConf.getInt(Constants.Metrics.MESSAGING_TOPIC_NUM)).boxed().collect(Collectors.toSet());
    long startTime = TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis());
    for (int iteration = 0; iteration < 50; iteration++) {
        // will fetch metrics concurrently.
        for (int i = 0; i < 50; i++) {
            // TOPIC_PREFIX + (i % PARTITION_SIZE) decides which topic the metric is published to
            publishMessagingMetrics(i, startTime, METRICS_CONTEXT, expected, "", MetricType.COUNTER);
        }
        for (int i = 50; i < 100; i++) {
            // TOPIC_PREFIX + (i % PARTITION_SIZE) decides which topic the metric is published to
            publishMessagingMetrics(i, startTime, METRICS_CONTEXT, expected, "", MetricType.GAUGE);
        }
        for (int i = 0; i < 50; i++) {
            // TOPIC_PREFIX + (i % PARTITION_SIZE) decides which topic the metric is published to
            publishMessagingMetrics(i, startTime, METRICS_CONTEXT, expected, "", MetricType.DISTRIBUTION);
        }
        final MockMetricStore metricStore = new MockMetricStore();
        // Create new MessagingMetricsProcessorManagerService instance every time because the same instance cannot be
        // started
        // again after it's stopped
        MessagingMetricsProcessorManagerService messagingMetricsProcessorManagerService = new MessagingMetricsProcessorManagerService(cConf, injector.getInstance(MetricDatasetFactory.class), messagingService, injector.getInstance(SchemaGenerator.class), injector.getInstance(DatumReaderFactory.class), metricStore, injector.getInstance(MetricsWriterProvider.class), partitions, new NoopMetricsContext(), 50, 0);
        messagingMetricsProcessorManagerService.startAndWait();
        // Wait for the 1 aggregated counter metric (with value 50) and 50 gauge metrics to be stored in the metricStore
        Tasks.waitFor(51, () -> metricStore.getAllCounterAndGaugeMetrics().size(), 15, TimeUnit.SECONDS, 100, TimeUnit.MILLISECONDS);
        assertMetricsResult(expected, metricStore.getAllCounterAndGaugeMetrics());
        // validate metrics processor metrics
        // 50 counter, 50 gauge and 50 distributed metrics are emitted in each iteration above
        Tasks.waitFor(150L, () -> metricStore.getMetricsProcessedByMetricsProcessor(), 15, TimeUnit.SECONDS, 100, TimeUnit.MILLISECONDS);
        assertDistributedMetricsResult(metricStore.getAllDistributionMetrics());
        // publish a dummy metric
        // this is to force the metrics processor to publish delay metrics for all the topics
        publishMessagingMetrics(100, startTime, METRICS_CONTEXT, expected, "", MetricType.GAUGE);
        // validate the newly published metric
        Tasks.waitFor(151L, () -> metricStore.getMetricsProcessedByMetricsProcessor(), 15, TimeUnit.SECONDS, 100, TimeUnit.MILLISECONDS);
        // in MessagingMetricsProcessorManagerService, before persisting the metrics and topic metas, a copy of the
        // topic metas
        // containing the metrics processor delay metrics is made before making a copy of metric values.
        // Therefore, there can be a very small chance where all metric values are persisted but the corresponding
        // topic metas are not yet persisted. Wait for all topic metas to be persisted
        Tasks.waitFor(true, metricStore::isMetricsProcessorDelayEmitted, 15, TimeUnit.SECONDS);
        // Clear metricStore and expected results for the next iteration
        metricStore.deleteAll();
        expected.clear();
        // Stop messagingMetricsProcessorManagerService
        messagingMetricsProcessorManagerService.stopAndWait();
    }
}
Also used : DatumReaderFactory(io.cdap.cdap.internal.io.DatumReaderFactory) StructuredTableAdmin(io.cdap.cdap.spi.data.StructuredTableAdmin) SchemaGenerator(io.cdap.cdap.internal.io.SchemaGenerator) DatasetService(io.cdap.cdap.data2.datafabric.dataset.service.DatasetService) NoopMetricsContext(io.cdap.cdap.api.metrics.NoopMetricsContext) MetricDatasetFactory(io.cdap.cdap.metrics.store.MetricDatasetFactory) MetricsWriterProvider(io.cdap.cdap.metrics.process.loader.MetricsWriterProvider) TransactionManager(org.apache.tephra.TransactionManager) DatasetOpExecutorService(io.cdap.cdap.data2.datafabric.dataset.service.executor.DatasetOpExecutorService) Test(org.junit.Test)

Example 5 with NoopMetricsContext

use of io.cdap.cdap.api.metrics.NoopMetricsContext in project cdap by caskdata.

the class MetricsProcessorServiceTest method testMetricsProcessor.

@Test
public void testMetricsProcessor() throws Exception {
    injector.getInstance(TransactionManager.class).startAndWait();
    StoreDefinition.createAllTables(injector.getInstance(StructuredTableAdmin.class));
    injector.getInstance(DatasetOpExecutorService.class).startAndWait();
    injector.getInstance(DatasetService.class).startAndWait();
    final MetricStore metricStore = injector.getInstance(MetricStore.class);
    Set<Integer> partitions = new HashSet<>();
    for (int i = 0; i < cConf.getInt(Constants.Metrics.MESSAGING_TOPIC_NUM); i++) {
        partitions.add(i);
    }
    // Start KafkaMetricsProcessorService after metrics are published to Kafka
    // Intentionally set queue size to a small value, so that MessagingMetricsProcessorManagerService
    // internally can persist metrics when more messages are to be fetched
    MessagingMetricsProcessorManagerService messagingMetricsProcessorManagerService = new MessagingMetricsProcessorManagerService(cConf, injector.getInstance(MetricDatasetFactory.class), messagingService, injector.getInstance(SchemaGenerator.class), injector.getInstance(DatumReaderFactory.class), metricStore, injector.getInstance(MetricsWriterProvider.class), partitions, new NoopMetricsContext(), 50, 0);
    messagingMetricsProcessorManagerService.startAndWait();
    long startTime = TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis());
    // Publish metrics with messaging service and record expected metrics
    for (int i = 10; i < 20; i++) {
        publishMessagingMetrics(i, startTime, METRICS_CONTEXT, expected, SYSTEM_METRIC_PREFIX, MetricType.COUNTER);
    }
    Thread.sleep(500);
    // Stop and restart messagingMetricsProcessorManagerService
    messagingMetricsProcessorManagerService.stopAndWait();
    // Intentionally set queue size to a large value, so that MessagingMetricsProcessorManagerService
    // internally only persists metrics during terminating.
    messagingMetricsProcessorManagerService = new MessagingMetricsProcessorManagerService(cConf, injector.getInstance(MetricDatasetFactory.class), messagingService, injector.getInstance(SchemaGenerator.class), injector.getInstance(DatumReaderFactory.class), metricStore, injector.getInstance(MetricsWriterProvider.class), partitions, new NoopMetricsContext(), 50, 0);
    messagingMetricsProcessorManagerService.startAndWait();
    // Publish metrics after MessagingMetricsProcessorManagerService restarts and record expected metrics
    for (int i = 20; i < 30; i++) {
        publishMessagingMetrics(i, startTime, METRICS_CONTEXT, expected, SYSTEM_METRIC_PREFIX, MetricType.GAUGE);
    }
    final List<String> missingMetricNames = new ArrayList<>();
    // are retrieved when timeout occurs, print out the missing metrics
    try {
        Tasks.waitFor(true, new Callable<Boolean>() {

            @Override
            public Boolean call() throws Exception {
                return canQueryAllMetrics(metricStore, METRICS_CONTEXT, expected, missingMetricNames);
            }
        }, 10000, TimeUnit.MILLISECONDS, "Failed to get all metrics");
    } catch (TimeoutException e) {
        Assert.fail(String.format("Metrics: [%s] cannot be found in the metrics store.", Joiner.on(", ").join(missingMetricNames)));
    }
    // Query metrics from the metricStore and compare them with the expected ones
    assertMetricsResult(metricStore, METRICS_CONTEXT, expected);
    // Query for the 5 counter metrics published with messaging between time 5 - 14
    Collection<MetricTimeSeries> queryResult = metricStore.query(new MetricDataQuery(5, 14, 1, Integer.MAX_VALUE, ImmutableMap.of(SYSTEM_METRIC_PREFIX + COUNTER_METRIC_NAME, AggregationFunction.SUM), METRICS_CONTEXT, ImmutableList.<String>of(), null));
    MetricTimeSeries timeSeries = Iterables.getOnlyElement(queryResult);
    Assert.assertEquals(5, timeSeries.getTimeValues().size());
    for (TimeValue timeValue : timeSeries.getTimeValues()) {
        Assert.assertEquals(1L, timeValue.getValue());
    }
    // Stop services and servers
    messagingMetricsProcessorManagerService.stopAndWait();
    // Delete all metrics
    metricStore.deleteAll();
}
Also used : MetricStore(io.cdap.cdap.api.metrics.MetricStore) DatumReaderFactory(io.cdap.cdap.internal.io.DatumReaderFactory) StructuredTableAdmin(io.cdap.cdap.spi.data.StructuredTableAdmin) ArrayList(java.util.ArrayList) MetricTimeSeries(io.cdap.cdap.api.metrics.MetricTimeSeries) DatasetService(io.cdap.cdap.data2.datafabric.dataset.service.DatasetService) NoopMetricsContext(io.cdap.cdap.api.metrics.NoopMetricsContext) MetricDatasetFactory(io.cdap.cdap.metrics.store.MetricDatasetFactory) MetricsWriterProvider(io.cdap.cdap.metrics.process.loader.MetricsWriterProvider) TimeValue(io.cdap.cdap.api.dataset.lib.cube.TimeValue) HashSet(java.util.HashSet) TimeoutException(java.util.concurrent.TimeoutException) SchemaGenerator(io.cdap.cdap.internal.io.SchemaGenerator) TimeoutException(java.util.concurrent.TimeoutException) TransactionManager(org.apache.tephra.TransactionManager) DatasetOpExecutorService(io.cdap.cdap.data2.datafabric.dataset.service.executor.DatasetOpExecutorService) MetricDataQuery(io.cdap.cdap.api.metrics.MetricDataQuery) Test(org.junit.Test)

Aggregations

NoopMetricsContext (io.cdap.cdap.api.metrics.NoopMetricsContext)9 Test (org.junit.Test)6 HttpHandler (io.cdap.http.HttpHandler)5 NettyHttpService (io.cdap.http.NettyHttpService)4 InetSocketAddress (java.net.InetSocketAddress)4 HttpURLConnection (java.net.HttpURLConnection)3 URL (java.net.URL)3 ArrayList (java.util.ArrayList)3 DatasetService (io.cdap.cdap.data2.datafabric.dataset.service.DatasetService)2 DatasetOpExecutorService (io.cdap.cdap.data2.datafabric.dataset.service.executor.DatasetOpExecutorService)2 DatumReaderFactory (io.cdap.cdap.internal.io.DatumReaderFactory)2 SchemaGenerator (io.cdap.cdap.internal.io.SchemaGenerator)2 MetricsWriterProvider (io.cdap.cdap.metrics.process.loader.MetricsWriterProvider)2 MetricDatasetFactory (io.cdap.cdap.metrics.store.MetricDatasetFactory)2 StructuredTableAdmin (io.cdap.cdap.spi.data.StructuredTableAdmin)2 File (java.io.File)2 TransactionManager (org.apache.tephra.TransactionManager)2 ImmutableList (com.google.common.collect.ImmutableList)1 TypeToken (com.google.common.reflect.TypeToken)1 TimeValue (io.cdap.cdap.api.dataset.lib.cube.TimeValue)1