View Javadoc
1   /* 
2    * Licensed under the Apache License, Version 2.0 (the "License");
3    * you may not use this file except in compliance with the License.
4    * You may obtain a copy of the License at
5    *
6    * http://www.apache.org/licenses/LICENSE-2.0
7    *
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  
16  package org.esigate.server.metrics;
17  
18  import java.io.IOException;
19  
20  import org.eclipse.jetty.server.ConnectionFactory;
21  import org.eclipse.jetty.server.Server;
22  import org.eclipse.jetty.server.ServerConnector;
23  import org.eclipse.jetty.util.annotation.Name;
24  import org.eclipse.jetty.util.ssl.SslContextFactory;
25  
26  import com.codahale.metrics.Counter;
27  import com.codahale.metrics.Meter;
28  import com.codahale.metrics.MetricRegistry;
29  
30  public class InstrumentedServerConnector extends ServerConnector {
31      private Meter accepts, connects, disconnects;
32      private Counter connections;
33  
34      /**
35       * Jetty 9 ServerConnector instrumented with Metrics.
36       * 
37       * @param id
38       *            connector id, will be used as prefix for metrics.
39       * @param port
40       *            this port will be set with ServerConnector#setPort().
41       * @param server
42       *            Jetty server.
43       */
44      public InstrumentedServerConnector(String id, int port, @Name("server") Server server, MetricRegistry registry) {
45          super(server);
46          instrument(id, port, registry);
47      }
48  
49      public InstrumentedServerConnector(String id, int port, @Name("server") Server server, MetricRegistry registry,
50              @Name("factories") ConnectionFactory... factories) {
51          super(server, factories);
52          instrument(id, port, registry);
53      }
54  
55      public InstrumentedServerConnector(String id, int port, @Name("server") Server server,
56              @Name("sslContextFactory") SslContextFactory sslContextFactory, MetricRegistry registry) {
57          super(server, sslContextFactory);
58          instrument(id, port, registry);
59      }
60  
61      @Override
62      public void accept(int acceptorID) throws IOException {
63          super.accept(acceptorID);
64          this.accepts.mark();
65      }
66  
67      @Override
68      public void close() {
69          super.close();
70          this.disconnects.mark();
71          this.connections.dec();
72      }
73  
74      /**
75       * Create metrics.
76       * 
77       * @param id
78       * @param port
79       * @param registry
80       */
81      private void instrument(String id, int port, MetricRegistry registry) {
82          this.setPort(port);
83          this.accepts = registry.meter(id + "-accepts");
84          this.connects = registry.meter(id + "-connects");
85          this.disconnects = registry.meter(id + "-disconnects");
86          this.connections = registry.counter(id + "-active-connections");
87  
88      }
89  
90      @Override
91      public void open() throws IOException {
92          this.connections.inc();
93          super.open();
94          this.connects.mark();
95      }
96  }