protected Handler addStatsHandler(Handler handler) { // Graceful shutdown is implemented via the statistics handler, // see https://bugs.eclipse.org/bugs/show_bug.cgi?id=420142 final StatisticsHandler statisticsHandler = new StatisticsHandler(); statisticsHandler.setHandler(handler); return statisticsHandler; }
@Override public List<MetricFamilySamples> collect() { return Arrays.asList( buildCounter("jetty_requests_total", "Number of requests", statisticsHandler.getRequests()), buildGauge("jetty_requests_active", "Number of requests currently active", statisticsHandler.getRequestsActive()), buildGauge("jetty_requests_active_max", "Maximum number of requests that have been active at once", statisticsHandler.getRequestsActiveMax()), buildGauge("jetty_request_time_max_seconds", "Maximum time spent handling requests", statisticsHandler.getRequestTimeMax() / 1000.0), buildCounter("jetty_request_time_seconds_total", "Total time spent in all request handling", statisticsHandler.getRequestTimeTotal() / 1000.0), buildCounter("jetty_dispatched_total", "Number of dispatches", statisticsHandler.getDispatched()), buildGauge("jetty_dispatched_active", "Number of dispatches currently active", statisticsHandler.getDispatchedActive()), buildGauge("jetty_dispatched_active_max", "Maximum number of active dispatches being handled", statisticsHandler.getDispatchedActiveMax()), buildGauge("jetty_dispatched_time_max", "Maximum time spent in dispatch handling", statisticsHandler.getDispatchedTimeMax()), buildCounter("jetty_dispatched_time_seconds_total", "Total time spent in dispatch handling", statisticsHandler.getDispatchedTimeTotal() / 1000.0), buildCounter("jetty_async_requests_total", "Total number of async requests", statisticsHandler.getAsyncRequests()), buildGauge("jetty_async_requests_waiting", "Currently waiting async requests", statisticsHandler.getAsyncRequestsWaiting()), buildGauge("jetty_async_requests_waiting_max", "Maximum number of waiting async requests", statisticsHandler.getAsyncRequestsWaitingMax()), buildCounter("jetty_async_dispatches_total", "Number of requested that have been asynchronously dispatched", statisticsHandler.getAsyncDispatches()), buildCounter("jetty_expires_total", "Number of async requests requests that have expired", statisticsHandler.getExpires()), buildStatusCounter(), buildGauge("jetty_stats_seconds", "Time in seconds stats have been collected for", statisticsHandler.getStatsOnMs() / 1000.0), buildCounter("jetty_responses_bytes_total", "Total number of bytes across all responses", statisticsHandler.getResponsesBytesTotal()) ); }
sb.append("Statistics gathering started ").append(getStatsOnMs()).append("ms ago").append("<br />\n"); sb.append("Total requests: ").append(getRequests()).append("<br />\n"); sb.append("Active requests: ").append(getRequestsActive()).append("<br />\n"); sb.append("Max active requests: ").append(getRequestsActiveMax()).append("<br />\n"); sb.append("Total requests time: ").append(getRequestTimeTotal()).append("<br />\n"); sb.append("Mean request time: ").append(getRequestTimeMean()).append("<br />\n"); sb.append("Max request time: ").append(getRequestTimeMax()).append("<br />\n"); sb.append("Request time standard deviation: ").append(getRequestTimeStdDev()).append("<br />\n"); sb.append("Total dispatched: ").append(getDispatched()).append("<br />\n"); sb.append("Active dispatched: ").append(getDispatchedActive()).append("<br />\n"); sb.append("Max active dispatched: ").append(getDispatchedActiveMax()).append("<br />\n"); sb.append("Total dispatched time: ").append(getDispatchedTimeTotal()).append("<br />\n"); sb.append("Mean dispatched time: ").append(getDispatchedTimeMean()).append("<br />\n"); sb.append("Max dispatched time: ").append(getDispatchedTimeMax()).append("<br />\n"); sb.append("Dispatched time standard deviation: ").append(getDispatchedTimeStdDev()).append("<br />\n"); sb.append("Total requests suspended: ").append(getAsyncRequests()).append("<br />\n"); sb.append("Total requests expired: ").append(getExpires()).append("<br />\n"); sb.append("Total requests resumed: ").append(getAsyncDispatches()).append("<br />\n"); sb.append("1xx responses: ").append(getResponses1xx()).append("<br />\n"); sb.append("2xx responses: ").append(getResponses2xx()).append("<br />\n"); sb.append("3xx responses: ").append(getResponses3xx()).append("<br />\n"); sb.append("4xx responses: ").append(getResponses4xx()).append("<br />\n"); sb.append("5xx responses: ").append(getResponses5xx()).append("<br />\n"); sb.append("Bytes sent total: ").append(getResponsesBytesTotal()).append("<br />\n");
htmlStaticHandler.setUnknownCipherKeyLength(unknownCipherKeyLength); StatisticsHandler statisticsHandler = new StatisticsHandler(); statisticsHandler.setServer(server.getJettyServer()); handlerCollection.addHandler(htmlStaticHandler); statisticsHandler.setHandler(handlerCollection);
public void run() throws Exception { Server server = new Server(settings.getInt(JettyConf.PORT)); HandlerList handlers = new HandlerList(); if (settings.getString(JettyConf.SHUTDOWN_TOKEN) != null) handlers.addHandler(new ShutdownHandler(settings.getString(JettyConf.SHUTDOWN_TOKEN), false, true)); ServletContextHandler handler = new ServletContextHandler(server, settings.getString(JettyConf.CONTEXT_PATH)); handler.addFilter(GuiceFilter.class, "/*", EnumSet.allOf(DispatcherType.class)); handler.addEventListener(new OxalisGuiceContextListener(injector)); handler.addServlet(DefaultServlet.class, "/"); handlers.addHandler(handler); handlers.addHandler(new StatisticsHandler()); server.setHandler(handlers); server.setStopTimeout(settings.getInt(JettyConf.STOP_TIMEOUT)); server.setStopAtShutdown(true); log.info("Starting server"); server.start(); server.join(); } }
private StatisticsHandler newStatisticsHandler() { StatisticsHandler statisticsHandler = new StatisticsHandler(); statisticsHandler.statsReset(); return statisticsHandler; }
@ManagementOperation(description = "The number of requests currently active") public int getRequestsActive() { return super.getRequestsActive(); }
/** * Return the number of active requests. */ protected int activeRequestCount() { if (statistics != null && statistics.isStarted()) { return statistics.getRequestsActive(); } return 0; }
private HandlerCollection getHandlerCollection( ServerConfig serverConfig, ServletPathsConfig servletPathsConfig, ServletHolder jdiscServlet, ComponentRegistry<ServletHolder> servletHolders, FilterHolder jDiscFilterInvokerFilter) { ServletContextHandler servletContextHandler = createServletContextHandler(); servletHolders.allComponentsById().forEach((id, servlet) -> { String path = getServletPath(servletPathsConfig, id); servletContextHandler.addServlet(servlet, path); servletContextHandler.addFilter(jDiscFilterInvokerFilter, path, EnumSet.allOf(DispatcherType.class)); }); servletContextHandler.addServlet(jdiscServlet, "/*"); GzipHandler gzipHandler = newGzipHandler(serverConfig); gzipHandler.setHandler(servletContextHandler); HttpResponseStatisticsCollector statisticsCollector = new HttpResponseStatisticsCollector(); statisticsCollector.setHandler(gzipHandler); StatisticsHandler statisticsHandler = newStatisticsHandler(); statisticsHandler.setHandler(statisticsCollector); HandlerCollection handlerCollection = new HandlerCollection(); handlerCollection.setHandlers(new Handler[] { statisticsHandler }); return handlerCollection; }
@ManagementOperation(description = "The number of dispatches seen by this handler") public int getDispatched() { return super.getDispatched(); }
@ManagementOperation(description = "The maximum time (in milliseconds) of request dispatch ") public long getDispatchedTimeMax() { return super.getDispatchedTimeMax(); }
@ManagementOperation(description = "The number of requests that expired while suspended") public int getExpires() { return super.getExpires(); }
@ManagementOperation(description = "The number of dispatches currently in this handler") public int getDispatchedActive() { return super.getDispatchedActive(); }
@ManagementOperation(description = "The max number of dispatches currently in this handler ") public int getDispatchedActiveMax() { return super.getDispatchedActiveMax(); }
@ManagementOperation(description = "The maximum time (in milliseconds) of request handling") public long getRequestTimeMax() { return super.getRequestTimeMax(); }
@ManagementOperation(description = "The total time (in milliseconds) of requests handling ") public long getDispatchedTimeTotal() { return super.getDispatchedTimeTotal(); }
sb.append("Statistics gathering started ").append(getStatsOnMs()).append("ms ago").append("<br />\n"); sb.append("Total requests: ").append(getRequests()).append("<br />\n"); sb.append("Active requests: ").append(getRequestsActive()).append("<br />\n"); sb.append("Max active requests: ").append(getRequestsActiveMax()).append("<br />\n"); sb.append("Total requests time: ").append(getRequestTimeTotal()).append("<br />\n"); sb.append("Mean request time: ").append(getRequestTimeMean()).append("<br />\n"); sb.append("Max request time: ").append(getRequestTimeMax()).append("<br />\n"); sb.append("Request time standard deviation: ").append(getRequestTimeStdDev()).append("<br />\n"); sb.append("Total dispatched: ").append(getDispatched()).append("<br />\n"); sb.append("Active dispatched: ").append(getDispatchedActive()).append("<br />\n"); sb.append("Max active dispatched: ").append(getDispatchedActiveMax()).append("<br />\n"); sb.append("Total dispatched time: ").append(getDispatchedTimeTotal()).append("<br />\n"); sb.append("Mean dispatched time: ").append(getDispatchedTimeMean()).append("<br />\n"); sb.append("Max dispatched time: ").append(getDispatchedTimeMax()).append("<br />\n"); sb.append("Dispatched time standard deviation: ").append(getDispatchedTimeStdDev()).append("<br />\n"); sb.append("Total requests suspended: ").append(getSuspends()).append("<br />\n"); sb.append("Total requests expired: ").append(getExpires()).append("<br />\n"); sb.append("Total requests resumed: ").append(getResumes()).append("<br />\n"); sb.append("1xx responses: ").append(getResponses1xx()).append("<br />\n"); sb.append("2xx responses: ").append(getResponses2xx()).append("<br />\n"); sb.append("3xx responses: ").append(getResponses3xx()).append("<br />\n"); sb.append("4xx responses: ").append(getResponses4xx()).append("<br />\n"); sb.append("5xx responses: ").append(getResponses5xx()).append("<br />\n"); sb.append("Bytes sent total: ").append(getResponsesBytesTotal()).append("<br />\n");
MBeanServer mbs = java.lang.management.ManagementFactory.getPlatformMBeanServer(); server.addBean(new MBeanContainer(mbs)); handlers.addHandler(new StatisticsHandler());
private void waitForConnections(long timeout, int open) { logger.info("jetty shutdown: {} requests are active, delaying for {} ms", open, timeout); timeout += System.currentTimeMillis(); while (true) { try { Thread.sleep(100); } catch (InterruptedException e) { logger.warn("jetty shutdown: clean shutdown failed sleep interval"); } open = statistics.getRequestsActive(); if (open <= 0) break; if (System.currentTimeMillis() >= timeout) { logger.warn("jetty shutdown: {} requests not finished, kicking them out", open); break; } } }
private void attemptCleanClose() { long timeout = Integer.getInteger(WEBAPP_SHUTDOWN_TIMEOUT_PROPERTY, WEBAPP_SHUTDOWN_TIMEOUT_DEFAULT); if (timeout > 0) { logger.info("jetty shutdown: requesting shutdown"); try { Connector[] connectors = server.getConnectors(); if (connectors != null) { for (Connector connector : connectors) { connector.shutdown(); } } if (statistics != null && statistics.isStarted()) { int open = statistics.getRequestsActive(); if (open > 0) { waitForConnections(timeout, open); } } } catch (Exception e) { logger.warn("jetty shutdown: formal shutdown failed", e); } } }