Chapter 16. Micrometer - application observability facade
Prev |  | Next

Links: Table of Contents | Single HTML

# Chapter 16. Micrometer - application observability facade

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 directly Micrometer Jersey/Jetty support. There is also support for Jakarta EE 10 integration. The detailed documentation regarding metrics fine-tuning can be found at the Micrometer project.

## 16.1. Integration into Jersey

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>3.1.9</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 type `MeterRegistry` which could be `new SimpleMeterRegistry();`. Then all metrics can be accessed like `registry.get("http.shared.metrics")`. The "http.shared.metrics" string is the name of a particular registry which was registered within the `MetricsApplicationEventListener`. Micrometer supports a set of `Meter` primitives, including `Timer`, `Counter`, `Gauge`, `DistributionSummary`, `LongTaskTimer`, `FunctionCounter`, `FunctionTimer`, and `TimeGauge`. Different meter types result in a different number of time series metrics. For example, while there is a single metric that represents a `Gauge`, a `Timer` 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`.

Prev |  | Next
Chapter 15. Jersey configuration | Home | Chapter 17. Server-Sent Events (SSE) Support