@Test public void testRequestWithSerializationException() throws IOException { FullHttpRequest request = createQueryRequest("?points=10&from=1&to=2"); ArgumentCaptor<FullHttpResponse> argument = ArgumentCaptor.forClass(FullHttpResponse.class); String message = "mock exception message"; when(serializer.transformRollupData(any(MetricData.class), anySet())).thenThrow(new SerializationException(message)); handler.handle(context, request); verify(channel).write(argument.capture()); String errorResponseBody = argument.getValue().content().toString(Charset.defaultCharset()); ErrorResponse errorResponse = getErrorResponse(errorResponseBody); assertEquals("Number of errors invalid", 1, errorResponse.getErrors().size()); assertEquals("Invalid error message", message, errorResponse.getErrors().get(0).getMessage()); assertEquals("Invalid tenant", TENANT, errorResponse.getErrors().get(0).getTenantId()); assertEquals("Invalid status", HttpResponseStatus.INTERNAL_SERVER_ERROR, argument.getValue().getStatus()); }
@Override public void handle(ChannelHandlerContext ctx, FullHttpRequest request) { Tracker.getInstance().track(request); final String tenantId = request.headers().get("tenantId"); final String metricName = request.headers().get("metricName"); if (!(request instanceof HttpRequestWithDecodedQueryParams)) { DefaultHandler.sendErrorResponse(ctx, request, "Missing query params: from, to, points", HttpResponseStatus.BAD_REQUEST); return; } HttpRequestWithDecodedQueryParams requestWithParams = (HttpRequestWithDecodedQueryParams) request; final Timer.Context httpMetricsFetchTimerContext = httpMetricsFetchTimer.time(); try { RollupsQueryParams params = PlotRequestParser.parseParams(requestWithParams.getQueryParams()); JSONObject metricData; if (params.isGetByPoints()) { metricData = GetDataByPoints(tenantId, metricName, params.getRange().getStart(), params.getRange().getStop(), params.getPoints(), params.getStats()); } else if (params.isGetByResolution()) { metricData = GetDataByResolution(tenantId, metricName, params.getRange().getStart(), params.getRange().getStop(), params.getResolution(), params.getStats()); } else { throw new InvalidRequestException("Invalid rollups query. Neither points nor resolution specified."); } final JsonElement element = parser.parse(metricData.toString()); final String jsonStringRep = gson.toJson(element); sendResponse(ctx, request, jsonStringRep, HttpResponseStatus.OK); } catch (InvalidRequestException e) { log.debug(e.getMessage()); DefaultHandler.sendErrorResponse(ctx, request, e.getMessage(), HttpResponseStatus.BAD_REQUEST); } catch (SerializationException e) { log.error(e.getMessage(), e); DefaultHandler.sendErrorResponse(ctx, request, e.getMessage(), HttpResponseStatus.INTERNAL_SERVER_ERROR); } catch (Exception e) { log.error(e.getMessage(), e); DefaultHandler.sendErrorResponse(ctx, request, e.getMessage(), HttpResponseStatus.INTERNAL_SERVER_ERROR); } finally { httpMetricsFetchTimerContext.stop(); } }
HttpRollupsQueryHandler extends RollupHandler implements MetricDataQueryInterface<MetricData>, HttpRequestHandler { @Override public void handle(ChannelHandlerContext ctx, FullHttpRequest request) { Tracker.getInstance().track(request); final String tenantId = request.headers().get("tenantId"); final String metricName = request.headers().get("metricName"); if (!(request instanceof HttpRequestWithDecodedQueryParams)) { DefaultHandler.sendErrorResponse(ctx, request, "Missing query params: from, to, points", HttpResponseStatus.BAD_REQUEST); return; } HttpRequestWithDecodedQueryParams requestWithParams = (HttpRequestWithDecodedQueryParams) request; final Timer.Context httpMetricsFetchTimerContext = httpMetricsFetchTimer.time(); try { RollupsQueryParams params = PlotRequestParser.parseParams(requestWithParams.getQueryParams()); JSONObject metricData; if (params.isGetByPoints()) { metricData = GetDataByPoints(tenantId, metricName, params.getRange().getStart(), params.getRange().getStop(), params.getPoints(), params.getStats()); } else if (params.isGetByResolution()) { metricData = GetDataByResolution(tenantId, metricName, params.getRange().getStart(), params.getRange().getStop(), params.getResolution(), params.getStats()); } else { throw new InvalidRequestException("Invalid rollups query. Neither points nor resolution specified."); } final JsonElement element = parser.parse(metricData.toString()); final String jsonStringRep = gson.toJson(element); sendResponse(ctx, request, jsonStringRep, HttpResponseStatus.OK); } catch (InvalidRequestException e) { log.debug(e.getMessage()); DefaultHandler.sendErrorResponse(ctx, request, e.getMessage(), HttpResponseStatus.BAD_REQUEST); } catch (SerializationException e) { log.error(e.getMessage(), e); DefaultHandler.sendErrorResponse(ctx, request, e.getMessage(), HttpResponseStatus.INTERNAL_SERVER_ERROR); } catch (Exception e) { log.error(e.getMessage(), e); DefaultHandler.sendErrorResponse(ctx, request, e.getMessage(), HttpResponseStatus.INTERNAL_SERVER_ERROR); } finally { httpMetricsFetchTimerContext.stop(); } } }
HttpRollupsQueryHandler extends RollupHandler implements MetricDataQueryInterface<MetricData>, HttpRequestHandler { @Override public void handle(ChannelHandlerContext ctx, FullHttpRequest request) { Tracker.getInstance().track(request); final String tenantId = request.headers().get("tenantId"); final String metricName = request.headers().get("metricName"); if (!(request instanceof HttpRequestWithDecodedQueryParams)) { DefaultHandler.sendErrorResponse(ctx, request, "Missing query params: from, to, points", HttpResponseStatus.BAD_REQUEST); return; } HttpRequestWithDecodedQueryParams requestWithParams = (HttpRequestWithDecodedQueryParams) request; final Timer.Context httpMetricsFetchTimerContext = httpMetricsFetchTimer.time(); try { RollupsQueryParams params = PlotRequestParser.parseParams(requestWithParams.getQueryParams()); JSONObject metricData; if (params.isGetByPoints()) { metricData = GetDataByPoints(tenantId, metricName, params.getRange().getStart(), params.getRange().getStop(), params.getPoints(), params.getStats()); } else if (params.isGetByResolution()) { metricData = GetDataByResolution(tenantId, metricName, params.getRange().getStart(), params.getRange().getStop(), params.getResolution(), params.getStats()); } else { throw new InvalidRequestException("Invalid rollups query. Neither points nor resolution specified."); } final JsonElement element = parser.parse(metricData.toString()); final String jsonStringRep = gson.toJson(element); sendResponse(ctx, request, jsonStringRep, HttpResponseStatus.OK); } catch (InvalidRequestException e) { log.debug(e.getMessage()); DefaultHandler.sendErrorResponse(ctx, request, e.getMessage(), HttpResponseStatus.BAD_REQUEST); } catch (SerializationException e) { log.error(e.getMessage(), e); DefaultHandler.sendErrorResponse(ctx, request, e.getMessage(), HttpResponseStatus.INTERNAL_SERVER_ERROR); } catch (Exception e) { log.error(e.getMessage(), e); DefaultHandler.sendErrorResponse(ctx, request, e.getMessage(), HttpResponseStatus.INTERNAL_SERVER_ERROR); } finally { httpMetricsFetchTimerContext.stop(); } } HttpRollupsQueryHandler(); @VisibleForTesting  HttpRollupsQueryHandler(BasicRollupsOutputSerializer<JSONObject> serializer); }
HttpRollupsQueryHandler extends RollupHandler implements MetricDataQueryInterface<MetricData>, HttpRequestHandler { @Override public void handle(ChannelHandlerContext ctx, FullHttpRequest request) { Tracker.getInstance().track(request); final String tenantId = request.headers().get("tenantId"); final String metricName = request.headers().get("metricName"); if (!(request instanceof HttpRequestWithDecodedQueryParams)) { DefaultHandler.sendErrorResponse(ctx, request, "Missing query params: from, to, points", HttpResponseStatus.BAD_REQUEST); return; } HttpRequestWithDecodedQueryParams requestWithParams = (HttpRequestWithDecodedQueryParams) request; final Timer.Context httpMetricsFetchTimerContext = httpMetricsFetchTimer.time(); try { RollupsQueryParams params = PlotRequestParser.parseParams(requestWithParams.getQueryParams()); JSONObject metricData; if (params.isGetByPoints()) { metricData = GetDataByPoints(tenantId, metricName, params.getRange().getStart(), params.getRange().getStop(), params.getPoints(), params.getStats()); } else if (params.isGetByResolution()) { metricData = GetDataByResolution(tenantId, metricName, params.getRange().getStart(), params.getRange().getStop(), params.getResolution(), params.getStats()); } else { throw new InvalidRequestException("Invalid rollups query. Neither points nor resolution specified."); } final JsonElement element = parser.parse(metricData.toString()); final String jsonStringRep = gson.toJson(element); sendResponse(ctx, request, jsonStringRep, HttpResponseStatus.OK); } catch (InvalidRequestException e) { log.debug(e.getMessage()); DefaultHandler.sendErrorResponse(ctx, request, e.getMessage(), HttpResponseStatus.BAD_REQUEST); } catch (SerializationException e) { log.error(e.getMessage(), e); DefaultHandler.sendErrorResponse(ctx, request, e.getMessage(), HttpResponseStatus.INTERNAL_SERVER_ERROR); } catch (Exception e) { log.error(e.getMessage(), e); DefaultHandler.sendErrorResponse(ctx, request, e.getMessage(), HttpResponseStatus.INTERNAL_SERVER_ERROR); } finally { httpMetricsFetchTimerContext.stop(); } } HttpRollupsQueryHandler(); @VisibleForTesting  HttpRollupsQueryHandler(BasicRollupsOutputSerializer<JSONObject> serializer); @Override MetricData GetDataByPoints(String tenantId,
                                      String metric,
                                      long from,
                                      long to,
                                      int points); @Override MetricData GetDataByResolution(String tenantId,
                                          String metric,
                                          long from,
                                          long to,
                                          Resolution resolution); @Override void handle(ChannelHandlerContext ctx, FullHttpRequest request); }
HttpRollupsQueryHandler extends RollupHandler implements MetricDataQueryInterface<MetricData>, HttpRequestHandler { @Override public void handle(ChannelHandlerContext ctx, FullHttpRequest request) { Tracker.getInstance().track(request); final String tenantId = request.headers().get("tenantId"); final String metricName = request.headers().get("metricName"); if (!(request instanceof HttpRequestWithDecodedQueryParams)) { DefaultHandler.sendErrorResponse(ctx, request, "Missing query params: from, to, points", HttpResponseStatus.BAD_REQUEST); return; } HttpRequestWithDecodedQueryParams requestWithParams = (HttpRequestWithDecodedQueryParams) request; final Timer.Context httpMetricsFetchTimerContext = httpMetricsFetchTimer.time(); try { RollupsQueryParams params = PlotRequestParser.parseParams(requestWithParams.getQueryParams()); JSONObject metricData; if (params.isGetByPoints()) { metricData = GetDataByPoints(tenantId, metricName, params.getRange().getStart(), params.getRange().getStop(), params.getPoints(), params.getStats()); } else if (params.isGetByResolution()) { metricData = GetDataByResolution(tenantId, metricName, params.getRange().getStart(), params.getRange().getStop(), params.getResolution(), params.getStats()); } else { throw new InvalidRequestException("Invalid rollups query. Neither points nor resolution specified."); } final JsonElement element = parser.parse(metricData.toString()); final String jsonStringRep = gson.toJson(element); sendResponse(ctx, request, jsonStringRep, HttpResponseStatus.OK); } catch (InvalidRequestException e) { log.debug(e.getMessage()); DefaultHandler.sendErrorResponse(ctx, request, e.getMessage(), HttpResponseStatus.BAD_REQUEST); } catch (SerializationException e) { log.error(e.getMessage(), e); DefaultHandler.sendErrorResponse(ctx, request, e.getMessage(), HttpResponseStatus.INTERNAL_SERVER_ERROR); } catch (Exception e) { log.error(e.getMessage(), e); DefaultHandler.sendErrorResponse(ctx, request, e.getMessage(), HttpResponseStatus.INTERNAL_SERVER_ERROR); } finally { httpMetricsFetchTimerContext.stop(); } } HttpRollupsQueryHandler(); @VisibleForTesting  HttpRollupsQueryHandler(BasicRollupsOutputSerializer<JSONObject> serializer); @Override MetricData GetDataByPoints(String tenantId,
                                      String metric,
                                      long from,
                                      long to,
                                      int points); @Override MetricData GetDataByResolution(String tenantId,
                                          String metric,
                                          long from,
                                          long to,
                                          Resolution resolution); @Override void handle(ChannelHandlerContext ctx, FullHttpRequest request);  }