Search in sources :

Example 6 with MetricQueryResult

use of co.cask.cdap.proto.MetricQueryResult in project cdap by caskdata.

the class GetMetricCommand method perform.

@Override
public void perform(Arguments arguments, PrintStream output) throws Exception {
    String metric = arguments.get("metric-name");
    Map<String, String> tags = ArgumentParser.parseMap(arguments.getOptional("tags", ""), "<tags>");
    String start = arguments.getOptional("start", "");
    String end = arguments.getOptional("end", "");
    MetricQueryResult result = client.query(tags, ImmutableList.of(metric), ImmutableList.<String>of(), start.isEmpty() ? null : start, end.isEmpty() ? null : end);
    output.printf("Start time: %d\n", result.getStartTime());
    output.printf("End time: %d\n", result.getEndTime());
    for (MetricQueryResult.TimeSeries series : result.getSeries()) {
        output.println();
        output.printf("Series: %s\n", series.getMetricName());
        if (!series.getGrouping().isEmpty()) {
            output.printf("Grouping: %s\n", Joiner.on(",").withKeyValueSeparator("=").join(series.getGrouping()));
        }
        Table table = Table.builder().setHeader("timestamp", "value").setRows(ImmutableList.copyOf(series.getData()), new RowMaker<MetricQueryResult.TimeValue>() {

            @Override
            public List<?> makeRow(MetricQueryResult.TimeValue object) {
                return Lists.newArrayList(object.getTime(), object.getValue());
            }
        }).build();
        cliConfig.getTableRenderer().render(cliConfig, output, table);
    }
}
Also used : Table(co.cask.cdap.cli.util.table.Table) RowMaker(co.cask.cdap.cli.util.RowMaker) MetricQueryResult(co.cask.cdap.proto.MetricQueryResult)

Example 7 with MetricQueryResult

use of co.cask.cdap.proto.MetricQueryResult in project cdap by caskdata.

the class StreamHandlerTest method getNumEventsFromResponse.

private long getNumEventsFromResponse(String response) {
    MetricQueryResult metricQueryResult = new Gson().fromJson(response, MetricQueryResult.class);
    MetricQueryResult.TimeSeries[] series = metricQueryResult.getSeries();
    if (series.length == 0) {
        return 0;
    }
    return series[0].getData()[0].getValue();
}
Also used : MetricQueryResult(co.cask.cdap.proto.MetricQueryResult) Gson(com.google.gson.Gson)

Example 8 with MetricQueryResult

use of co.cask.cdap.proto.MetricQueryResult in project cdap by caskdata.

the class MetricsClient method query.

/**
 * Gets the value of the given metrics.
 *
 * @param tags tags for the request
 * @param metrics names of the metrics
 * @param groupBys groupBys for the request
 * @param timeRangeParams parameters specifying the time range
 * @return values of the metrics
 * @throws IOException if a network error occurred
 * @throws UnauthenticatedException if the request is not authorized successfully in the gateway server
 */
// TODO: take in query object shared by MetricsHandler
public MetricQueryResult query(Map<String, String> tags, List<String> metrics, List<String> groupBys, Map<String, String> timeRangeParams) throws IOException, UnauthenticatedException, UnauthorizedException {
    List<String> queryParts = Lists.newArrayList();
    queryParts.add("target=tag");
    add("metric", metrics, queryParts);
    add("groupBy", groupBys, queryParts);
    addTags(tags, queryParts);
    addTimeRangeParametersToQuery(timeRangeParams, queryParts);
    URL url = config.resolveURLV3(String.format("metrics/query?%s", Joiner.on("&").join(queryParts)));
    HttpResponse response = restClient.execute(HttpMethod.POST, url, config.getAccessToken());
    return ObjectResponse.fromJsonBody(response, MetricQueryResult.class).getResponseObject();
}
Also used : HttpResponse(co.cask.common.http.HttpResponse) MetricQueryResult(co.cask.cdap.proto.MetricQueryResult) URL(java.net.URL)

Example 9 with MetricQueryResult

use of co.cask.cdap.proto.MetricQueryResult in project cdap by caskdata.

the class MetricsHandlerTestRun 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(co.cask.cdap.proto.MetricQueryResult) Test(org.junit.Test)

Example 10 with MetricQueryResult

use of co.cask.cdap.proto.MetricQueryResult in project cdap by caskdata.

the class MetricsHandlerTestRun 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(co.cask.cdap.proto.MetricQueryResult) HashMap(java.util.HashMap) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap)

Aggregations

MetricQueryResult (co.cask.cdap.proto.MetricQueryResult)13 ImmutableMap (com.google.common.collect.ImmutableMap)2 Map (java.util.Map)2 Test (org.junit.Test)2 TimeValue (co.cask.cdap.api.dataset.lib.cube.TimeValue)1 MetricTimeSeries (co.cask.cdap.api.metrics.MetricTimeSeries)1 RowMaker (co.cask.cdap.cli.util.RowMaker)1 Table (co.cask.cdap.cli.util.table.Table)1 FakeApp (co.cask.cdap.client.app.FakeApp)1 UnauthenticatedException (co.cask.cdap.common.UnauthenticatedException)1 MetricQueryRequest (co.cask.cdap.proto.MetricQueryRequest)1 MetricTagValue (co.cask.cdap.proto.MetricTagValue)1 ApplicationId (co.cask.cdap.proto.id.ApplicationId)1 FlowletId (co.cask.cdap.proto.id.FlowletId)1 ProgramId (co.cask.cdap.proto.id.ProgramId)1 StreamId (co.cask.cdap.proto.id.StreamId)1 UnauthorizedException (co.cask.cdap.security.spi.authorization.UnauthorizedException)1 HttpResponse (co.cask.common.http.HttpResponse)1 ImmutableBiMap (com.google.common.collect.ImmutableBiMap)1 Gson (com.google.gson.Gson)1