1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.eclipse.jetty.server;
20
21 import java.io.IOException;
22 import java.util.Arrays;
23 import java.util.concurrent.atomic.AtomicLong;
24
25 import org.eclipse.jetty.io.Connection;
26 import org.eclipse.jetty.util.annotation.ManagedAttribute;
27 import org.eclipse.jetty.util.annotation.ManagedObject;
28 import org.eclipse.jetty.util.annotation.ManagedOperation;
29 import org.eclipse.jetty.util.component.AbstractLifeCycle;
30 import org.eclipse.jetty.util.component.Container;
31 import org.eclipse.jetty.util.component.ContainerLifeCycle;
32 import org.eclipse.jetty.util.component.Dumpable;
33 import org.eclipse.jetty.util.statistic.CounterStatistic;
34 import org.eclipse.jetty.util.statistic.SampleStatistic;
35
36
37
38
39
40
41
42 @ManagedObject("Connector Statistics")
43 public class ConnectorStatistics extends AbstractLifeCycle implements Dumpable, Connection.Listener
44 {
45 private final AtomicLong _startMillis = new AtomicLong(-1L);
46 private final CounterStatistic _connectionStats = new CounterStatistic();
47 private final SampleStatistic _messagesIn = new SampleStatistic();
48 private final SampleStatistic _messagesOut = new SampleStatistic();
49 private final SampleStatistic _connectionDurationStats = new SampleStatistic();
50
51 @Override
52 public void onOpened(Connection connection)
53 {
54 connectionOpened();
55 }
56
57 @Override
58 public void onClosed(Connection connection)
59 {
60 connectionClosed(System.currentTimeMillis()-connection.getCreatedTimeStamp(),connection.getMessagesIn(),connection.getMessagesOut());
61 }
62
63 @ManagedAttribute("Total number of bytes received by this connector")
64 public int getBytesIn()
65 {
66
67 return -1;
68 }
69
70 @ManagedAttribute("Total number of bytes sent by this connector")
71 public int getBytesOut()
72 {
73
74 return -1;
75 }
76
77 @ManagedAttribute("Total number of connections seen by this connector")
78 public int getConnections()
79 {
80 return (int)_connectionStats.getTotal();
81 }
82
83 @ManagedAttribute("Connection duraton maximum in ms")
84 public long getConnectionsDurationMax()
85 {
86 return _connectionDurationStats.getMax();
87 }
88
89 @ManagedAttribute("Connection duraton mean in ms")
90 public double getConnectionsDurationMean()
91 {
92 return _connectionDurationStats.getMean();
93 }
94
95 @ManagedAttribute("Connection duraton standard deviation")
96 public double getConnectionsDurationStdDev()
97 {
98 return _connectionDurationStats.getStdDev();
99 }
100
101 @ManagedAttribute("Connection duraton total of all connections in ms")
102 public long getConnectionsDurationTotal()
103 {
104 return _connectionDurationStats.getTotal();
105 }
106
107 @ManagedAttribute("Messages In for all connections")
108 public int getMessagesIn()
109 {
110 return (int)_messagesIn.getTotal();
111 }
112
113 @ManagedAttribute("Messages In per connection maximum")
114 public int getConnectionsMessagesInMax()
115 {
116 return (int)_messagesIn.getMax();
117 }
118
119 @ManagedAttribute("Messages In per connection mean")
120 public double getConnectionsMessagesInMean()
121 {
122 return _messagesIn.getMean();
123 }
124
125 @ManagedAttribute("Messages In per connection standard deviation")
126 public double getConnectionsMessagesInStdDev()
127 {
128 return _messagesIn.getStdDev();
129 }
130
131 @ManagedAttribute("Connections open")
132 public int getConnectionsOpen()
133 {
134 return (int)_connectionStats.getCurrent();
135 }
136
137 @ManagedAttribute("Connections open maximum")
138 public int getConnectionsOpenMax()
139 {
140 return (int)_connectionStats.getMax();
141 }
142
143 @ManagedAttribute("Messages Out for all connections")
144 public int getMessagesOut()
145 {
146 return (int)_messagesIn.getTotal();
147 }
148
149 @ManagedAttribute("Connection statistics started ms since epoch")
150 public long getStartedMillis()
151 {
152 long start = _startMillis.get();
153 return start < 0 ? 0 : System.currentTimeMillis() - start;
154 }
155
156 @Override
157 public void doStart()
158 {
159 reset();
160 }
161
162 @Override
163 public void doStop()
164 {
165 }
166
167 @ManagedOperation("Reset the statistics")
168 public void reset()
169 {
170 _startMillis.set(System.currentTimeMillis());
171 _messagesIn.reset();
172 _messagesOut.reset();
173 _connectionStats.reset();
174 _connectionDurationStats.reset();
175 }
176
177 public void connectionOpened()
178 {
179 if (isStarted())
180 {
181 _connectionStats.increment();
182 }
183 }
184
185 public void connectionUpgraded(int messagesIn, int messagesOut)
186 {
187 if (isStarted())
188 {
189 _messagesIn.set(messagesIn);
190 _messagesOut.set(messagesOut);
191 }
192 }
193
194 public void connectionClosed(long duration, int messagesIn, int messagesOut)
195 {
196 if (isStarted())
197 {
198 _messagesIn.set(messagesIn);
199 _messagesOut.set(messagesOut);
200 _connectionStats.decrement();
201 _connectionDurationStats.set(duration);
202 }
203 }
204
205 @Override
206 @ManagedOperation("dump thread state")
207 public String dump()
208 {
209 return ContainerLifeCycle.dump(this);
210 }
211
212 @Override
213 public void dump(Appendable out, String indent) throws IOException
214 {
215 ContainerLifeCycle.dumpObject(out,this);
216 ContainerLifeCycle.dump(out,indent,Arrays.asList(new String[]{"connections="+_connectionStats,"duration="+_connectionDurationStats,"in="+_messagesIn,"out="+_messagesOut}));
217 }
218
219 public static void addToAllConnectors(Server server)
220 {
221 for (Connector connector : server.getConnectors())
222 {
223 if (connector instanceof Container)
224 ((Container)connector).addBean(new ConnectorStatistics());
225 }
226 }
227 }