Search in sources :

Example 1 with MetricsQueryHelper

use of co.cask.cdap.metrics.query.MetricsQueryHelper in project cdap by caskdata.

the class PreviewHttpHandler method query.

@POST
@Path("previews/{preview-id}/metrics/query")
public void query(HttpRequest request, HttpResponder responder, @PathParam("namespace-id") String namespaceId, @PathParam("preview-id") String previewId, @QueryParam("metric") List<String> metrics, @QueryParam("groupBy") List<String> groupBy, @QueryParam("tag") List<String> tags) throws Exception {
    MetricsQueryHelper helper = getMetricsQueryHelper(namespaceId, previewId);
    try {
        if (new QueryStringDecoder(request.getUri()).getParameters().isEmpty()) {
            if (HttpHeaders.getContentLength(request) > 0) {
                Map<String, MetricsQueryHelper.QueryRequestFormat> queries = GSON.fromJson(request.getContent().toString(Charsets.UTF_8), new TypeToken<Map<String, MetricsQueryHelper.QueryRequestFormat>>() {
                }.getType());
                overrideQueries(queries, namespaceId, previewId);
                responder.sendJson(HttpResponseStatus.OK, helper.executeBatchQueries(queries));
                return;
            }
            responder.sendJson(HttpResponseStatus.BAD_REQUEST, "Batch request with empty content");
        }
        tags = overrideTags(tags, namespaceId, previewId);
        responder.sendJson(HttpResponseStatus.OK, helper.executeTagQuery(tags, metrics, groupBy, new QueryStringDecoder(request.getUri()).getParameters()));
    } catch (IllegalArgumentException e) {
        LOG.warn("Invalid request", e);
        responder.sendString(HttpResponseStatus.BAD_REQUEST, e.getMessage());
    } catch (Exception e) {
        LOG.error("Exception querying metrics ", e);
        responder.sendString(HttpResponseStatus.INTERNAL_SERVER_ERROR, "Internal error while querying for metrics");
    }
}
Also used : QueryStringDecoder(org.jboss.netty.handler.codec.http.QueryStringDecoder) MetricsQueryHelper(co.cask.cdap.metrics.query.MetricsQueryHelper) TypeToken(com.google.gson.reflect.TypeToken) JsonSyntaxException(com.google.gson.JsonSyntaxException) BadRequestException(co.cask.cdap.common.BadRequestException) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST)

Aggregations

BadRequestException (co.cask.cdap.common.BadRequestException)1 MetricsQueryHelper (co.cask.cdap.metrics.query.MetricsQueryHelper)1 JsonSyntaxException (com.google.gson.JsonSyntaxException)1 TypeToken (com.google.gson.reflect.TypeToken)1 POST (javax.ws.rs.POST)1 Path (javax.ws.rs.Path)1 QueryStringDecoder (org.jboss.netty.handler.codec.http.QueryStringDecoder)1