View Javadoc

1   /**
2    *
3    */
4   package org.jmonit.features;
5   
6   import java.lang.reflect.Field;
7   import java.util.HashMap;
8   import java.util.Iterator;
9   import java.util.Map;
10  import java.util.concurrent.atomic.AtomicLong;
11  
12  import javax.servlet.http.HttpServletResponse;
13  
14  import org.jmonit.Monitor;
15  import org.jmonit.Monitoring;
16  import org.jmonit.events.HttpRequestServed;
17  import org.jmonit.events.MonitoringEvent;
18  import org.jmonit.events.MonitoringEventListener;
19  import org.jmonit.log.Log;
20  import org.jmonit.reporting.Visitable;
21  import org.jmonit.reporting.Visitor;
22  
23  /**
24   * @author ndeloof
25   */
26  public class HttpStatus
27      extends AbstractFeature<HttpStatus>
28      implements Visitable, MonitoringEventListener
29  {
30      /** logger */
31      private static Log log = Log.getLog( HttpStatus.class );
32  
33      /** Map of HTTP status counters */
34      private final Map<String, AtomicLong> status = new HashMap<String, AtomicLong>();
35  
36      /**
37       * {@inheritDoc}
38       * 
39       * @see org.jmonit.events.MonitoringEventListener#onMonitoringEvent(org.jmonit.events.MonitoringEvent)
40       */
41      public void onMonitoringEvent( MonitoringEvent event )
42      {
43          if ( event instanceof HttpRequestServed )
44          {
45              onHttpRequestServed( (HttpRequestServed) event );
46  
47          }
48      }
49  
50      private void onHttpRequestServed( HttpRequestServed event )
51      {
52          String name = event.getMonitor().getName();
53          int sc = event.getHttpStatus();
54          Monitor http = Monitoring.getMonitor( name + "~http" + sc );
55          http.tag( "perf" ).tag( "http" ).add( event.getTime() );
56  
57          setHttpStatus( sc );
58      }
59  
60      /**
61       *
62       */
63      public HttpStatus()
64      {
65          Field[] codes = HttpServletResponse.class.getFields();
66          for ( int i = 0; i < codes.length; i++ )
67          {
68              Field code = codes[i];
69              try
70              {
71                  status.put( String.valueOf( code.get( HttpServletResponse.class ) ),
72                      new AtomicLong() );
73              }
74              catch ( Exception e )
75              {
76                  // Failed to read a public field ?
77                  log.error( "Failed to get constants from the HttpServletResponse class", e );
78              }
79          }
80          status.put( "1xx", new AtomicLong() );
81          status.put( "2xx", new AtomicLong() );
82          status.put( "3xx", new AtomicLong() );
83          status.put( "4xx", new AtomicLong() );
84          status.put( "5xx", new AtomicLong() );
85      }
86  
87      public void setHttpStatus( int sc )
88      {
89          AtomicLong counter = status.get( String.valueOf( sc ) );
90          if ( counter != null )
91          {
92              counter.getAndAdd( 1 );
93          }
94          counter = status.get( String.valueOf( sc / 100 ) + "xx" );
95          if ( counter != null )
96          {
97              counter.getAndAdd( 1 );
98          }
99      }
100 
101     public void clear()
102     {
103         for ( Iterator<AtomicLong> iterator = status.values().iterator(); iterator.hasNext(); )
104         {
105             iterator.next().set( 0 );
106         }
107     }
108 
109     /**
110      * {@inheritDoc}
111      * 
112      * @see org.jmonit.spi.Plugin#getFeature()
113      */
114     public HttpStatus getFeature()
115     {
116         return this;
117     }
118 
119     /**
120      * {@inheritDoc}
121      * 
122      * @see org.jmonit.reporting.Visitable#accept(org.jmonit.reporting.Visitor)
123      */
124     public void accept( Visitor visitor )
125     {
126         Map<String, Object> attributes = new HashMap<String, Object>();
127         for ( Iterator<Map.Entry<String, AtomicLong>> iterator = status.entrySet().iterator(); iterator
128             .hasNext(); )
129         {
130             Map.Entry<String, AtomicLong> entry = iterator.next();
131             attributes.put( entry.getKey(), entry.getValue().longValue() );
132         }
133         visitor.visit( attributes );
134     }
135 
136     public static final org.jmonit.spi.Factory<HttpStatus> FACTORY =
137         new org.jmonit.spi.Factory<HttpStatus>()
138         {
139             public HttpStatus newPluginInstance()
140             {
141                 return new HttpStatus();
142             }
143         };
144 }