Monitoring HTTP Requests with MicroProfile Metrics 📎
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.