Eclipse Jetty and Jersey Instrumentation
Jetty
Micrometer supports binding metrics to Jetty through Connection.Listener
.
You can collect metrics from a Jetty Connector
by configuring it with JettyConnectionMetrics
, as follows:
Server server = new Server(0);
NetworkTrafficServerConnector connector = new NetworkTrafficServerConnector(server);
JettyConnectionMetrics metrics = new JettyConnectionMetrics(registry, connector);
connector.addBean(metrics); (1)
connector.setNetworkTrafficListener(metrics); (2)
server.setConnectors(new Connector[] { connector });
1 | Register general connection metrics |
2 | Registers metrics for bytes in/out on this connector |
Alternatively, you can apply the metrics instrumentation to all connectors on a Server
as follows:
JettyConnectionMetrics.addToAllConnectors(server, registry);
Connection metrics can be configured on a client as well, but bytes in/out will not be available when instrumenting a client.
HttpClient httpClient = new HttpClient();
httpClient.addBean(new JettyConnectionMetrics(registry));
Jersey
Micrometer also supports binding metrics to Jersey through ApplicationEventListener
.
You can collect metrics from Jersey by adding MetricsApplicationEventListener
, as follows:
ResourceConfig resourceConfig = new ResourceConfig();
resourceConfig.register(new MetricsApplicationEventListener(
registry,
new DefaultJerseyTagsProvider(),
"http.server.requests",
true));
ServletContainer servletContainer = new ServletContainer(resourceConfig);
Eclipse Observation Jersey Instrumentation
Below you can find an example of how to instrument Jersey with Micrometer Observation. That means that depending on your Observation Handler configuration you instrument once, and can have multiple benefits out of it (e.g. metrics, distributed tracing).
// Setting up instrumentation
new ObservationApplicationEventListener(getObservationRegistry(), timerName())
ResourceConfig config = new ResourceConfig();
config.register(listener);