List of usage examples for com.google.common.base Stopwatch createUnstarted
@CheckReturnValue public static Stopwatch createUnstarted(Ticker ticker)
From source file:com.google.api.control.ControlFilter.java
@Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { if (client == null) { log.log(Level.INFO, String.format("No control client was created - skipping service control")); chain.doFilter(request, response); return;//from w ww .j ava 2s . c o m } if (projectId == null) { log.log(Level.INFO, String.format("No project Id was specified - skipping service control")); chain.doFilter(request, response); return; } // Start tracking the latency LatencyTimer timer = new LatencyTimer(ticker); // Service Control is not required for this method, execute the rest of // the filter chain MethodRegistry.Info info = ConfigFilter.getMethodInfo(request); HttpServletRequest httpRequest = (HttpServletRequest) request; if (info == null) { if (log.isLoggable(Level.FINE)) { log.log(Level.FINE, String.format("no method corresponds to %s - skipping service control", httpRequest.getRequestURI())); } chain.doFilter(request, response); return; } // Internal stats tracking Stopwatch creationTimer = Stopwatch.createUnstarted(ticker); Stopwatch overallTimer = Stopwatch.createStarted(ticker); // Perform the check AppStruct appInfo = new AppStruct(); appInfo.httpMethod = httpRequest.getMethod(); appInfo.requestSize = httpRequest.getContentLength(); appInfo.url = httpRequest.getRequestURI(); CheckRequestInfo checkInfo = createCheckInfo(httpRequest, appInfo.url, info); CheckErrorInfo errorInfo; CheckResponse checkResponse = null; if (Strings.isNullOrEmpty(checkInfo.getApiKey()) && !info.shouldAllowUnregisteredCalls()) { errorInfo = CheckErrorInfo.API_KEY_NOT_PROVIDED; if (log.isLoggable(Level.FINE)) { log.log(Level.FINE, String.format("no api key was provided")); } } else { creationTimer.reset().start(); CheckRequest checkRequest = checkInfo.asCheckRequest(clock); statistics.totalChecks.incrementAndGet(); statistics.totalCheckCreationTime.addAndGet(creationTimer.elapsed(TimeUnit.MILLISECONDS)); if (log.isLoggable(Level.FINE)) { log.log(Level.FINE, String.format("checking using %s", checkRequest)); } checkResponse = client.check(checkRequest); errorInfo = CheckErrorInfo.convert(checkResponse); } // Handle check failures. This includes check transport failures, in // which case the checkResponse is null. if (errorInfo != CheckErrorInfo.OK) { log.log(Level.WARNING, String.format("the check did not succeed; the response %s", checkResponse)); // ensure the report request is created with updated api_key validity, as this determines // the consumer id checkInfo.setApiKeyValid(!errorInfo.isApiKeyError()); appInfo.responseCode = errorInfo.getHttpCode(); // 'Send' a report, end the latency timer to collect correct overhead and backend latencies timer.end(); ReportRequest reportRequest = createReportRequest(info, checkInfo, appInfo, ConfigFilter.getReportRule(request), timer); if (log.isLoggable(Level.FINEST)) { log.log(Level.FINEST, String.format("sending an error report request %s", reportRequest)); } client.report(reportRequest); if (errorInfo == CheckErrorInfo.API_KEY_NOT_PROVIDED) { // a needed API key was not provided HttpServletResponse httpResponse = (HttpServletResponse) response; httpResponse.sendError(errorInfo.getHttpCode(), errorInfo.getMessage()); } else if (checkResponse == null) { // the check did not complete: 'fail open' chain.doFilter(request, response); } else { // the checked failed: assume that any error information will be in the first check error HttpServletResponse httpResponse = (HttpServletResponse) response; httpResponse.sendError(errorInfo.getHttpCode(), errorInfo.fullMessage(projectId, checkResponse.getCheckErrors(0).getDetail())); } statistics.totalFiltered.incrementAndGet(); statistics.totalFilteredTime.addAndGet(overallTimer.elapsed(TimeUnit.MILLISECONDS)); logStatistics(); return; } // Execute the request in wrapper, capture the response, then write it to the output GenericResponseWrapper wrapper = new GenericResponseWrapper((HttpServletResponse) response); try { timer.appStart(); chain.doFilter(request, wrapper); } finally { timer.end(); ServletOutputStream out = response.getOutputStream(); out.write(wrapper.getData()); out.close(); } // Send a report appInfo.responseCode = wrapper.getResponseCode(); appInfo.responseSize = wrapper.getContentLength() != 0 ? wrapper.getContentLength() : wrapper.getData().length; creationTimer.reset().start(); ReportRequest reportRequest = createReportRequest(info, checkInfo, appInfo, ConfigFilter.getReportRule(request), timer); statistics.totalReports.incrementAndGet(); statistics.totalReportCreationTime.addAndGet(creationTimer.elapsed(TimeUnit.MILLISECONDS)); if (log.isLoggable(Level.FINEST)) { log.log(Level.FINEST, String.format("sending a report request %s", reportRequest)); } client.report(reportRequest); statistics.totalFiltered.incrementAndGet(); statistics.totalFilteredTime.addAndGet(overallTimer.elapsed(TimeUnit.MILLISECONDS)); logStatistics(); }
From source file:com.google.api.control.Client.java
private void flushAndScheduleChecks() { if (resetIfStopped()) { log.log(Level.FINE, "did not schedule check flush: client is stopped"); return;//from w w w . j av a 2 s .com } int interval = checkAggregator.getFlushIntervalMillis(); if (interval < 0) { log.log(Level.FINE, "did not schedule check flush: caching is disabled"); return; // cache is disabled, so no flushing it } if (isRunningSchedulerDirectly()) { log.log(Level.FINE, "did not schedule check flush: no scheduler thread is running"); return; } log.log(Level.FINE, "flushing the check aggregator"); Stopwatch w = Stopwatch.createUnstarted(ticker); for (CheckRequest req : checkAggregator.flush()) { try { statistics.recachedChecks.incrementAndGet(); w.reset().start(); CheckResponse resp = transport.services().check(serviceName, req).execute(); statistics.totalCheckTransportTimeMillis.addAndGet(w.elapsed(TimeUnit.MILLISECONDS)); w.reset().start(); checkAggregator.addResponse(req, resp); statistics.totalCheckCacheUpdateTimeMillis.addAndGet(w.elapsed(TimeUnit.MILLISECONDS)); } catch (IOException e) { log.log(Level.SEVERE, String.format("direct send of a check request %s failed because of %s", req, e)); } } scheduler.enter(new Runnable() { @Override public void run() { flushAndScheduleChecks(); // Do this again after the interval } }, interval, 0 /* high priority */); }
From source file:com.google.api.control.Client.java
private void flushAndScheduleReports() { if (resetIfStopped()) { log.log(Level.FINE, "did not schedule report flush: client is stopped"); return;/* w w w. j a va2s . c om*/ } int interval = reportAggregator.getFlushIntervalMillis(); if (interval < 0) { log.log(Level.FINE, "did not schedule report flush: cache is disabled"); return; // cache is disabled, so no flushing it } ReportRequest[] flushed = reportAggregator.flush(); if (log.isLoggable(Level.FINE)) { log.log(Level.FINE, String.format("flushing %d reports from the report aggregator", flushed.length)); } statistics.flushedReports.addAndGet(flushed.length); Stopwatch w = Stopwatch.createUnstarted(ticker); for (ReportRequest req : flushed) { try { statistics.flushedOperations.addAndGet(req.getOperationsCount()); w.reset().start(); transport.services().report(serviceName, req).execute(); statistics.totalTransportedReportTimeMillis.addAndGet(w.elapsed(TimeUnit.MILLISECONDS)); } catch (IOException e) { log.log(Level.SEVERE, String.format("direct send of a report request failed because of %s", e)); } } scheduler.enter(new Runnable() { @Override public void run() { flushAndScheduleReports(); // Do this again after the interval } }, interval, 1 /* not so high priority */); }