Search in sources :

Example 16 with MetricQueryResult

use of io.cdap.cdap.proto.MetricQueryResult in project cdap by cdapio.

the class MetricsHandlerTest method batchTest.

private void batchTest(Map<String, QueryRequestFormat> jsonBatch, ImmutableMap<String, QueryResult> expected) throws Exception {
    String url = "/v3/metrics/query";
    Map<String, MetricQueryResult> results = post(url, GSON.toJson(jsonBatch), new TypeToken<Map<String, MetricQueryResult>>() {
    }.getType());
    // check we have all the keys
    Assert.assertEquals(expected.keySet(), results.keySet());
    for (Map.Entry<String, MetricQueryResult> entry : results.entrySet()) {
        ImmutableList<TimeSeriesSummary> expectedTimeSeriesSummary = expected.get(entry.getKey()).getExpectedList();
        MetricQueryResult actualQueryResult = entry.getValue();
        compareQueryResults(expectedTimeSeriesSummary, actualQueryResult);
        Assert.assertEquals(expected.get(entry.getKey()).getExpectedResolution(), actualQueryResult.getResolution());
    }
}
Also used : TypeToken(com.google.gson.reflect.TypeToken) MetricQueryResult(io.cdap.cdap.proto.MetricQueryResult) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 17 with MetricQueryResult

use of io.cdap.cdap.proto.MetricQueryResult in project cdap by cdapio.

the class MetricsHandlerTest method testResolutionInResponse.

@Test
public void testResolutionInResponse() throws Exception {
    long start = 1;
    String url = "/v3/metrics/query?" + getTags("resolutions", "WordCount1", "WordCounter", "splitter") + "&metric=system.reads&resolution=auto&start=" + (start - 1) + "&end=" + (start + 36000);
    MetricQueryResult queryResult = post(url, MetricQueryResult.class);
    Assert.assertEquals("3600s", queryResult.getResolution());
    url = "/v3/metrics/query?" + getTags("resolutions", "WordCount1", "WordCounter", "splitter") + "&metric=system.reads&resolution=1m&start=" + (start - 1) + "&end=" + (start + 36000);
    queryResult = post(url, MetricQueryResult.class);
    Assert.assertEquals("60s", queryResult.getResolution());
    // Have an aggregate query and ensure that its resolution is INT_MAX
    url = "/v3/metrics/query?" + getTags("WordCount1", "WordCounter", "splitter") + "&metric=system.reads";
    queryResult = post(url, MetricQueryResult.class);
    Assert.assertEquals(Integer.MAX_VALUE + "s", queryResult.getResolution());
}
Also used : MetricQueryResult(io.cdap.cdap.proto.MetricQueryResult) Test(org.junit.Test)

Example 18 with MetricQueryResult

use of io.cdap.cdap.proto.MetricQueryResult in project cdap by cdapio.

the class MetricsClientTestRun method testAll.

@Test
public void testAll() throws Exception {
    appClient.deploy(NamespaceId.DEFAULT, createAppJarFile(FakeApp.class));
    ApplicationId app = NamespaceId.DEFAULT.app(FakeApp.NAME);
    ServiceId service = app.service(PingService.NAME);
    try {
        programClient.start(service);
        programClient.waitForStatus(service, ProgramStatus.RUNNING, 15, TimeUnit.SECONDS);
        URL serviceURL = serviceClient.getServiceURL(service);
        URL pingURL = new URL(serviceURL, "ping");
        HttpResponse response = HttpRequests.execute(HttpRequest.get(pingURL).build(), new DefaultHttpRequestConfig(false));
        Assert.assertEquals(200, response.getResponseCode());
        Tasks.waitFor(true, () -> metricsClient.query(MetricsTags.service(service), Collections.singletonList(Constants.Metrics.Name.Service.SERVICE_INPUT), Collections.emptyList(), ImmutableMap.of("start", "now-20s", "end", "now")).getSeries().length > 0, 10, TimeUnit.SECONDS);
        MetricQueryResult result = metricsClient.query(MetricsTags.service(service), Constants.Metrics.Name.Service.SERVICE_INPUT);
        Assert.assertEquals(1, result.getSeries()[0].getData()[0].getValue());
        result = metricsClient.query(MetricsTags.service(service), Collections.singletonList(Constants.Metrics.Name.Service.SERVICE_INPUT), Collections.emptyList(), Collections.singletonMap("aggregate", "true"));
        Assert.assertEquals(1, result.getSeries()[0].getData()[0].getValue());
        result = metricsClient.query(MetricsTags.service(service), Collections.singletonList(Constants.Metrics.Name.Service.SERVICE_INPUT), Collections.emptyList(), ImmutableMap.of("start", "now-20s", "end", "now"));
        Assert.assertEquals(1, result.getSeries()[0].getData()[0].getValue());
        List<MetricTagValue> tags = metricsClient.searchTags(MetricsTags.service(service));
        Assert.assertEquals(1, tags.size());
        Assert.assertEquals("run", tags.get(0).getName());
        List<String> metrics = metricsClient.searchMetrics(MetricsTags.service(service));
        Assert.assertTrue(metrics.contains(Constants.Metrics.Name.Service.SERVICE_INPUT));
    } finally {
        programClient.stop(service);
        assertProgramRuns(programClient, service, ProgramRunStatus.KILLED, 1, 10);
        appClient.delete(app);
    }
}
Also used : FakeApp(io.cdap.cdap.client.app.FakeApp) DefaultHttpRequestConfig(io.cdap.cdap.common.http.DefaultHttpRequestConfig) MetricTagValue(io.cdap.cdap.proto.MetricTagValue) HttpResponse(io.cdap.common.http.HttpResponse) MetricQueryResult(io.cdap.cdap.proto.MetricQueryResult) ApplicationId(io.cdap.cdap.proto.id.ApplicationId) URL(java.net.URL) ServiceId(io.cdap.cdap.proto.id.ServiceId) Test(org.junit.Test)

Example 19 with MetricQueryResult

use of io.cdap.cdap.proto.MetricQueryResult in project cdap by caskdata.

the class MetricsQueryHelper method executeBatchQueries.

public Map<String, MetricQueryResult> executeBatchQueries(Map<String, QueryRequestFormat> queries) throws Exception {
    LOG.trace("Received Queries {}", queries);
    Map<String, MetricQueryResult> queryFinalResponse = Maps.newHashMap();
    for (Map.Entry<String, QueryRequestFormat> query : queries.entrySet()) {
        MetricQueryRequest queryRequest = getQueryRequestFromFormat(query.getValue());
        queryFinalResponse.put(query.getKey(), executeQuery(queryRequest));
    }
    return queryFinalResponse;
}
Also used : MetricQueryResult(io.cdap.cdap.proto.MetricQueryResult) ImmutableBiMap(com.google.common.collect.ImmutableBiMap) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) MetricQueryRequest(io.cdap.cdap.proto.MetricQueryRequest)

Example 20 with MetricQueryResult

use of io.cdap.cdap.proto.MetricQueryResult 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

MetricQueryResult (io.cdap.cdap.proto.MetricQueryResult)32 Test (org.junit.Test)8 TypeToken (com.google.gson.reflect.TypeToken)6 ArrayList (java.util.ArrayList)6 Map (java.util.Map)6 HttpResponse (io.cdap.common.http.HttpResponse)5 URL (java.net.URL)5 ImmutableMap (com.google.common.collect.ImmutableMap)4 TimeValue (io.cdap.cdap.api.dataset.lib.cube.TimeValue)4 MetricDeleteQuery (io.cdap.cdap.api.metrics.MetricDeleteQuery)4 MetricTimeSeries (io.cdap.cdap.api.metrics.MetricTimeSeries)4 MetricsSystemClient (io.cdap.cdap.api.metrics.MetricsSystemClient)4 RandomEndpointStrategy (io.cdap.cdap.common.discovery.RandomEndpointStrategy)4 DefaultHttpRequestConfig (io.cdap.cdap.common.http.DefaultHttpRequestConfig)4 IOException (java.io.IOException)4 HttpRequest (io.cdap.common.http.HttpRequest)3 Discoverable (org.apache.twill.discovery.Discoverable)3 DiscoveryServiceClient (org.apache.twill.discovery.DiscoveryServiceClient)3 Joiner (com.google.common.base.Joiner)2 ImmutableBiMap (com.google.common.collect.ImmutableBiMap)2