/** * Pause a stopwatch. * * @param caller the caller to name this stopwatch with its class's simple name */ public static void stopTiming(Object caller) { stopTiming(caller.getClass().getSimpleName()); }
/** * Stop the most recent stopwatch. * * @deprecated Stopping a timer based on context is brittle, and prone to unexpected changes. Timers should be * stopped explicitly, or started in a try-with-resources block */ @Deprecated public static void stopMostRecentTimer() { try { stopTiming(RLOG.get().mostRecentTimer.getName()); } catch (NullPointerException ignored) { LOG.warn("Stopping timing failed because mostRecentTimer wasn't registered."); } }
/** * Stop the request timer, start the response timer, and then invoke the failure callback code. * * @param error The error that caused the failure */ default void dispatch(Throwable error) { RequestLog.stopTiming(REQUEST_WORKFLOW_TIMER); RequestLog.startTiming(RESPONSE_WORKFLOW_TIMER); invoke(error); } }
/** * Publish final response to user. * * @param response The Response to send back to the user */ private void publishResponse(Response response) { if (RequestLog.isRunning(RESPONSE_WORKFLOW_TIMER)) { RequestLog.stopTiming(RESPONSE_WORKFLOW_TIMER); } asyncResponse.resume(response); } }
/** * Sends the response back to the user. * * @param response The response to send to the user * @param asyncResponse The channel over which to send the response */ private void send(Response response, AsyncResponse asyncResponse) { if (RequestLog.isRunning(RESPONSE_WORKFLOW_TIMER)) { RequestLog.stopTiming(RESPONSE_WORKFLOW_TIMER); } asyncResponse.resume(response); }
@Override public void onThrowable(Throwable t) { RequestLog.restore(logCtx); RequestLog.stopTiming(timerName); if (outstanding.decrementAndGet() == 0) { RequestLog.startTiming(RESPONSE_WORKFLOW_TIMER); } exceptionMeter.mark(); LOG.error("druid {} request failed:", serviceConfig.getNameAndUrl(), t); failure.invoke(t); } });
@Override public void filter(ContainerRequestContext requestContext) throws IOException { RequestLog.startTiming(this); String path = requestContext.getUriInfo().getAbsolutePath().getPath(); if (path.startsWith("/v1/data") || path.startsWith("/data")) { // See if we have any unhealthy checks Map<String, Result> unhealthyChecks = getUnhealthy(); StringBuilder debugMsgBuilder = builderErrorResponseBody(requestContext); if (!unhealthyChecks.keySet().isEmpty()) { unhealthyChecks.forEach((key, value) -> LOG.error("Healthcheck '{}' failed: {}", key, value)); RequestLog.stopTiming(this); debugMsgBuilder.insert(0, "Service is unhealthy. At least 1 healthcheck is failing\n"); requestContext.abortWith( Response.status(Status.SERVICE_UNAVAILABLE) .entity(debugMsgBuilder.toString()) .build() ); return; } } RequestLog.stopTiming(this); }
/** * Stop the request timer, start the response timer, and then invoke the error callback code. * * @param statusCode Http status code of the error response * @param reasonPhrase The reason for the error. Often the status code description. * @param responseBody The body of the error response */ default void dispatch(int statusCode, String reasonPhrase, String responseBody) { RequestLog.stopTiming(REQUEST_WORKFLOW_TIMER); RequestLog.startTiming(RESPONSE_WORKFLOW_TIMER); invoke(statusCode, reasonPhrase, responseBody); } }
@Override public void filter(ClientRequestContext request, ClientResponseContext response) throws IOException { StringBuilder debugMsgBuilder = new StringBuilder(); debugMsgBuilder.append("ClientRequest: ").append(request.getMethod()).append("\t"); debugMsgBuilder.append(request.getUri().toASCIIString()).append("\t"); appendObjectHeaders(debugMsgBuilder, "> ", request.getHeaders().entrySet()); appendStringHeaders(debugMsgBuilder, "< ", response.getHeaders().entrySet()); debugMsgBuilder.append(response.getStatusInfo()).append(", length=").append(response.getLength()).append(" "); Long requestStartTime = (Long) request.getProperty(PROPERTY_NANOS); if (requestStartTime != null) { debugMsgBuilder .append((System.nanoTime() - requestStartTime) / MILLISECONDS_PER_NANOSECOND) .append(" ms\t"); } LOG.debug(debugMsgBuilder.toString()); RequestLog.stopTiming(CLIENT_TOTAL_TIMER); }
@Override public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext) throws IOException { RequestLog.startTiming(this); MultivaluedMap<String, Object> headers = responseContext.getHeaders(); String origin = requestContext.getHeaderString("origin"); if (origin == null || "".equals(origin)) { headers.add("Access-Control-Allow-Origin", "*"); } else { headers.add("Access-Control-Allow-Origin", origin); } String requestedHeaders = requestContext.getHeaderString("access-control-request-headers"); // allow all requested headers headers.add("Access-Control-Allow-Headers", requestedHeaders == null ? "" : requestedHeaders); headers.add("Access-Control-Allow-Methods", "GET, POST, DELETE, OPTIONS, PUT, PATCH"); headers.add("Access-Control-Allow-Credentials", "true"); RequestLog.stopTiming(this); } }
/** * Sorting the resultSet based on dateTime column sort direction. * * @param resultSet The result set need to be sorted in ascending or descending order * * @return sorted ResultSet */ @Override public ResultSet map(ResultSet resultSet) { Map<DateTime, List<Result>> bucketizedResultsMap = new LinkedHashMap<>(); RequestLog.startTiming("sortResultSet"); try { for (Result result : resultSet) { bucketizedResultsMap.computeIfAbsent(result.getTimeStamp(), ignored -> new ArrayList<>()).add(result); } List<DateTime> dateTimeList = new ArrayList<>(bucketizedResultsMap.keySet()); Collections.sort(dateTimeList, direction == SortDirection.ASC ? Comparator.naturalOrder() : Comparator.reverseOrder()); return new ResultSet( resultSet.getSchema(), dateTimeList.stream() .map(bucketizedResultsMap::get) .flatMap(List::stream) .collect(Collectors.toList()) ); } finally { RequestLog.stopTiming("sortResultSet"); } }
@Override @SuppressWarnings("checkstyle:cyclomaticcomplexity") public void filter(ContainerRequestContext request) throws IOException { RequestLog.startTiming(this); URI uri = request.getUriInfo().getAbsolutePath(); String path = uri.getPath(); // Determine if we should filter based on URL path if (path.startsWith(DATA_PATH) || path.startsWith("/test")) { RateLimitRequestToken token = rateLimiter.getToken(request); // Add the token to the request if it was bound if (token.isBound()) { request.setProperty(PROPERTY_TOKEN, token); } else { String msg = String.format("Rate limit reached. Reject %s", uri.toString()); LOG.debug(msg); RequestLog.stopTiming(this); request.abortWith(Response.status(RATE_LIMIT).entity(msg).build()); return; } } RequestLog.stopTiming(this); }
@Override public void invoke(int statusCode, String reason, String responseBody) { if (RequestLog.isRunning(REQUEST_WORKFLOW_TIMER)) { RequestLog.stopTiming(REQUEST_WORKFLOW_TIMER); } next.getErrorCallback(druidQuery).invoke(statusCode, reason, responseBody); } };
@Override public void filter(ClientRequestContext request, ClientResponseContext response) { RequestLog.stopTiming("TestLogWrapper"); RequestLog.log(); } }
@Override public void invoke(Throwable error) { if (RequestLog.isRunning(REQUEST_WORKFLOW_TIMER)) { RequestLog.stopTiming(REQUEST_WORKFLOW_TIMER); } next.getFailureCallback(druidQuery).invoke(error); } };
@Override public void filter(ContainerRequestContext request, ContainerResponseContext response) { RequestLog.stopTiming("TestLogWrapper"); RequestLog.log(); }
@Override public void filter(ContainerRequestContext request, ContainerResponseContext response) { RequestLog.stopTiming("TestLogWrapper"); RequestLog.log(); }
@Override public void filter(ClientRequestContext request, ClientResponseContext response) { RequestLog.stopTiming("TestLogWrapper"); RequestLog.log(); } }
/** * Accept request and reply with OK. * * @param uriInfo Information about the URL for the request * @param asyncResponse The response object to send the final response to */ @GET @Timed(name = "logTimed") @Metered(name = "logMetered") @ExceptionMetered(name = "logException") @Produces(MediaType.APPLICATION_JSON) @Path("/log") public void getSucceed(@Context UriInfo uriInfo, @Suspended AsyncResponse asyncResponse) { RequestLog.startTiming(this); try { Thread.sleep(200); } catch (InterruptedException ignore) { // Do nothing } RequestLog.stopTiming(this); Response response = Response.status(Response.Status.OK).build(); asyncResponse.resume(response); }
/** * Accept request and reply with OK. * * @param uriInfo Information about the URL for the request * @param asyncResponse The response object to send the final response to */ @GET @Timed(name = "logTimed") @Metered(name = "logMetered") @ExceptionMetered(name = "logException") @Produces(MediaType.APPLICATION_JSON) @Path("/log") public void getSucceed(@Context UriInfo uriInfo, @Suspended AsyncResponse asyncResponse) { RequestLog.startTiming(this); try { Thread.sleep(200); } catch (InterruptedException ignore) { // Do nothing } RequestLog.stopTiming(this); Response response = Response.status(Response.Status.OK).build(); asyncResponse.resume(response); }