]> WPIA git - gigi.git/blob - lib/jetty/org/eclipse/jetty/server/handler/StatisticsHandler.java
Importing upstream Jetty jetty-9.2.1.v20140609
[gigi.git] / lib / jetty / org / eclipse / jetty / server / handler / StatisticsHandler.java
1 //
2 //  ========================================================================
3 //  Copyright (c) 1995-2014 Mort Bay Consulting Pty. Ltd.
4 //  ------------------------------------------------------------------------
5 //  All rights reserved. This program and the accompanying materials
6 //  are made available under the terms of the Eclipse Public License v1.0
7 //  and Apache License v2.0 which accompanies this distribution.
8 //
9 //      The Eclipse Public License is available at
10 //      http://www.eclipse.org/legal/epl-v10.html
11 //
12 //      The Apache License v2.0 is available at
13 //      http://www.opensource.org/licenses/apache2.0.php
14 //
15 //  You may elect to redistribute this code under either of these licenses.
16 //  ========================================================================
17 //
18
19 package org.eclipse.jetty.server.handler;
20
21 import java.io.IOException;
22 import java.util.concurrent.Future;
23 import java.util.concurrent.TimeoutException;
24 import java.util.concurrent.atomic.AtomicInteger;
25 import java.util.concurrent.atomic.AtomicLong;
26 import java.util.concurrent.atomic.AtomicReference;
27
28 import javax.servlet.AsyncEvent;
29 import javax.servlet.AsyncListener;
30 import javax.servlet.ServletException;
31 import javax.servlet.http.HttpServletRequest;
32 import javax.servlet.http.HttpServletResponse;
33
34 import org.eclipse.jetty.server.AsyncContextEvent;
35 import org.eclipse.jetty.server.HttpChannelState;
36 import org.eclipse.jetty.server.Request;
37 import org.eclipse.jetty.server.Response;
38 import org.eclipse.jetty.util.FutureCallback;
39 import org.eclipse.jetty.util.annotation.ManagedAttribute;
40 import org.eclipse.jetty.util.annotation.ManagedObject;
41 import org.eclipse.jetty.util.annotation.ManagedOperation;
42 import org.eclipse.jetty.util.component.Graceful;
43 import org.eclipse.jetty.util.statistic.CounterStatistic;
44 import org.eclipse.jetty.util.statistic.SampleStatistic;
45
46 @ManagedObject("Request Statistics Gathering")
47 public class StatisticsHandler extends HandlerWrapper implements Graceful
48 {
49     private final AtomicLong _statsStartedAt = new AtomicLong();
50
51     private final CounterStatistic _requestStats = new CounterStatistic();
52     private final SampleStatistic _requestTimeStats = new SampleStatistic();
53     private final CounterStatistic _dispatchedStats = new CounterStatistic();
54     private final SampleStatistic _dispatchedTimeStats = new SampleStatistic();
55     private final CounterStatistic _asyncWaitStats = new CounterStatistic();
56
57     private final AtomicInteger _asyncDispatches = new AtomicInteger();
58     private final AtomicInteger _expires = new AtomicInteger();
59
60     private final AtomicInteger _responses1xx = new AtomicInteger();
61     private final AtomicInteger _responses2xx = new AtomicInteger();
62     private final AtomicInteger _responses3xx = new AtomicInteger();
63     private final AtomicInteger _responses4xx = new AtomicInteger();
64     private final AtomicInteger _responses5xx = new AtomicInteger();
65     private final AtomicLong _responsesTotalBytes = new AtomicLong();
66
67     private final AtomicReference<FutureCallback> _shutdown=new AtomicReference<>();
68     
69     private final AsyncListener _onCompletion = new AsyncListener()
70     {
71         @Override
72         public void onTimeout(AsyncEvent event) throws IOException
73         {
74             _expires.incrementAndGet();
75         }
76         
77         @Override
78         public void onStartAsync(AsyncEvent event) throws IOException
79         {
80             event.getAsyncContext().addListener(this);
81         }
82         
83         @Override
84         public void onError(AsyncEvent event) throws IOException
85         {
86         }
87
88         @Override
89         public void onComplete(AsyncEvent event) throws IOException
90         {
91             HttpChannelState state = ((AsyncContextEvent)event).getHttpChannelState();
92
93             Request request = state.getBaseRequest();
94             final long elapsed = System.currentTimeMillis()-request.getTimeStamp();
95
96             long d=_requestStats.decrement();
97             _requestTimeStats.set(elapsed);
98
99             updateResponse(request);
100
101             _asyncWaitStats.decrement();
102             
103             // If we have no more dispatches, should we signal shutdown?
104             if (d==0)
105             {
106                 FutureCallback shutdown = _shutdown.get();
107                 if (shutdown!=null)
108                     shutdown.succeeded();
109             }   
110         }
111     };
112
113     /**
114      * Resets the current request statistics.
115      */
116     @ManagedOperation(value="resets statistics", impact="ACTION")
117     public void statsReset()
118     {
119         _statsStartedAt.set(System.currentTimeMillis());
120
121         _requestStats.reset();
122         _requestTimeStats.reset();
123         _dispatchedStats.reset();
124         _dispatchedTimeStats.reset();
125         _asyncWaitStats.reset();
126
127         _asyncDispatches.set(0);
128         _expires.set(0);
129         _responses1xx.set(0);
130         _responses2xx.set(0);
131         _responses3xx.set(0);
132         _responses4xx.set(0);
133         _responses5xx.set(0);
134         _responsesTotalBytes.set(0L);
135     }
136
137     @Override
138     public void handle(String path, Request request, HttpServletRequest httpRequest, HttpServletResponse httpResponse) throws IOException, ServletException
139     {
140         _dispatchedStats.increment();
141
142         final long start;
143         HttpChannelState state = request.getHttpChannelState();
144         if (state.isInitial())
145         {
146             // new request
147             _requestStats.increment();
148             start = request.getTimeStamp();
149         }
150         else
151         {
152             // resumed request
153             start = System.currentTimeMillis();
154             _asyncDispatches.incrementAndGet();
155         }
156
157         try
158         {
159             super.handle(path, request, httpRequest, httpResponse);
160         }
161         finally
162         {
163             final long now = System.currentTimeMillis();
164             final long dispatched=now-start;
165
166             _dispatchedStats.decrement();
167             _dispatchedTimeStats.set(dispatched);
168
169             if (state.isSuspended())
170             {
171                 if (state.isInitial())
172                 {
173                     state.addListener(_onCompletion);
174                     _asyncWaitStats.increment();
175                 }
176             }
177             else if (state.isInitial())
178             {
179                 long d=_requestStats.decrement();
180                 _requestTimeStats.set(dispatched);
181                 updateResponse(request);
182                 
183                 // If we have no more dispatches, should we signal shutdown?
184                 FutureCallback shutdown = _shutdown.get();
185                 if (shutdown!=null)
186                 {
187                     httpResponse.flushBuffer();
188                     if (d==0)
189                         shutdown.succeeded();
190                 }   
191             }
192             // else onCompletion will handle it.
193         }
194     }
195
196     private void updateResponse(Request request)
197     {
198         Response response = request.getResponse();
199         switch (response.getStatus() / 100)
200         {
201             case 0:
202                 if (request.isHandled())
203                     _responses2xx.incrementAndGet();
204                 else
205                     _responses4xx.incrementAndGet();
206                 break;
207             case 1:
208                 _responses1xx.incrementAndGet();
209                 break;
210             case 2:
211                 _responses2xx.incrementAndGet();
212                 break;
213             case 3:
214                 _responses3xx.incrementAndGet();
215                 break;
216             case 4:
217                 _responses4xx.incrementAndGet();
218                 break;
219             case 5:
220                 _responses5xx.incrementAndGet();
221                 break;
222             default:
223                 break;
224         }
225         _responsesTotalBytes.addAndGet(response.getContentCount());
226     }
227
228     @Override
229     protected void doStart() throws Exception
230     {
231         _shutdown.set(null);
232         super.doStart();
233         statsReset();
234     }
235     
236
237     @Override
238     protected void doStop() throws Exception
239     {
240         super.doStop();
241         FutureCallback shutdown = _shutdown.get();
242         if (shutdown!=null && !shutdown.isDone())
243             shutdown.failed(new TimeoutException());
244     }
245
246     /**
247      * @return the number of requests handled by this handler
248      * since {@link #statsReset()} was last called, excluding
249      * active requests
250      * @see #getAsyncDispatches()
251      */
252     @ManagedAttribute("number of requests")
253     public int getRequests()
254     {
255         return (int)_requestStats.getTotal();
256     }
257
258     /**
259      * @return the number of requests currently active.
260      * since {@link #statsReset()} was last called.
261      */
262     @ManagedAttribute("number of requests currently active")
263     public int getRequestsActive()
264     {
265         return (int)_requestStats.getCurrent();
266     }
267
268     /**
269      * @return the maximum number of active requests
270      * since {@link #statsReset()} was last called.
271      */
272     @ManagedAttribute("maximum number of active requests")
273     public int getRequestsActiveMax()
274     {
275         return (int)_requestStats.getMax();
276     }
277
278     /**
279      * @return the maximum time (in milliseconds) of request handling
280      * since {@link #statsReset()} was last called.
281      */
282     @ManagedAttribute("maximum time spend handling requests (in ms)")
283     public long getRequestTimeMax()
284     {
285         return _requestTimeStats.getMax();
286     }
287
288     /**
289      * @return the total time (in milliseconds) of requests handling
290      * since {@link #statsReset()} was last called.
291      */
292     @ManagedAttribute("total time spend in all request handling (in ms)")
293     public long getRequestTimeTotal()
294     {
295         return _requestTimeStats.getTotal();
296     }
297
298     /**
299      * @return the mean time (in milliseconds) of request handling
300      * since {@link #statsReset()} was last called.
301      * @see #getRequestTimeTotal()
302      * @see #getRequests()
303      */
304     @ManagedAttribute("mean time spent handling requests (in ms)")
305     public double getRequestTimeMean()
306     {
307         return _requestTimeStats.getMean();
308     }
309
310     /**
311      * @return the standard deviation of time (in milliseconds) of request handling
312      * since {@link #statsReset()} was last called.
313      * @see #getRequestTimeTotal()
314      * @see #getRequests()
315      */
316     @ManagedAttribute("standard deviation for request handling (in ms)")
317     public double getRequestTimeStdDev()
318     {
319         return _requestTimeStats.getStdDev();
320     }
321
322     /**
323      * @return the number of dispatches seen by this handler
324      * since {@link #statsReset()} was last called, excluding
325      * active dispatches
326      */
327     @ManagedAttribute("number of dispatches")
328     public int getDispatched()
329     {
330         return (int)_dispatchedStats.getTotal();
331     }
332
333     /**
334      * @return the number of dispatches currently in this handler
335      * since {@link #statsReset()} was last called, including
336      * resumed requests
337      */
338     @ManagedAttribute("number of dispatches currently active")
339     public int getDispatchedActive()
340     {
341         return (int)_dispatchedStats.getCurrent();
342     }
343
344     /**
345      * @return the max number of dispatches currently in this handler
346      * since {@link #statsReset()} was last called, including
347      * resumed requests
348      */
349     @ManagedAttribute("maximum number of active dispatches being handled")
350     public int getDispatchedActiveMax()
351     {
352         return (int)_dispatchedStats.getMax();
353     }
354
355     /**
356      * @return the maximum time (in milliseconds) of request dispatch
357      * since {@link #statsReset()} was last called.
358      */
359     @ManagedAttribute("maximum time spend in dispatch handling")
360     public long getDispatchedTimeMax()
361     {
362         return _dispatchedTimeStats.getMax();
363     }
364
365     /**
366      * @return the total time (in milliseconds) of requests handling
367      * since {@link #statsReset()} was last called.
368      */
369     @ManagedAttribute("total time spent in dispatch handling (in ms)")
370     public long getDispatchedTimeTotal()
371     {
372         return _dispatchedTimeStats.getTotal();
373     }
374
375     /**
376      * @return the mean time (in milliseconds) of request handling
377      * since {@link #statsReset()} was last called.
378      * @see #getRequestTimeTotal()
379      * @see #getRequests()
380      */
381     @ManagedAttribute("mean time spent in dispatch handling (in ms)")
382     public double getDispatchedTimeMean()
383     {
384         return _dispatchedTimeStats.getMean();
385     }
386
387     /**
388      * @return the standard deviation of time (in milliseconds) of request handling
389      * since {@link #statsReset()} was last called.
390      * @see #getRequestTimeTotal()
391      * @see #getRequests()
392      */
393     @ManagedAttribute("standard deviation for dispatch handling (in ms)")
394     public double getDispatchedTimeStdDev()
395     {
396         return _dispatchedTimeStats.getStdDev();
397     }
398
399     /**
400      * @return the number of requests handled by this handler
401      * since {@link #statsReset()} was last called, including
402      * resumed requests
403      * @see #getAsyncDispatches()
404      */
405     @ManagedAttribute("total number of async requests")
406     public int getAsyncRequests()
407     {
408         return (int)_asyncWaitStats.getTotal();
409     }
410
411     /**
412      * @return the number of requests currently suspended.
413      * since {@link #statsReset()} was last called.
414      */
415     @ManagedAttribute("currently waiting async requests")
416     public int getAsyncRequestsWaiting()
417     {
418         return (int)_asyncWaitStats.getCurrent();
419     }
420
421     /**
422      * @return the maximum number of current suspended requests
423      * since {@link #statsReset()} was last called.
424      */
425     @ManagedAttribute("maximum number of waiting async requests")
426     public int getAsyncRequestsWaitingMax()
427     {
428         return (int)_asyncWaitStats.getMax();
429     }
430
431     /**
432      * @return the number of requests that have been asynchronously dispatched
433      */
434     @ManagedAttribute("number of requested that have been asynchronously dispatched")
435     public int getAsyncDispatches()
436     {
437         return _asyncDispatches.get();
438     }
439
440     /**
441      * @return the number of requests that expired while suspended.
442      * @see #getAsyncDispatches()
443      */
444     @ManagedAttribute("number of async requests requests that have expired")
445     public int getExpires()
446     {
447         return _expires.get();
448     }
449
450     /**
451      * @return the number of responses with a 1xx status returned by this context
452      * since {@link #statsReset()} was last called.
453      */
454     @ManagedAttribute("number of requests with 1xx response status")
455     public int getResponses1xx()
456     {
457         return _responses1xx.get();
458     }
459
460     /**
461      * @return the number of responses with a 2xx status returned by this context
462      * since {@link #statsReset()} was last called.
463      */
464     @ManagedAttribute("number of requests with 2xx response status")
465     public int getResponses2xx()
466     {
467         return _responses2xx.get();
468     }
469
470     /**
471      * @return the number of responses with a 3xx status returned by this context
472      * since {@link #statsReset()} was last called.
473      */
474     @ManagedAttribute("number of requests with 3xx response status")
475     public int getResponses3xx()
476     {
477         return _responses3xx.get();
478     }
479
480     /**
481      * @return the number of responses with a 4xx status returned by this context
482      * since {@link #statsReset()} was last called.
483      */
484     @ManagedAttribute("number of requests with 4xx response status")
485     public int getResponses4xx()
486     {
487         return _responses4xx.get();
488     }
489
490     /**
491      * @return the number of responses with a 5xx status returned by this context
492      * since {@link #statsReset()} was last called.
493      */
494     @ManagedAttribute("number of requests with 5xx response status")
495     public int getResponses5xx()
496     {
497         return _responses5xx.get();
498     }
499
500     /**
501      * @return the milliseconds since the statistics were started with {@link #statsReset()}.
502      */
503     @ManagedAttribute("time in milliseconds stats have been collected for")
504     public long getStatsOnMs()
505     {
506         return System.currentTimeMillis() - _statsStartedAt.get();
507     }
508
509     /**
510      * @return the total bytes of content sent in responses
511      */
512     @ManagedAttribute("total number of bytes across all responses")
513     public long getResponsesBytesTotal()
514     {
515         return _responsesTotalBytes.get();
516     }
517
518     public String toStatsHTML()
519     {
520         StringBuilder sb = new StringBuilder();
521
522         sb.append("<h1>Statistics:</h1>\n");
523         sb.append("Statistics gathering started ").append(getStatsOnMs()).append("ms ago").append("<br />\n");
524
525         sb.append("<h2>Requests:</h2>\n");
526         sb.append("Total requests: ").append(getRequests()).append("<br />\n");
527         sb.append("Active requests: ").append(getRequestsActive()).append("<br />\n");
528         sb.append("Max active requests: ").append(getRequestsActiveMax()).append("<br />\n");
529         sb.append("Total requests time: ").append(getRequestTimeTotal()).append("<br />\n");
530         sb.append("Mean request time: ").append(getRequestTimeMean()).append("<br />\n");
531         sb.append("Max request time: ").append(getRequestTimeMax()).append("<br />\n");
532         sb.append("Request time standard deviation: ").append(getRequestTimeStdDev()).append("<br />\n");
533
534
535         sb.append("<h2>Dispatches:</h2>\n");
536         sb.append("Total dispatched: ").append(getDispatched()).append("<br />\n");
537         sb.append("Active dispatched: ").append(getDispatchedActive()).append("<br />\n");
538         sb.append("Max active dispatched: ").append(getDispatchedActiveMax()).append("<br />\n");
539         sb.append("Total dispatched time: ").append(getDispatchedTimeTotal()).append("<br />\n");
540         sb.append("Mean dispatched time: ").append(getDispatchedTimeMean()).append("<br />\n");
541         sb.append("Max dispatched time: ").append(getDispatchedTimeMax()).append("<br />\n");
542         sb.append("Dispatched time standard deviation: ").append(getDispatchedTimeStdDev()).append("<br />\n");
543
544
545         sb.append("Total requests suspended: ").append(getAsyncRequests()).append("<br />\n");
546         sb.append("Total requests expired: ").append(getExpires()).append("<br />\n");
547         sb.append("Total requests resumed: ").append(getAsyncDispatches()).append("<br />\n");
548
549         sb.append("<h2>Responses:</h2>\n");
550         sb.append("1xx responses: ").append(getResponses1xx()).append("<br />\n");
551         sb.append("2xx responses: ").append(getResponses2xx()).append("<br />\n");
552         sb.append("3xx responses: ").append(getResponses3xx()).append("<br />\n");
553         sb.append("4xx responses: ").append(getResponses4xx()).append("<br />\n");
554         sb.append("5xx responses: ").append(getResponses5xx()).append("<br />\n");
555         sb.append("Bytes sent total: ").append(getResponsesBytesTotal()).append("<br />\n");
556
557         return sb.toString();
558
559     }
560
561     @Override
562     public Future<Void> shutdown()
563     {
564         FutureCallback shutdown=new FutureCallback(false);
565         _shutdown.compareAndSet(null,shutdown);
566         shutdown=_shutdown.get();
567         if (_dispatchedStats.getCurrent()==0)
568             shutdown.succeeded();
569         return shutdown;
570     }
571 }