use of io.cdap.cdap.api.metrics.MetricTimeSeries in project cdap by cdapio.
the class MetricsQueryHelper method decorate.
private MetricQueryResult decorate(Collection<MetricTimeSeries> series, long startTs, long endTs, int resolution) {
MetricQueryResult.TimeSeries[] serieses = new MetricQueryResult.TimeSeries[series.size()];
int i = 0;
for (MetricTimeSeries timeSeries : series) {
MetricQueryResult.TimeValue[] timeValues = decorate(timeSeries.getTimeValues());
serieses[i++] = new MetricQueryResult.TimeSeries(timeSeries.getMetricName(), tagNamesToHuman(timeSeries.getTagValues()), timeValues);
}
return new MetricQueryResult(startTs, endTs, serieses, resolution);
}
use of io.cdap.cdap.api.metrics.MetricTimeSeries in project cdap by cdapio.
the class MetricsQueryHelper method executeQuery.
private MetricQueryResult executeQuery(MetricQueryRequest queryRequest) throws Exception {
if (queryRequest.getMetrics().size() == 0) {
throw new IllegalArgumentException("Missing metrics parameter in the query");
}
MetricQueryRequest.TimeRange timeRange = queryRequest.getTimeRange();
AggregationOption aggregation = timeRange.getAggregation();
if (timeRange.getCount() <= 0) {
throw new IllegalArgumentException("Invalid metrics aggregation request, the limit must be greater than 0");
}
Map<String, String> tagsSliceBy = humanToTagNames(transformTagMap(queryRequest.getTags()));
MetricDataQuery query = new MetricDataQuery(timeRange.getStart(), timeRange.getEnd(), timeRange.getResolutionInSeconds(), timeRange.getCount(), toMetrics(queryRequest.getMetrics()), tagsSliceBy, transformGroupByTags(queryRequest.getGroupBy()), aggregation, timeRange.getInterpolate());
Collection<MetricTimeSeries> queryResult = metricStore.query(query);
long endTime = timeRange.getEnd();
if (timeRange.getResolutionInSeconds() == Integer.MAX_VALUE && endTime == 0) {
// for aggregate query, we set the end time to be query time (current time)
endTime = TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis());
}
return decorate(queryResult, timeRange.getStart(), endTime, timeRange.getResolutionInSeconds());
}
use of io.cdap.cdap.api.metrics.MetricTimeSeries in project cdap by cdapio.
the class TestFrameworkTestRun method verifyMapperJobOutput.
private void verifyMapperJobOutput(Class<?> appClass, DataSetManager<KeyValueTable> outTableManager) throws Exception {
KeyValueTable outputTable = outTableManager.get();
Assert.assertEquals("world", Bytes.toString(outputTable.read("hello")));
// Verify dataset metrics
String readCountName = "system." + Constants.Metrics.Name.Dataset.READ_COUNT;
String writeCountName = "system." + Constants.Metrics.Name.Dataset.WRITE_COUNT;
Collection<MetricTimeSeries> metrics = getMetricsManager().query(new MetricDataQuery(0, System.currentTimeMillis() / 1000, Integer.MAX_VALUE, ImmutableMap.of(readCountName, AggregationFunction.SUM, writeCountName, AggregationFunction.SUM), ImmutableMap.of(Constants.Metrics.Tag.NAMESPACE, DefaultId.NAMESPACE.getNamespace(), Constants.Metrics.Tag.APP, appClass.getSimpleName(), Constants.Metrics.Tag.MAPREDUCE, DatasetWithMRApp.MAPREDUCE_PROGRAM), ImmutableList.<String>of()));
// Transform the collection of metrics into a map from metrics name to aggregated sum
Map<String, Long> aggs = Maps.transformEntries(Maps.uniqueIndex(metrics, new Function<MetricTimeSeries, String>() {
@Override
public String apply(MetricTimeSeries input) {
return input.getMetricName();
}
}), new Maps.EntryTransformer<String, MetricTimeSeries, Long>() {
@Override
public Long transformEntry(String key, MetricTimeSeries value) {
Preconditions.checkArgument(value.getTimeValues().size() == 1, "Expected one value for aggregated sum for metrics %s", key);
return value.getTimeValues().get(0).getValue();
}
});
Assert.assertEquals(Long.valueOf(1), aggs.get(readCountName));
Assert.assertEquals(Long.valueOf(1), aggs.get(writeCountName));
}
use of io.cdap.cdap.api.metrics.MetricTimeSeries in project cdap by cdapio.
the class RemoteMetricsSystemClient method query.
@Override
public Collection<MetricTimeSeries> query(int start, int end, int resolution, Map<String, String> tags, Collection<String> metrics, Collection<String> groupByTags) throws IOException {
String metricsParam = Joiner.on("&metric=").join(metrics);
if (!metricsParam.isEmpty()) {
metricsParam = "&metric=" + metricsParam;
}
String tagsParam = Joiner.on("&tag=").withKeyValueSeparator(":").join(tags);
if (!tagsParam.isEmpty()) {
tagsParam = "&tag=" + tagsParam;
}
String groupByParam = Joiner.on("&groupBy=").join(groupByTags);
if (!groupByParam.isEmpty()) {
groupByParam = "&groupBy=" + groupByParam;
}
// Only query for aggregate metrics. Currently that's the only use case.
String queryString = "aggregate=true&start=" + start + "&end=" + end + "&resolution=" + resolution + "s" + metricsParam + tagsParam + groupByParam;
URL url = getBaseURI().resolve("query?" + queryString).toURL();
HttpResponse response = HttpRequests.execute(HttpRequest.post(url).build(), REQUEST_CONFIG);
if (response.getResponseCode() != HttpURLConnection.HTTP_OK) {
throw new IOException("Failed to query for metrics " + metrics + ", with tags " + tags + ". Error code " + response.getResponseCode() + ", message " + response.getResponseBodyAsString());
}
MetricQueryResult queryResult = GSON.fromJson(response.getResponseBodyAsString(), MetricQueryResult.class);
List<MetricTimeSeries> result = new ArrayList<>();
for (MetricQueryResult.TimeSeries timeSeries : queryResult.getSeries()) {
List<TimeValue> timeValues = Arrays.stream(timeSeries.getData()).map(tv -> new TimeValue(tv.getTime(), tv.getValue())).collect(Collectors.toList());
result.add(new MetricTimeSeries(timeSeries.getMetricName(), timeSeries.getGrouping(), timeValues));
}
return result;
}
use of io.cdap.cdap.api.metrics.MetricTimeSeries in project cdap by cdapio.
the class SupportBundleRuntimeInfoTask method queryMetrics.
public JsonObject queryMetrics(String runId, String configuration, long startTs, long stopTs) {
// startTs from run time but metrics already starts before that time
int startQueryTime = (int) (startTs - 5000);
JsonObject metrics = new JsonObject();
try {
// This program type tag can be null
String typeTag = getMetricsTag(programType);
Map<String, String> queryTags = new HashMap<>();
queryTags.put("namespace", namespaceId.getNamespace());
queryTags.put("app", appId.getApplication());
queryTags.put("run", runId);
if (typeTag != null) {
queryTags.put(typeTag, programName.getProgram());
}
Collection<String> metricsNameList = remoteMetricsSystemClient.search(queryTags);
List<MetricTimeSeries> metricTimeSeriesList = new ArrayList<>(remoteMetricsSystemClient.query(startQueryTime, (int) (stopTs), queryTags, metricsNameList));
for (MetricTimeSeries timeSeries : metricTimeSeriesList) {
if (!metrics.has(timeSeries.getMetricName())) {
metrics.add(timeSeries.getMetricName(), new JsonArray());
}
for (TimeValue timeValue : timeSeries.getTimeValues()) {
JsonObject time = new JsonObject();
time.addProperty("time", timeValue.getTimestamp());
time.addProperty("value", timeValue.getValue());
metrics.getAsJsonArray(timeSeries.getMetricName()).add(time);
}
}
} catch (IOException e) {
LOG.warn("Failed to find metrics with run {} ", runId, e);
return null;
}
return metrics;
}
Aggregations