Search in sources :

Example 1 with MetricsClient

use of io.cdap.cdap.client.MetricsClient 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)

Aggregations

Injector (com.google.inject.Injector)1 MetricDeleteQuery (io.cdap.cdap.api.metrics.MetricDeleteQuery)1 MetricsCollectionService (io.cdap.cdap.api.metrics.MetricsCollectionService)1 MetricsContext (io.cdap.cdap.api.metrics.MetricsContext)1 MetricsSystemClient (io.cdap.cdap.api.metrics.MetricsSystemClient)1 MetricsClient (io.cdap.cdap.client.MetricsClient)1 ConnectionConfig (io.cdap.cdap.client.config.ConnectionConfig)1 RandomEndpointStrategy (io.cdap.cdap.common.discovery.RandomEndpointStrategy)1 RemoteMetricsSystemClient (io.cdap.cdap.metrics.process.RemoteMetricsSystemClient)1 MetricsCleanUpService (io.cdap.cdap.metrics.store.MetricsCleanUpService)1 MetricQueryResult (io.cdap.cdap.proto.MetricQueryResult)1 InetSocketAddress (java.net.InetSocketAddress)1 ArrayList (java.util.ArrayList)1 Discoverable (org.apache.twill.discovery.Discoverable)1 DiscoveryServiceClient (org.apache.twill.discovery.DiscoveryServiceClient)1 Test (org.junit.Test)1