Search in sources :

Example 11 with MetricQueryResult

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

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 12 with MetricQueryResult

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

the class MetricsHandlerTest method verifyResolution.

private void verifyResolution(@Nullable String resolution, @Nullable Long start, @Nullable Long end, @Nullable Integer count, int expectedCode, @Nullable String expectedResolution) throws Exception {
    StringBuilder url = new StringBuilder("/v3/metrics/query?tag=namespace:default&tag=app:nonexist&metric=system.read");
    url.append(resolution == null ? "" : "&resolution=" + resolution);
    url.append(start == null ? "" : "&start=" + start);
    url.append(end == null ? "" : "&end=" + end);
    url.append(count == null ? "" : "&count=" + count);
    MetricQueryResult result = post(url.toString(), null, MetricQueryResult.class, expectedCode);
    if (result != null) {
        Assert.assertEquals(expectedResolution, result.getResolution());
    }
}
Also used : MetricQueryResult(io.cdap.cdap.proto.MetricQueryResult)

Example 13 with MetricQueryResult

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

the class MetricsHandlerTest method testAggregationOption.

@Test
public void testAggregationOption() throws Exception {
    Map<String, String> tags = getServiceContext("aggregation", "test", "test", "run1", "aggregationOption");
    // put 3600 metrics, each per second to the table, counter will increment by 2 per second, gauge will become larger
    // by 1 per second
    List<MetricValues> metricValues = new ArrayList<>();
    for (int i = 0; i < 3600; i++) {
        metricValues.add(new MetricValues(tags, "agg.counter", i, 2L, MetricType.COUNTER));
        metricValues.add(new MetricValues(tags, "agg.gauge", i, i + 1, MetricType.GAUGE));
    }
    metricStore.add(metricValues);
    // test some invalid query
    // the agg count should be greater than 0
    queryAggregationOption(ImmutableList.of("system.agg.counter"), "1s", 0, AggregationOption.SUM, 400);
    queryAggregationOption(ImmutableList.of("system.agg.counter"), "1s", -1, AggregationOption.SUM, 400);
    // query without aggregation option, should get 3600 data points back for each metric name
    MetricQueryResult result = queryAggregationOption(ImmutableList.of("system.agg.counter", "system.agg.gauge"), "1s", null, null, 200);
    MetricQueryResult.TimeSeries[] series = result.getSeries();
    Assert.assertEquals(2, series.length);
    Assert.assertEquals(3600, series[0].getData().length);
    for (int i = 0; i < series[0].getData().length; i++) {
        Assert.assertEquals(i, series[0].getData()[i].getTime());
        Assert.assertEquals(2, series[0].getData()[i].getValue());
        Assert.assertEquals(i, series[1].getData()[i].getTime());
        Assert.assertEquals(i + 1, series[1].getData()[i].getValue());
    }
    // query with aggregation SUM, but a limit greater than the number of data points, it should not partition the
    // data points and it should get back all the data points
    result = queryAggregationOption(ImmutableList.of("system.agg.counter", "system.agg.gauge"), "1s", Integer.MAX_VALUE, AggregationOption.SUM, 200);
    series = result.getSeries();
    Assert.assertEquals(2, series.length);
    Assert.assertEquals(3600, series[0].getData().length);
    for (int i = 0; i < series[0].getData().length; i++) {
        Assert.assertEquals(i, series[0].getData()[i].getTime());
        Assert.assertEquals(2, series[0].getData()[i].getValue());
        Assert.assertEquals(i, series[1].getData()[i].getTime());
        Assert.assertEquals(i + 1, series[1].getData()[i].getValue());
    }
    // query with aggregation sum for 10 data points for counter with 1s resolution,
    // each point should have sum 3600/10*2=720
    result = queryAggregationOption(ImmutableList.of("system.agg.counter"), "1s", 10, AggregationOption.SUM, 200);
    validateAggregationOptionSum(result, 10, 720, 1);
    // query with aggregation latest for 10 data points for gauge, each point should have latest value as multiples of
    // 360
    result = queryAggregationOption(ImmutableList.of("system.agg.gauge"), "1s", 10, AggregationOption.LATEST, 200);
    series = result.getSeries();
    Assert.assertEquals(1, series.length);
    Assert.assertEquals(10, series[0].getData().length);
    for (int i = 0; i < series[0].getData().length; i++) {
        Assert.assertEquals(360 * (i + 1) - 1, series[0].getData()[i].getTime());
        Assert.assertEquals(360 * (i + 1), series[0].getData()[i].getValue());
    }
    // query with aggregation sum for 5 data points for counter for 1m, each point should have sum 3600/5*2=1440
    result = queryAggregationOption(ImmutableList.of("system.agg.counter"), "1m", 5, AggregationOption.SUM, 200);
    validateAggregationOptionSum(result, 5, 1440, 60);
    // query by request body
    QueryRequestFormat query = new QueryRequestFormat(getTagsMap("namespace", "aggregation", "app", "test", "service", "test", "handler", "aggregationOption"), ImmutableList.of("system.agg.counter"), ImmutableList.of(), ImmutableMap.of("start", "0", "end", "now", "resolution", "1s", "count", "10", "aggregate", "SUM"));
    Map<String, MetricQueryResult> bodyResult = post("/v3/metrics/query", GSON.toJson(ImmutableMap.of("qid", query)), new TypeToken<Map<String, MetricQueryResult>>() {
    }.getType(), 200);
    Assert.assertEquals(1, bodyResult.size());
    validateAggregationOptionSum(bodyResult.get("qid"), 10, 720, 1);
}
Also used : TypeToken(com.google.gson.reflect.TypeToken) ArrayList(java.util.ArrayList) MetricQueryResult(io.cdap.cdap.proto.MetricQueryResult) MetricValues(io.cdap.cdap.api.metrics.MetricValues) Test(org.junit.Test)

Example 14 with MetricQueryResult

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

the class MetricsHandlerTest method verifyEmptyQueryResult.

private void verifyEmptyQueryResult(String url) throws Exception {
    MetricQueryResult queryResult = post(url, MetricQueryResult.class);
    Assert.assertEquals(0, queryResult.getSeries().length);
}
Also used : MetricQueryResult(io.cdap.cdap.proto.MetricQueryResult)

Example 15 with MetricQueryResult

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

the class MetricsHandlerTest method verifyGroupByResult.

private void verifyGroupByResult(String url, List<TimeSeriesResult> groupByResult) throws Exception {
    MetricQueryResult result = post(url, MetricQueryResult.class);
    Assert.assertEquals(groupByResult.size(), result.getSeries().length);
    for (MetricQueryResult.TimeSeries timeSeries : result.getSeries()) {
        boolean timeSeriesMatchFound = false;
        for (TimeSeriesResult expectedTs : groupByResult) {
            if (expectedTs.getTagValues().equals(timeSeries.getGrouping())) {
                assertTimeValues(expectedTs, timeSeries);
                timeSeriesMatchFound = true;
            }
        }
        Assert.assertTrue(timeSeriesMatchFound);
    }
}
Also used : MetricQueryResult(io.cdap.cdap.proto.MetricQueryResult)

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