Search in sources :

Example 1 with MetricDeleteQuery

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

the class MetricsHandlerTest method testInterpolate.

@Test
public void testInterpolate() throws Exception {
    long start = System.currentTimeMillis() / 1000;
    long end = start + 3;
    Map<String, String> sliceBy = getServiceContext("interspace", "WordCount1", "WordCounter", "run1", "splitter");
    MetricValues value = new MetricValues(sliceBy, "reads", start, 100, MetricType.COUNTER);
    metricStore.add(value);
    value = new MetricValues(sliceBy, "reads", end, 400, MetricType.COUNTER);
    metricStore.add(value);
    verifyRangeQueryResult("/v3/metrics/query?" + getTags("interspace", "WordCount1", "WordCounter", "splitter") + "&metric=system.reads&interpolate=step&start=" + start + "&end=" + end, 4, 700);
    verifyRangeQueryResult("/v3/metrics/query?" + getTags("interspace", "WordCount1", "WordCounter", "splitter") + "&metric=system.reads&interpolate=linear&start=" + start + "&end=" + end, 4, 1000);
    Map<String, String> deleteTags = new LinkedHashMap<>();
    deleteTags.put(Constants.Metrics.Tag.NAMESPACE, "interspace");
    deleteTags.put(Constants.Metrics.Tag.APP, "WordCount1");
    deleteTags.put(Constants.Metrics.Tag.SERVICE, "WordCounter");
    // delete the added metrics for testing interpolator
    MetricDeleteQuery deleteQuery = new MetricDeleteQuery(start, end, Collections.emptySet(), deleteTags, new ArrayList<>(deleteTags.keySet()));
    metricStore.delete(deleteQuery);
}
Also used : MetricDeleteQuery(io.cdap.cdap.api.metrics.MetricDeleteQuery) MetricValues(io.cdap.cdap.api.metrics.MetricValues) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.Test)

Example 2 with MetricDeleteQuery

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

the class ApplicationLifecycleService method deleteMetrics.

/**
 * Delete the metrics for an application.
 *
 * @param applicationId the application to delete metrics for.
 */
private void deleteMetrics(ApplicationId applicationId, ApplicationSpecification spec) throws IOException {
    long endTs = System.currentTimeMillis() / 1000;
    Map<String, String> tags = new LinkedHashMap<>();
    tags.put(Constants.Metrics.Tag.NAMESPACE, applicationId.getNamespace());
    // add or replace application name in the tagMap
    tags.put(Constants.Metrics.Tag.APP, spec.getName());
    MetricDeleteQuery deleteQuery = new MetricDeleteQuery(0, endTs, Collections.emptySet(), tags, new ArrayList<>(tags.keySet()));
    metricsSystemClient.delete(deleteQuery);
}
Also used : MetricDeleteQuery(io.cdap.cdap.api.metrics.MetricDeleteQuery) LinkedHashMap(java.util.LinkedHashMap)

Example 3 with MetricDeleteQuery

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

the class DefaultMetricStore method deleteAll.

@Override
public void deleteAll() {
    // this will delete all aggregates metrics data
    delete(new MetricDeleteQuery(0, System.currentTimeMillis() / 1000, Collections.emptySet(), Collections.emptyMap(), Collections.emptyList()));
    // this will delete all timeseries data
    deleteBefore(System.currentTimeMillis() / 1000);
}
Also used : MetricDeleteQuery(io.cdap.cdap.api.metrics.MetricDeleteQuery)

Example 4 with MetricDeleteQuery

use of io.cdap.cdap.api.metrics.MetricDeleteQuery 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 5 with MetricDeleteQuery

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

the class MetricsHandlerTest method testAutoResolutions.

@Test
public void testAutoResolutions() throws Exception {
    long start = 1;
    Map<String, String> sliceBy = getServiceContext("resolutions", "WordCount1", "WordCounter", "run1", "splitter");
    // 1 second
    metricStore.add(new MetricValues(sliceBy, "reads", start, 1, MetricType.COUNTER));
    // 30 second
    metricStore.add(new MetricValues(sliceBy, "reads", start + 30, 1, MetricType.COUNTER));
    // 1 minute
    metricStore.add(new MetricValues(sliceBy, "reads", start + 60, 1, MetricType.COUNTER));
    // 10 minutes
    metricStore.add(new MetricValues(sliceBy, "reads", start + 600, 1, MetricType.COUNTER));
    // 1 hour
    metricStore.add(new MetricValues(sliceBy, "reads", start + 3600, 1, MetricType.COUNTER));
    // 10 hour
    metricStore.add(new MetricValues(sliceBy, "reads", start + 36000, 1, MetricType.COUNTER));
    // seconds
    verifyRangeQueryResult("/v3/metrics/query?" + getTags("resolutions", "WordCount1", "WordCounter", "splitter") + "&metric=system.reads&resolution=auto&start=" + start + "&end=" + (start + 600), 4, 4);
    // minutes
    verifyRangeQueryResult("/v3/metrics/query?" + getTags("resolutions", "WordCount1", "WordCounter", "splitter") + "&metric=system.reads&resolution=auto&start=" + (start - 1) + "&end=" + (start + 600), 3, 4);
    // minutes
    verifyRangeQueryResult("/v3/metrics/query?" + getTags("resolutions", "WordCount1", "WordCounter", "splitter") + "&metric=system.reads&resolution=auto&start=" + (start - 1) + "&end=" + (start + 3600), 4, 5);
    // hours
    verifyRangeQueryResult("/v3/metrics/query?" + getTags("resolutions", "WordCount1", "WordCounter", "splitter") + "&metric=system.reads&resolution=auto&start=" + (start - 1) + "&end=" + (start + 36000), 3, 6);
    Map<String, String> deleteTags = new LinkedHashMap<>();
    deleteTags.put(Constants.Metrics.Tag.NAMESPACE, "resolutions");
    deleteTags.put(Constants.Metrics.Tag.APP, "WordCount1");
    deleteTags.put(Constants.Metrics.Tag.SERVICE, "WordCounter");
    // delete the added metrics for testing auto resolutions
    MetricDeleteQuery deleteQuery = new MetricDeleteQuery(start, (start + 36000), Collections.emptySet(), deleteTags, new ArrayList<>(deleteTags.keySet()));
    metricStore.delete(deleteQuery);
}
Also used : MetricDeleteQuery(io.cdap.cdap.api.metrics.MetricDeleteQuery) MetricValues(io.cdap.cdap.api.metrics.MetricValues) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.Test)

Aggregations

MetricDeleteQuery (io.cdap.cdap.api.metrics.MetricDeleteQuery)9 LinkedHashMap (java.util.LinkedHashMap)6 Test (org.junit.Test)4 Injector (com.google.inject.Injector)2 MetricValues (io.cdap.cdap.api.metrics.MetricValues)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 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)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