From af5d9e75e614c576f3597633a2826b33da0904aa Mon Sep 17 00:00:00 2001 From: Jan-Olav Eide Date: Thu, 10 Jun 2021 13:59:29 +0200 Subject: [PATCH] lagt til timing-filter --- .../integrasjon/rest/jersey/TimingFilter.java | 54 +++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 integrasjon/rest-klient/src/main/java/no/nav/vedtak/felles/integrasjon/rest/jersey/TimingFilter.java diff --git a/integrasjon/rest-klient/src/main/java/no/nav/vedtak/felles/integrasjon/rest/jersey/TimingFilter.java b/integrasjon/rest-klient/src/main/java/no/nav/vedtak/felles/integrasjon/rest/jersey/TimingFilter.java new file mode 100644 index 000000000..ef05ac83e --- /dev/null +++ b/integrasjon/rest-klient/src/main/java/no/nav/vedtak/felles/integrasjon/rest/jersey/TimingFilter.java @@ -0,0 +1,54 @@ +package no.nav.vedtak.felles.integrasjon.rest.jersey; + +import static io.micrometer.core.instrument.Metrics.timer; +import static no.nav.vedtak.log.metrics.MetricsUtil.utvidMedHistogram; + +import java.io.IOException; +import java.time.Duration; + +import javax.annotation.Priority; +import javax.ws.rs.Priorities; +import javax.ws.rs.container.ContainerRequestContext; +import javax.ws.rs.container.ContainerRequestFilter; +import javax.ws.rs.container.ContainerResponseContext; +import javax.ws.rs.container.ContainerResponseFilter; +import javax.ws.rs.ext.Provider; + +@Provider +@Priority(Priorities.USER) +public class TimingFilter implements ContainerRequestFilter, ContainerResponseFilter { + + private static final String STATUS = "status"; + private static final String PATH = "path"; + private static final String METRIC_NAME = "rest"; + private static final ThreadLocalTimer TIMER = new ThreadLocalTimer(); + + public TimingFilter() { + utvidMedHistogram(METRIC_NAME); + } + + @Override + public void filter(ContainerRequestContext req, ContainerResponseContext res) throws IOException { + timer(METRIC_NAME, PATH, req.getUriInfo().getPath(), STATUS, String.valueOf(res.getStatus())).record(Duration.ofMillis(TIMER.stop())); + } + + @Override + public void filter(ContainerRequestContext req) throws IOException { + TIMER.start(); + } + + private static class ThreadLocalTimer extends ThreadLocal { + public void start() { + this.set(System.currentTimeMillis()); + } + + public long stop() { + return System.currentTimeMillis() - get(); + } + + @Override + protected Long initialValue() { + return System.currentTimeMillis(); + } + } +}