Search in sources :

Example 1 with MetricsSystemClient

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

the class MetricsServiceMainTest method testMetricsService.

@Test
public void testMetricsService() throws Exception {
    Injector injector = getServiceMainInstance(MetricsServiceMain.class).getInjector();
    // make sure the metrics clean up service is running
    MetricsCleanUpService service = injector.getInstance(MetricsCleanUpService.class);
    Assert.assertTrue(service.isRunning());
    // Publish some metrics via the MetricsCollectionService
    MetricsCollectionService metricsCollectionService = injector.getInstance(MetricsCollectionService.class);
    MetricsContext context = metricsCollectionService.getContext(ImmutableMap.of(Constants.Metrics.Tag.NAMESPACE, NamespaceId.SYSTEM.getNamespace(), Constants.Metrics.Tag.APP, "test"));
    context.increment("name", 10);
    // Discovery the location of metrics query service
    DiscoveryServiceClient discoveryServiceClient = injector.getInstance(DiscoveryServiceClient.class);
    Discoverable metricsEndpoint = new RandomEndpointStrategy(() -> discoveryServiceClient.discover(Constants.Service.METRICS)).pick(5, TimeUnit.SECONDS);
    Assert.assertNotNull(metricsEndpoint);
    // Try to query the metrics
    InetSocketAddress metricsAddr = metricsEndpoint.getSocketAddress();
    ConnectionConfig connConfig = ConnectionConfig.builder().setSSLEnabled(URIScheme.HTTPS.isMatch(metricsEndpoint)).setHostname(metricsAddr.getHostName()).setPort(metricsAddr.getPort()).build();
    MetricsClient metricsClient = new MetricsClient(ClientConfig.builder().setVerifySSLCert(false).setConnectionConfig(connConfig).build());
    // Need to poll because metrics processing is async.
    Tasks.waitFor(10L, () -> {
        MetricQueryResult result = metricsClient.query(context.getTags(), "system.name");
        MetricQueryResult.TimeSeries[] series = result.getSeries();
        if (series.length == 0) {
            return 0L;
        }
        return series[0].getData()[0].getValue();
    }, 10, TimeUnit.SECONDS, 1, TimeUnit.SECONDS);
    MetricsSystemClient metricsSystemClient = injector.getInstance(RemoteMetricsSystemClient.class);
    metricsSystemClient.delete(new MetricDeleteQuery(0, Integer.MAX_VALUE, Collections.emptySet(), context.getTags(), new ArrayList<>(context.getTags().keySet())));
    Tasks.waitFor(0L, () -> {
        MetricQueryResult result = metricsClient.query(context.getTags(), "system.name");
        MetricQueryResult.TimeSeries[] series = result.getSeries();
        if (series.length == 0) {
            return 0L;
        }
        return series[0].getData()[0].getValue();
    }, 10, TimeUnit.SECONDS, 1, TimeUnit.SECONDS);
}
Also used : Discoverable(org.apache.twill.discovery.Discoverable) DiscoveryServiceClient(org.apache.twill.discovery.DiscoveryServiceClient) MetricsCollectionService(io.cdap.cdap.api.metrics.MetricsCollectionService) InetSocketAddress(java.net.InetSocketAddress) MetricDeleteQuery(io.cdap.cdap.api.metrics.MetricDeleteQuery) MetricsContext(io.cdap.cdap.api.metrics.MetricsContext) ArrayList(java.util.ArrayList) MetricQueryResult(io.cdap.cdap.proto.MetricQueryResult) MetricsClient(io.cdap.cdap.client.MetricsClient) Injector(com.google.inject.Injector) MetricsCleanUpService(io.cdap.cdap.metrics.store.MetricsCleanUpService) RemoteMetricsSystemClient(io.cdap.cdap.metrics.process.RemoteMetricsSystemClient) MetricsSystemClient(io.cdap.cdap.api.metrics.MetricsSystemClient) ConnectionConfig(io.cdap.cdap.client.config.ConnectionConfig) RandomEndpointStrategy(io.cdap.cdap.common.discovery.RandomEndpointStrategy) Test(org.junit.Test)

Example 2 with MetricsSystemClient

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

the class MetricsAdminSubscriberServiceTest method test.

@Test
public void test() throws Exception {
    MetricsAdminSubscriberService adminService = injector.getInstance(MetricsAdminSubscriberService.class);
    adminService.startAndWait();
    // publish a metrics
    MetricsContext metricsContext = metricsCollectionService.getContext(Collections.singletonMap(Constants.Metrics.Tag.NAMESPACE, NamespaceId.SYSTEM.getNamespace()));
    metricsContext.increment("test.increment", 10L);
    metricsContext.gauge("test.gauge", 20L);
    MetricsSystemClient systemClient = injector.getInstance(RemoteMetricsSystemClient.class);
    // Search for metrics names
    Tasks.waitFor(true, () -> {
        Set<String> names = new HashSet<>(systemClient.search(metricsContext.getTags()));
        return names.contains("system.test.increment") && names.contains("system.test.gauge");
    }, 10, TimeUnit.SECONDS, 1, TimeUnit.SECONDS);
    // Query for metrics values
    Tasks.waitFor(true, () -> {
        Collection<MetricTimeSeries> values = systemClient.query(metricsContext.getTags(), Arrays.asList("system.test.increment", "system.test.gauge"));
        // Find and match the values for the increment and gauge
        boolean incMatched = values.stream().filter(timeSeries -> timeSeries.getMetricName().equals("system.test.increment")).flatMap(timeSeries -> timeSeries.getTimeValues().stream()).findFirst().filter(timeValue -> timeValue.getValue() == 10L).isPresent();
        boolean gaugeMatched = values.stream().filter(timeSeries -> timeSeries.getMetricName().equals("system.test.gauge")).flatMap(timeSeries -> timeSeries.getTimeValues().stream()).findFirst().filter(timeValue -> timeValue.getValue() == 20L).isPresent();
        return incMatched && gaugeMatched;
    }, 10, TimeUnit.SECONDS, 1, TimeUnit.SECONDS);
    // Emit more metrics
    metricsContext.increment("test.increment", 40L);
    metricsContext.gauge("test.gauge", 40L);
    // Query for metrics values. Should see the latest aggregates
    Tasks.waitFor(true, () -> {
        Collection<MetricTimeSeries> values = systemClient.query(metricsContext.getTags(), Arrays.asList("system.test.increment", "system.test.gauge"));
        // Find and match the values for the increment and gauge
        boolean incMatched = values.stream().filter(timeSeries -> timeSeries.getMetricName().equals("system.test.increment")).flatMap(timeSeries -> timeSeries.getTimeValues().stream()).findFirst().filter(timeValue -> timeValue.getValue() == 50L).isPresent();
        boolean gaugeMatched = values.stream().filter(timeSeries -> timeSeries.getMetricName().equals("system.test.gauge")).flatMap(timeSeries -> timeSeries.getTimeValues().stream()).findFirst().filter(timeValue -> timeValue.getValue() == 40L).isPresent();
        return incMatched && gaugeMatched;
    }, 10, TimeUnit.SECONDS, 1, TimeUnit.SECONDS);
    // Delete the increment metrics
    systemClient.delete(new MetricDeleteQuery(0, Integer.MAX_VALUE, Collections.emptySet(), metricsContext.getTags(), new ArrayList<>(metricsContext.getTags().keySet())));
    Tasks.waitFor(true, () -> {
        Collection<MetricTimeSeries> values = systemClient.query(metricsContext.getTags(), Arrays.asList("system.test.increment", "system.test.gauge"));
        // increment should be missing
        boolean foundInc = values.stream().anyMatch(timeSeries -> timeSeries.getMetricName().equals("system.test.increment"));
        // Find and match the values for gauge
        boolean foundGauge = values.stream().anyMatch(timeSeries -> timeSeries.getMetricName().equals("system.test.gauge"));
        return !foundInc && !foundGauge;
    }, 1000, TimeUnit.SECONDS, 1, TimeUnit.SECONDS);
    adminService.stopAndWait();
}
Also used : MessagingServerRuntimeModule(io.cdap.cdap.messaging.guice.MessagingServerRuntimeModule) Arrays(java.util.Arrays) ConfigModule(io.cdap.cdap.common.guice.ConfigModule) BeforeClass(org.junit.BeforeClass) MetricsStoreModule(io.cdap.cdap.metrics.guice.MetricsStoreModule) NamespaceId(io.cdap.cdap.proto.id.NamespaceId) PrivateModule(com.google.inject.PrivateModule) LocalMetricsCollectionService(io.cdap.cdap.metrics.collect.LocalMetricsCollectionService) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) SystemDatasetRuntimeModule(io.cdap.cdap.data.runtime.SystemDatasetRuntimeModule) MetricsHandlerModule(io.cdap.cdap.metrics.guice.MetricsHandlerModule) MetricDeleteQuery(io.cdap.cdap.api.metrics.MetricDeleteQuery) ClassRule(org.junit.ClassRule) IOModule(io.cdap.cdap.common.guice.IOModule) Tasks(io.cdap.cdap.common.utils.Tasks) AfterClass(org.junit.AfterClass) MetricsQueryService(io.cdap.cdap.metrics.query.MetricsQueryService) Collection(java.util.Collection) InMemoryDiscoveryModule(io.cdap.cdap.common.guice.InMemoryDiscoveryModule) MessagingService(io.cdap.cdap.messaging.MessagingService) Set(java.util.Set) Test(org.junit.Test) IOException(java.io.IOException) MetricTimeSeries(io.cdap.cdap.api.metrics.MetricTimeSeries) Scopes(com.google.inject.Scopes) Service(com.google.common.util.concurrent.Service) MetricsCollectionService(io.cdap.cdap.api.metrics.MetricsCollectionService) Injector(com.google.inject.Injector) TimeUnit(java.util.concurrent.TimeUnit) MetricsContext(io.cdap.cdap.api.metrics.MetricsContext) CConfiguration(io.cdap.cdap.common.conf.CConfiguration) Guice(com.google.inject.Guice) MetricsSystemClient(io.cdap.cdap.api.metrics.MetricsSystemClient) Constants(io.cdap.cdap.common.conf.Constants) Collections(java.util.Collections) TemporaryFolder(org.junit.rules.TemporaryFolder) MetricDeleteQuery(io.cdap.cdap.api.metrics.MetricDeleteQuery) MetricsContext(io.cdap.cdap.api.metrics.MetricsContext) ArrayList(java.util.ArrayList) MetricTimeSeries(io.cdap.cdap.api.metrics.MetricTimeSeries) MetricsSystemClient(io.cdap.cdap.api.metrics.MetricsSystemClient) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

Injector (com.google.inject.Injector)2 MetricDeleteQuery (io.cdap.cdap.api.metrics.MetricDeleteQuery)2 MetricsCollectionService (io.cdap.cdap.api.metrics.MetricsCollectionService)2 MetricsContext (io.cdap.cdap.api.metrics.MetricsContext)2 MetricsSystemClient (io.cdap.cdap.api.metrics.MetricsSystemClient)2 ArrayList (java.util.ArrayList)2 Test (org.junit.Test)2 Service (com.google.common.util.concurrent.Service)1 Guice (com.google.inject.Guice)1 PrivateModule (com.google.inject.PrivateModule)1 Scopes (com.google.inject.Scopes)1 MetricTimeSeries (io.cdap.cdap.api.metrics.MetricTimeSeries)1 MetricsClient (io.cdap.cdap.client.MetricsClient)1 ConnectionConfig (io.cdap.cdap.client.config.ConnectionConfig)1 CConfiguration (io.cdap.cdap.common.conf.CConfiguration)1 Constants (io.cdap.cdap.common.conf.Constants)1 RandomEndpointStrategy (io.cdap.cdap.common.discovery.RandomEndpointStrategy)1 ConfigModule (io.cdap.cdap.common.guice.ConfigModule)1 IOModule (io.cdap.cdap.common.guice.IOModule)1 InMemoryDiscoveryModule (io.cdap.cdap.common.guice.InMemoryDiscoveryModule)1