/
JettyConnectionMetrics.java
263 lines (227 loc) · 9.68 KB
/
JettyConnectionMetrics.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
/*
* Copyright 2019 VMware, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.micrometer.core.instrument.binder.jetty;
import io.micrometer.common.lang.Nullable;
import io.micrometer.common.util.internal.logging.InternalLogger;
import io.micrometer.common.util.internal.logging.InternalLoggerFactory;
import io.micrometer.core.instrument.*;
import io.micrometer.core.instrument.binder.BaseUnits;
import io.micrometer.core.instrument.distribution.DistributionStatisticConfig;
import io.micrometer.core.instrument.distribution.TimeWindowMax;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.Socket;
import java.nio.ByteBuffer;
import org.eclipse.jetty.io.Connection;
import org.eclipse.jetty.io.NetworkTrafficListener;
import org.eclipse.jetty.server.Connector;
import org.eclipse.jetty.server.NetworkTrafficServerConnector;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.util.component.AbstractLifeCycle;
import java.util.HashMap;
import java.util.Map;
/**
* Jetty connection metrics.<br>
* <br>
* <p>
* Usage example:
*
* <pre>{@code
* MeterRegistry registry = ...;
* Server server = new Server(0);
* Connector connector = new ServerConnector(server);
* connector.addBean(new JettyConnectionMetrics(registry));
* server.setConnectors(new Connector[] { connector });
* }</pre>
*
* Alternatively, configure on all server connectors with
* {@link JettyConnectionMetrics#addToAllConnectors(Server, MeterRegistry, Iterable)}.
*
* @author Jon Schneider
* @since 1.4.0
*/
public class JettyConnectionMetrics extends AbstractLifeCycle implements Connection.Listener, NetworkTrafficListener {
private static final InternalLogger logger = InternalLoggerFactory.getInstance(JettyConnectionMetrics.class);
private final MeterRegistry registry;
private final Iterable<Tag> tags;
private final Object connectionSamplesLock = new Object();
private final Map<Connection, Timer.Sample> connectionSamples = new HashMap<>();
private final Counter messagesIn;
private final Counter messagesOut;
private final DistributionSummary bytesIn;
private final DistributionSummary bytesOut;
private final TimeWindowMax maxConnections;
public JettyConnectionMetrics(MeterRegistry registry) {
this(registry, Tags.empty());
}
public JettyConnectionMetrics(MeterRegistry registry, Iterable<Tag> tags) {
this.registry = registry;
this.tags = tags;
this.messagesIn = Counter.builder("jetty.connections.messages.in")
.baseUnit(BaseUnits.MESSAGES)
.description("Messages received by tracked connections")
.tags(tags)
.register(registry);
this.messagesOut = Counter.builder("jetty.connections.messages.out")
.baseUnit(BaseUnits.MESSAGES)
.description("Messages sent by tracked connections")
.tags(tags)
.register(registry);
this.bytesIn = DistributionSummary.builder("jetty.connections.bytes.in")
.baseUnit(BaseUnits.BYTES)
.description("Bytes received by tracked connections")
.tags(tags)
.register(registry);
this.bytesOut = DistributionSummary.builder("jetty.connections.bytes.out")
.baseUnit(BaseUnits.BYTES)
.description("Bytes sent by tracked connections")
.tags(tags)
.register(registry);
this.maxConnections = new TimeWindowMax(registry.config().clock(), DistributionStatisticConfig.DEFAULT);
Gauge.builder("jetty.connections.max", this, jcm -> jcm.maxConnections.poll())
.strongReference(true)
.baseUnit(BaseUnits.CONNECTIONS)
.description("The maximum number of observed connections over a rolling 2-minute interval")
.tags(tags)
.register(registry);
Gauge.builder("jetty.connections.current", this, jcm -> jcm.connectionSamples.size())
.strongReference(true)
.baseUnit(BaseUnits.CONNECTIONS)
.description("The current number of open Jetty connections")
.tags(tags)
.register(registry);
}
/**
* Create a {@code JettyConnectionMetrics} instance. {@link Connector#getName()} will
* be used for {@literal connector.name} tag.
* @param registry registry to use
* @param connector connector to instrument
* @since 1.8.0
*/
public JettyConnectionMetrics(MeterRegistry registry, Connector connector) {
this(registry, connector, Tags.empty());
}
/**
* Create a {@code JettyConnectionMetrics} instance. {@link Connector#getName()} will
* be used for {@literal connector.name} tag.
* @param registry registry to use
* @param connector connector to instrument
* @param tags tags to add to metrics
* @since 1.8.0
*/
public JettyConnectionMetrics(MeterRegistry registry, Connector connector, Iterable<Tag> tags) {
this(registry, getConnectorNameTag(connector).and(tags));
}
private static Tags getConnectorNameTag(Connector connector) {
String name = connector.getName();
return Tags.of("connector.name", name != null ? name : "unnamed");
}
@Override
public void onOpened(Connection connection) {
Timer.Sample started = Timer.start(registry);
synchronized (connectionSamplesLock) {
connectionSamples.put(connection, started);
maxConnections.record(connectionSamples.size());
}
}
@Override
public void onClosed(Connection connection) {
Timer.Sample sample;
synchronized (connectionSamplesLock) {
sample = connectionSamples.remove(connection);
}
if (sample != null) {
String type = "UNKNOWN";
if (connection.getClass().getName().contains("server")) {
type = "server";
}
else if (connection.getClass().getName().contains("client")) {
type = "client";
}
sample.stop(Timer.builder("jetty.connections.request")
.description("Jetty client or server requests")
.tag("type", type)
.tags(tags)
.register(registry));
}
messagesIn.increment(connection.getMessagesIn());
messagesOut.increment(connection.getMessagesOut());
}
@Override
public void incoming(Socket socket, ByteBuffer bytes) {
bytesIn.record(bytes.limit());
}
@Override
public void outgoing(Socket socket, ByteBuffer bytes) {
bytesOut.record(bytes.limit());
}
/**
* Configures metrics instrumentation on all the {@link Server}'s {@link Connector}.
* @param server apply to this server's connectors
* @param registry register metrics to this registry
* @param tags add these tags as additional tags on metrics registered via this
*/
public static void addToAllConnectors(Server server, MeterRegistry registry, Iterable<Tag> tags) {
for (Connector connector : server.getConnectors()) {
if (connector != null) {
JettyConnectionMetrics metrics = new JettyConnectionMetrics(registry, connector, tags);
connector.addBean(metrics);
if (connector instanceof NetworkTrafficServerConnector) {
NetworkTrafficServerConnector networkTrafficServerConnector = (NetworkTrafficServerConnector) connector;
Method setNetworkTrafficListenerMethod = getNetworkTrafficListenerMethod(
networkTrafficServerConnector);
if (setNetworkTrafficListenerMethod != null) {
try {
setNetworkTrafficListenerMethod.invoke(networkTrafficServerConnector, metrics);
}
catch (IllegalAccessException | InvocationTargetException e) {
logger.debug("Unable to set network traffic listener on connector " + connector, e);
}
}
}
}
}
}
/**
* Configures metrics instrumentation on all the {@link Server}'s {@link Connector}.
* @param server apply to this server's connectors
* @param registry register metrics to this registry
*/
public static void addToAllConnectors(Server server, MeterRegistry registry) {
addToAllConnectors(server, registry, Tags.empty());
}
@Nullable
private static Method getNetworkTrafficListenerMethod(NetworkTrafficServerConnector networkTrafficServerConnector) {
Method method = null;
try {
// Jetty 9 method
method = networkTrafficServerConnector.getClass()
.getMethod("addNetworkTrafficListener", NetworkTrafficListener.class);
}
catch (NoSuchMethodException ignore) {
}
if (method != null)
return method;
try {
// Jetty 12 method
method = networkTrafficServerConnector.getClass()
.getMethod("setNetworkTrafficListener", NetworkTrafficListener.class);
}
catch (NoSuchMethodException ignore) {
}
return method;
}
}