1
2
3
4
5
6
7
8
9
10
11
12
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
36
37
38
39
40
41
42
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
76
77
78
79
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 }