Table of Contents
The chapter is about Micrometer integration into Jersey which comes since the version 2.41 as an extension module. Before Jersey 2.41, it was possible to integrate Micrometer with Jersey using directlyMicrometer Jersey/Jetty support. There is also support for Jakarta EE 10 integration. The detailed documentation regarding metrics fine-tuning can be found at theMicrometer project.
Since Jersey 2.41 it's possibly to use an extension module in order to use Micrometer instrumentation inside your projects. The module shall be added as a dependency:
<dependency> <groupId>org.glassfish.jersey.ext.micrometer</groupId> <artifactId>jersey-micrometer</artifactId> <version>4.0.0</scope></dependency>
After the dependency is added, the Micrometer can be configured as follows:
final ResourceConfig resourceConfig = new ResourceConfig();resourceConfig.register(new MetricsApplicationEventListener( registry, new DefaultJerseyTagsProvider(), "http.shared.metrics", true));final ServletContainer servletContainer = new ServletContainer(resourceConfig);
the registry instance is of typeMeterRegistry which could benew SimpleMeterRegistry();. Then all metrics can be accessed likeregistry.get("http.shared.metrics"). The "http.shared.metrics" string is the name of a particular registry which was registered within theMetricsApplicationEventListener. Micrometer supports a set ofMeter primitives, includingTimer,Counter,Gauge,DistributionSummary,LongTaskTimer,FunctionCounter,FunctionTimer, andTimeGauge. Different meter types result in a different number of time series metrics. For example, while there is a single metric that represents aGauge, aTimer measures both the count of timed events and the total time of all timed events.
Implementing resource methods, which should be measured, several annotations can be used. The basic example demonstrates the@Counted annotation.
Example 16.1. Annotated Micrometer resource methods
@GET@Counted(value = COUNTER_NAME, description = COUNTER_DESCRIPTION)@Produces(MediaType.TEXT_PLAIN)@Path("counted")public String getCounterMessage() { return "Requests to this method are counted. Use /metrics to see more";}
Metrics however can be introduced using another annotations@Timed, or@TimedSet which is a set of@Timed.