|
15 | 15 | */
|
16 | 16 | package io.micrometer.core.instrument.binder.jetty;
|
17 | 17 |
|
| 18 | +import io.micrometer.common.lang.Nullable; |
| 19 | +import io.micrometer.common.util.internal.logging.InternalLogger; |
| 20 | +import io.micrometer.common.util.internal.logging.InternalLoggerFactory; |
18 | 21 | import io.micrometer.core.instrument.*;
|
19 | 22 | import io.micrometer.core.instrument.binder.BaseUnits;
|
20 | 23 | import io.micrometer.core.instrument.distribution.DistributionStatisticConfig;
|
21 | 24 | import io.micrometer.core.instrument.distribution.TimeWindowMax;
|
22 | 25 |
|
| 26 | +import java.lang.reflect.InvocationTargetException; |
| 27 | +import java.lang.reflect.Method; |
23 | 28 | import java.net.Socket;
|
24 | 29 | import java.nio.ByteBuffer;
|
25 | 30 |
|
26 | 31 | import org.eclipse.jetty.io.Connection;
|
27 | 32 | import org.eclipse.jetty.io.NetworkTrafficListener;
|
28 | 33 | import org.eclipse.jetty.server.Connector;
|
| 34 | +import org.eclipse.jetty.server.NetworkTrafficServerConnector; |
29 | 35 | import org.eclipse.jetty.server.Server;
|
30 | 36 | import org.eclipse.jetty.util.component.AbstractLifeCycle;
|
31 | 37 |
|
|
46 | 52 | * server.setConnectors(new Connector[] { connector });
|
47 | 53 | * }</pre>
|
48 | 54 | *
|
49 |
| - * Alternatively, configure on all connectors with |
| 55 | + * Alternatively, configure on all server connectors with |
50 | 56 | * {@link JettyConnectionMetrics#addToAllConnectors(Server, MeterRegistry, Iterable)}.
|
51 | 57 | *
|
52 | 58 | * @author Jon Schneider
|
53 | 59 | * @since 1.4.0
|
54 | 60 | */
|
55 | 61 | public class JettyConnectionMetrics extends AbstractLifeCycle implements Connection.Listener, NetworkTrafficListener {
|
56 | 62 |
|
| 63 | + private static final InternalLogger logger = InternalLoggerFactory.getInstance(JettyConnectionMetrics.class); |
| 64 | + |
57 | 65 | private final MeterRegistry registry;
|
58 | 66 |
|
59 | 67 | private final Iterable<Tag> tags;
|
@@ -193,16 +201,63 @@ public void outgoing(Socket socket, ByteBuffer bytes) {
|
193 | 201 | bytesOut.record(bytes.limit());
|
194 | 202 | }
|
195 | 203 |
|
| 204 | + /** |
| 205 | + * Configures metrics instrumentation on all the {@link Server}'s {@link Connector}. |
| 206 | + * @param server apply to this server's connectors |
| 207 | + * @param registry register metrics to this registry |
| 208 | + * @param tags add these tags as additional tags on metrics registered via this |
| 209 | + */ |
196 | 210 | public static void addToAllConnectors(Server server, MeterRegistry registry, Iterable<Tag> tags) {
|
197 | 211 | for (Connector connector : server.getConnectors()) {
|
198 | 212 | if (connector != null) {
|
199 |
| - connector.addBean(new JettyConnectionMetrics(registry, connector, tags)); |
| 213 | + JettyConnectionMetrics metrics = new JettyConnectionMetrics(registry, connector, tags); |
| 214 | + connector.addBean(metrics); |
| 215 | + if (connector instanceof NetworkTrafficServerConnector) { |
| 216 | + NetworkTrafficServerConnector networkTrafficServerConnector = (NetworkTrafficServerConnector) connector; |
| 217 | + Method setNetworkTrafficListenerMethod = getNetworkTrafficListenerMethod( |
| 218 | + networkTrafficServerConnector); |
| 219 | + if (setNetworkTrafficListenerMethod != null) { |
| 220 | + try { |
| 221 | + setNetworkTrafficListenerMethod.invoke(networkTrafficServerConnector, metrics); |
| 222 | + } |
| 223 | + catch (IllegalAccessException | InvocationTargetException e) { |
| 224 | + logger.debug("Unable to set network traffic listener on connector " + connector, e); |
| 225 | + } |
| 226 | + } |
| 227 | + } |
200 | 228 | }
|
201 | 229 | }
|
202 | 230 | }
|
203 | 231 |
|
| 232 | + /** |
| 233 | + * Configures metrics instrumentation on all the {@link Server}'s {@link Connector}. |
| 234 | + * @param server apply to this server's connectors |
| 235 | + * @param registry register metrics to this registry |
| 236 | + */ |
204 | 237 | public static void addToAllConnectors(Server server, MeterRegistry registry) {
|
205 | 238 | addToAllConnectors(server, registry, Tags.empty());
|
206 | 239 | }
|
207 | 240 |
|
| 241 | + @Nullable |
| 242 | + private static Method getNetworkTrafficListenerMethod(NetworkTrafficServerConnector networkTrafficServerConnector) { |
| 243 | + Method method = null; |
| 244 | + try { |
| 245 | + // Jetty 9 method |
| 246 | + method = networkTrafficServerConnector.getClass() |
| 247 | + .getMethod("addNetworkTrafficListener", NetworkTrafficListener.class); |
| 248 | + } |
| 249 | + catch (NoSuchMethodException ignore) { |
| 250 | + } |
| 251 | + if (method != null) |
| 252 | + return method; |
| 253 | + try { |
| 254 | + // Jetty 12 method |
| 255 | + method = networkTrafficServerConnector.getClass() |
| 256 | + .getMethod("setNetworkTrafficListener", NetworkTrafficListener.class); |
| 257 | + } |
| 258 | + catch (NoSuchMethodException ignore) { |
| 259 | + } |
| 260 | + return method; |
| 261 | + } |
| 262 | + |
208 | 263 | }
|
0 commit comments