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);
}
}
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();
}
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();
}
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());
}
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());
}
}
Aggregations