use of io.cdap.cdap.api.dataset.lib.cube.TimeValue in project cdap by cdapio.
the class MetricsQueryHelper method decorate.
private MetricQueryResult.TimeValue[] decorate(List<TimeValue> points) {
MetricQueryResult.TimeValue[] timeValues = new MetricQueryResult.TimeValue[points.size()];
int k = 0;
for (TimeValue timeValue : points) {
timeValues[k++] = new MetricQueryResult.TimeValue(timeValue.getTimestamp(), timeValue.getValue());
}
return timeValues;
}
use of io.cdap.cdap.api.dataset.lib.cube.TimeValue 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.dataset.lib.cube.TimeValue 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;
}
use of io.cdap.cdap.api.dataset.lib.cube.TimeValue in project cdap by cdapio.
the class MetricsProcessorServiceTest method assertMetricsResult.
private void assertMetricsResult(MetricStore metricStore, Map<String, String> metricsContext, Map<String, Long> expected) {
for (Map.Entry<String, Long> metric : expected.entrySet()) {
Collection<MetricTimeSeries> queryResult = metricStore.query(new MetricDataQuery(0, Integer.MAX_VALUE, Integer.MAX_VALUE, metric.getKey(), AggregationFunction.SUM, metricsContext, ImmutableList.<String>of()));
MetricTimeSeries timeSeries = Iterables.getOnlyElement(queryResult);
List<TimeValue> timeValues = timeSeries.getTimeValues();
TimeValue timeValue = Iterables.getOnlyElement(timeValues);
Assert.assertEquals(String.format("Actual value of metric: %s does not match expected", metric.getKey()), metric.getValue().longValue(), timeValue.getValue());
}
}
Aggregations