Skip to content

Commit 9e19b0c

Browse files
committed
Added TimingFilter to measure request duration and log ms
1 parent af32a5d commit 9e19b0c

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

src/main/java/org/juv25d/App.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import org.juv25d.filter.IpFilter;
44
import org.juv25d.filter.LoggingFilter;
5+
import org.juv25d.filter.TimingFilter;
56
import org.juv25d.logging.ServerLogging;
67
import org.juv25d.http.HttpParser;
78
import org.juv25d.plugin.NotFoundPlugin; // New import
@@ -27,6 +28,7 @@ public static void main(String[] args) {
2728
Set.of()
2829
), 0);
2930
pipeline.addGlobalFilter(new LoggingFilter(), 0);
31+
pipeline.addGlobalFilter(new TimingFilter(), 0);
3032

3133
// Initialize and configure SimpleRouter
3234
SimpleRouter router = new SimpleRouter();
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package org.juv25d.filter;
2+
3+
import org.juv25d.http.HttpRequest;
4+
import org.juv25d.http.HttpResponse;
5+
import org.juv25d.logging.ServerLogging;
6+
7+
import java.io.IOException;
8+
import java.util.logging.Logger;
9+
10+
public class TimingFilter implements Filter {
11+
12+
private static final Logger logger = ServerLogging.getLogger();
13+
14+
@Override
15+
public void doFilter(HttpRequest req, HttpResponse res, FilterChain chain) throws IOException {
16+
long start = System.nanoTime();
17+
18+
chain.doFilter(req, res);
19+
20+
long durationMs = (System.nanoTime() - start) / 1_000_000;
21+
logger.info(req.method() + " " + req.path() + " took " + durationMs + " ms");
22+
}
23+
}

0 commit comments

Comments
 (0)