-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e27948f
commit af5d9e7
Showing
1 changed file
with
54 additions
and
0 deletions.
There are no files selected for viewing
54 changes: 54 additions & 0 deletions
54
.../rest-klient/src/main/java/no/nav/vedtak/felles/integrasjon/rest/jersey/TimingFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<Long> { | ||
public void start() { | ||
this.set(System.currentTimeMillis()); | ||
} | ||
|
||
public long stop() { | ||
return System.currentTimeMillis() - get(); | ||
} | ||
|
||
@Override | ||
protected Long initialValue() { | ||
return System.currentTimeMillis(); | ||
} | ||
} | ||
} |