View Javadoc

    1   //========================================================================
    2   //Copyright 2006 Mort Bay Consulting Pty. Ltd.
    3   //------------------------------------------------------------------------
    4   //Licensed under the Apache License, Version 2.0 (the "License");
    5   //you may not use this file except in compliance with the License.
    6   //You may obtain a copy of the License at
    7   //http://www.apache.org/licenses/LICENSE-2.0
    8   //Unless required by applicable law or agreed to in writing, software
    9   //distributed under the License is distributed on an "AS IS" BASIS,
    10  //WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    11  //See the License for the specific language governing permissions and
    12  //limitations under the License.
    13  //========================================================================
    14
    15  package org.mortbay.jetty.handler;
    16
    17  import java.io.IOException;
    18
    19  import javax.servlet.ServletException;
    20  import javax.servlet.http.HttpServletRequest;
    21  import javax.servlet.http.HttpServletResponse;
    22
    23  import org.mortbay.jetty.HttpConnection;
    24  import org.mortbay.jetty.Request;
    25  import org.mortbay.jetty.Response;
    26
    27  public class StatisticsHandler extends HandlerWrapper
    28  {
    29      transient long _statsStartedAt;
    30
    31      transient int _requests;
    32
    33      transient long _requestsDurationMin;         // min request duration
    34      transient long _requestsDurationMax;         // max request duration
    35      transient long _requestsDurationTotal;       // total request duration
    36
    37      transient int _requestsActive;
    38      transient int _requestsActiveMin;            // min number of connections handled simultaneously
    39      transient int _requestsActiveMax;
    40      transient int _responses1xx; // Informal
    41      transient int _responses2xx; // Success
    42      transient int _responses3xx; // Redirection
    43      transient int _responses4xx; // Client Error
    44      transient int _responses5xx; // Server Error
    45
    46
    47      /* ------------------------------------------------------------ */
    48      public void statsReset()
    49      {
    50          synchronized(this)
    51          {
    52              if (isStarted())
    53                  _statsStartedAt=System.currentTimeMillis();
    54              _requests=0;
    55              _requestsActiveMax=_requestsActive;
    56              _responses1xx=0;
    57              _responses2xx=0;
    58              _responses3xx=0;
    59              _responses4xx=0;
    60              _responses5xx=0;
    61
    62              _requestsActiveMin=_requestsActive;
    63              _requestsActiveMax=_requestsActive;
    64              _requestsActive=0;
    65
    66              _requestsDurationMin=0;
    67              _requestsDurationMax=0;
    68              _requestsDurationTotal=0;
    69          }
    70      }
    71
    72
    73      /* ------------------------------------------------------------ */
    74      public void handle(String target, HttpServletRequest request, HttpServletResponse response, int dispatch) throws IOException, ServletException
    75      {
    76          final Request base_request=(request instanceof Request)?((Request)request):HttpConnection.getCurrentConnection().getRequest();
    77          final Response base_response=(response instanceof Response)?((Response)response):HttpConnection.getCurrentConnection().getResponse();
    78
    79          try
    80          {
    81              synchronized(this)
    82              {
    83                  _requests++;
    84                  _requestsActive++;
    85                  if (_requestsActive>_requestsActiveMax)
    86                      _requestsActiveMax=_requestsActive;
    87              }
    88
    89              super.handle(target, request, response, dispatch);
    90          }
    91          finally
    92          {
    93              synchronized(this)
    94              {
    95                  _requestsActive--;
    96                  if (_requestsActive<0)
    97                      _requestsActive=0;
    98                  if (_requestsActive < _requestsActiveMin)
    99                      _requestsActiveMin=_requestsActive;
    100
    101                 long duration = System.currentTimeMillis()-base_request.getTimeStamp();
    102
    103                 _requestsDurationTotal+=duration;
    104                 if (_requestsDurationMin==0 || duration<_requestsDurationMin)
    105                     _requestsDurationMin=duration;
    106                 if (duration>_requestsDurationMax)
    107                     _requestsDurationMax=duration;
    108
    109                 switch(base_response.getStatus()/100)
    110                 {
    111                     case 1: _responses1xx++;break;
    112                     case 2: _responses2xx++;break;
    113                     case 3: _responses3xx++;break;
    114                     case 4: _responses4xx++;break;
    115                     case 5: _responses5xx++;break;
    116                 }
    117
    118             }
    119         }
    120     }
    121
    122     /* ------------------------------------------------------------ */
    123     protected void doStart() throws Exception
    124     {
    125         super.doStart();
    126         _statsStartedAt=System.currentTimeMillis();
    127     }
    128
    129     /* ------------------------------------------------------------ */
    130     protected void doStop() throws Exception
    131     {
    132         super.doStop();
    133     }
    134
    135     /* ------------------------------------------------------------ */
    136     /**
    137      * @return Get the number of requests handled by this context
    138      * since last call of statsReset(). If setStatsOn(false) then this
    139      * is undefined.
    140      */
    141     public int getRequests() {return _requests;}
    142
    143     /* ------------------------------------------------------------ */
    144     /**
    145      * @return Number of requests currently active.
    146      * Undefined if setStatsOn(false).
    147      */
    148     public int getRequestsActive() {return _requestsActive;}
    149
    150     /* ------------------------------------------------------------ */
    151     /**
    152      * @return Maximum number of active requests
    153      * since statsReset() called. Undefined if setStatsOn(false).
    154      */
    155     public int getRequestsActiveMax() {return _requestsActiveMax;}
    156
    157     /* ------------------------------------------------------------ */
    158     /**
    159      * @return Get the number of responses with a 2xx status returned
    160      * by this context since last call of statsReset(). Undefined if
    161      * if setStatsOn(false).
    162      */
    163     public int getResponses1xx() {return _responses1xx;}
    164
    165     /* ------------------------------------------------------------ */
    166     /**
    167      * @return Get the number of responses with a 100 status returned
    168      * by this context since last call of statsReset(). Undefined if
    169      * if setStatsOn(false).
    170      */
    171     public int getResponses2xx() {return _responses2xx;}
    172
    173     /* ------------------------------------------------------------ */
    174     /**
    175      * @return Get the number of responses with a 3xx status returned
    176      * by this context since last call of statsReset(). Undefined if
    177      * if setStatsOn(false).
    178      */
    179     public int getResponses3xx() {return _responses3xx;}
    180
    181     /* ------------------------------------------------------------ */
    182     /**
    183      * @return Get the number of responses with a 4xx status returned
    184      * by this context since last call of statsReset(). Undefined if
    185      * if setStatsOn(false).
    186      */
    187     public int getResponses4xx() {return _responses4xx;}
    188
    189     /* ------------------------------------------------------------ */
    190     /**
    191      * @return Get the number of responses with a 5xx status returned
    192      * by this context since last call of statsReset(). Undefined if
    193      * if setStatsOn(false).
    194      */
    195     public int getResponses5xx() {return _responses5xx;}
    196
    197     /* ------------------------------------------------------------ */
    198     /**
    199      * @return Timestamp stats were started at.
    200      */
    201     public long getStatsOnMs()
    202     {
    203         return System.currentTimeMillis()-_statsStartedAt;
    204     }
    205
    206     /* ------------------------------------------------------------ */
    207     /**
    208      * @return Returns the requestsActiveMin.
    209      */
    210     public int getRequestsActiveMin()
    211     {
    212         return _requestsActiveMin;
    213     }
    214
    215     /* ------------------------------------------------------------ */
    216     /**
    217      * @return Returns the requestsDurationMin.
    218      */
    219     public long getRequestsDurationMin()
    220     {
    221         return _requestsDurationMin;
    222     }
    223
    224     /* ------------------------------------------------------------ */
    225     /**
    226      * @return Returns the requestsDurationTotal.
    227      */
    228     public long getRequestsDurationTotal()
    229     {
    230         return _requestsDurationTotal;
    231     }
    232
    233     /* ------------------------------------------------------------ */
    234     /**
    235      * @return Average duration of request handling in milliseconds
    236      * since statsReset() called. Undefined if setStatsOn(false).
    237      */
    238     public long getRequestsDurationAve() {return _requests==0?0:(_requestsDurationTotal/_requests);}
    239
    240     /* ------------------------------------------------------------ */
    241     /**
    242      * @return Get maximum duration in milliseconds of request handling
    243      * since statsReset() called. Undefined if setStatsOn(false).
    244      */
    245     public long getRequestsDurationMax() {return _requestsDurationMax;}
    246
    247
    248 }