Monitoring HTTP Requests with MicroProfile Metrics

A Java EE 8 HttpFilter, configured to map all URLs:


import javax.inject.Inject;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.eclipse.microprofile.metrics.Meter;
import org.eclipse.microprofile.metrics.MetricRegistry;
import org.eclipse.microprofile.metrics.annotation.RegistryType;

@WebFilter(urlPatterns = "/*")
public class FilterEverything extends HttpFilter {

    @Inject
    @RegistryType(type = MetricRegistry.Type.APPLICATION)
    MetricRegistry registry;

    @Override
    protected void doFilter(HttpServletRequest req, HttpServletResponse res, FilterChain chain) throws IOException, ServletException {
        registry.counter(req.getPathInfo()).inc();
        Meter meter = registry.meter("meter_" + req.getPathInfo());
        chain.doFilter(req, res);
        meter.mark();
    }

}

...will generate for the invocation of the PingResource:


@Path("ping")
public class PingResource {

    @GET
    @Path("slow")
    public String slow() {
        //heavy lifting
        return "42";
    }

    @GET
    @Path("fast")
    public String fast() {
        return "21";
    }

}

...the following prometheus metrics (curl http://localhost:8080/metrics/application):


# TYPE application:/ping/slow counter
application:/ping/slow 1
# TYPE application:/ping/fast counter
application:/ping/fast 1
# TYPE application:meter_/ping/fast_total counter
application:meter_/ping/fast_total 1
# TYPE application:meter_/ping/fast_rate_per_second gauge
application:meter_/ping/fast_rate_per_second 0.044544529211015586
# TYPE application:meter_/ping/fast_one_min_rate_per_second gauge
application:meter_/ping/fast_one_min_rate_per_second 0.155760156614281
# TYPE application:meter_/ping/fast_five_min_rate_per_second gauge
application:meter_/ping/fast_five_min_rate_per_second 0.1902458849001428
# TYPE application:meter_/ping/fast_fifteen_min_rate_per_second gauge
application:meter_/ping/fast_fifteen_min_rate_per_second 0.1966942907643235
# TYPE application:meter_/ping/slow_total counter
application:meter_/ping/slow_total 1
# TYPE application:meter_/ping/slow_rate_per_second gauge
application:meter_/ping/slow_rate_per_second 0.05214608959864638
# TYPE application:meter_/ping/slow_one_min_rate_per_second gauge
application:meter_/ping/slow_one_min_rate_per_second 0.16929634497812282
# TYPE application:meter_/ping/slow_five_min_rate_per_second gauge
application:meter_/ping/slow_five_min_rate_per_second 0.1934432200964012
# TYPE application:meter_/ping/slow_fifteen_min_rate_per_second gauge
application:meter_/ping/slow_fifteen_min_rate_per_second 0.19779007785878447

...also available as JSON (curl -H"Accept: application/json" http://localhost:8080/metrics/application):

{
    "/ping/slow": 1,
    "/ping/fast": 1,
    "meter_/ping/fast": {
        "count": 1,
        "fiveMinRate": 0.14571471450227858,
        "oneMinRate": 0.04105793151598188,
        "fifteenMinRate": 0.17996489624183667,
        "meanRate": 0.009531711523994577
    },
    "meter_/ping/slow": {
        "count": 1,
        "fiveMinRate": 0.14571471450227858,
        "oneMinRate": 0.04105793151598188,
        "fifteenMinRate": 0.17996489624183667,
        "meanRate": 0.009838528938613995
    }
}

Project was created with javaee8-essentials-archetype, the 5kB ThinWAR was built and deployed with wad.sh in 2963ms and tested with Payara 5.

See you at Web, MicroProfile and Java EE Workshops at Munich Airport, Terminal 2 or Virtual Dedicated Workshops / consulting. Is Munich's airport too far? Learn from home: airhacks.io.


Web Apps, SPA, PWA with vanilla Java Script (ES 6+), CSS 3 and WebStandards only. As simple as possible, but not simpler. See you at: (Progressive) Web apps, Single Page Apps and WebStandards airhacks workshops at MUC airport, Winter Edition

airhacks.fm the podcast:

Stay in touch: airhacks.news.

Comments:

Hi adam, amazing post. i have a question nonetheless. say i was using bce pattern, what package would house the Filter class? control or boundary?

Posted by Olatunji Oniyide on March 27, 2019 at 08:33 PM CET #

Post a Comment:
  • HTML Syntax: NOT allowed
Online Workshops
realworldpatterns.com
...the last 150 posts
...the last 10 comments
License