Skip to content

Commit 15e3cde

Browse files
committed
feat(HttpServer): add request tracking with UUID and timestamps
Add RequestInfo class and activeRequests map to track ongoing HTTP requests Include request method, path, IP and timestamp for monitoring purposes
1 parent ae4abb1 commit 15e3cde

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

src/main/java/mindustrytool/handler/HttpServer.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
import java.util.HashMap;
77
import java.util.List;
88
import java.util.Map;
9+
import java.util.UUID;
10+
import java.util.concurrent.ConcurrentHashMap;
911
import java.util.function.Predicate;
1012
import java.util.stream.Collectors;
1113

@@ -59,6 +61,22 @@ public class HttpServer {
5961

6062
private final WeakReference<ServerController> context;
6163

64+
public class RequestInfo {
65+
public final String method;
66+
public final String path;
67+
public final String ip;
68+
public final long timestamp;
69+
70+
public RequestInfo(String method, String path, String ip, long timestamp) {
71+
this.method = method;
72+
this.path = path;
73+
this.ip = ip;
74+
this.timestamp = timestamp;
75+
}
76+
}
77+
78+
private final Map<String, RequestInfo> activeRequests = new ConcurrentHashMap<>();
79+
6280
public HttpServer(WeakReference<ServerController> context) {
6381
this.context = context;
6482

@@ -91,6 +109,21 @@ public HttpServer(WeakReference<ServerController> context) {
91109
});
92110
});
93111

112+
app.before(ctx -> {
113+
String reqId = UUID.randomUUID().toString();
114+
ctx.attribute("reqId", reqId);
115+
activeRequests.put(reqId, new RequestInfo(
116+
ctx.method().name(), ctx.path(), ctx.ip(), System.currentTimeMillis()));
117+
});
118+
119+
// Remove when request finishes
120+
app.after(ctx -> {
121+
String reqId = ctx.attribute("reqId");
122+
if (reqId != null) {
123+
activeRequests.remove(reqId);
124+
}
125+
});
126+
94127
Log.info("Http server created: " + this);
95128
}
96129

@@ -431,6 +464,8 @@ public void init() {
431464
.toList()))
432465
.toList());
433466

467+
data.put("activeRequest", activeRequests.values());
468+
434469
var maps = new ArrayList<HashMap<String, String>>();
435470
Vars.maps.all().forEach(map -> {
436471
var tags = new HashMap<String, String>();

0 commit comments

Comments
 (0)