|
| 1 | +package org.juv25d.plugin; |
| 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.io.InputStream; |
| 9 | +import java.nio.charset.StandardCharsets; |
| 10 | +import java.time.ZoneId; |
| 11 | +import java.time.ZonedDateTime; |
| 12 | +import java.time.format.DateTimeFormatter; |
| 13 | +import java.util.Properties; |
| 14 | +import java.util.logging.Logger; |
| 15 | + |
| 16 | +public class MetricPlugin implements Plugin { |
| 17 | + |
| 18 | + |
| 19 | + private static final String SERVER_NAME = "juv25d-webserver"; |
| 20 | + private static final DateTimeFormatter TIME_FORMAT = |
| 21 | + DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss z"); |
| 22 | + |
| 23 | + private final String version; |
| 24 | + private final String commit; |
| 25 | + |
| 26 | + public MetricPlugin() { |
| 27 | + Logger logger = ServerLogging.getLogger(); |
| 28 | + Properties props = new Properties(); |
| 29 | + try (InputStream is = getClass().getClassLoader().getResourceAsStream("build.properties")) { |
| 30 | + if (is != null) { |
| 31 | + props.load(is); |
| 32 | + } |
| 33 | + } catch (IOException e) { |
| 34 | + logger.warning("Error loading build.properties: " + e.getMessage()); |
| 35 | + } |
| 36 | + this.version = escapeJson(props.getProperty("build.version", "dev")); |
| 37 | + String commitValue = props.getProperty("build.commit"); |
| 38 | + if (commitValue == null || commitValue.isBlank()) { |
| 39 | + this.commit = "unknown"; |
| 40 | + } else { |
| 41 | + this.commit = escapeJson(commitValue); |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + private String escapeJson(String value) { |
| 46 | + if (value == null) { |
| 47 | + return ""; |
| 48 | + } |
| 49 | + return value.replace("\\", "\\\\").replace("\"", "\\\""); |
| 50 | + } |
| 51 | + |
| 52 | + @Override |
| 53 | + public void handle(HttpRequest req, HttpResponse res) throws IOException { |
| 54 | + |
| 55 | + Runtime runtime = Runtime.getRuntime(); |
| 56 | + long usedMemory = runtime.totalMemory() - runtime.freeMemory(); |
| 57 | + long maxMemory = runtime.maxMemory(); |
| 58 | + long responseTimeUs = |
| 59 | + (System.nanoTime() - req.creationTimeNanos()) / 1_000; |
| 60 | + String localTime = ZonedDateTime |
| 61 | + .now(ZoneId.systemDefault()) |
| 62 | + .format(TIME_FORMAT); |
| 63 | + String utcTime = ZonedDateTime |
| 64 | + .now(ZoneId.of("UTC")) |
| 65 | + .format(TIME_FORMAT); |
| 66 | + |
| 67 | + String jsonBody = String.format(""" |
| 68 | + { |
| 69 | + "localTime": "%s", |
| 70 | + "utcTime": "%s", |
| 71 | + "server": "%s", |
| 72 | + "buildVersion": "%s", |
| 73 | + "gitCommit": "%s", |
| 74 | + "responseTimeUs": %d, |
| 75 | + "memory": { |
| 76 | + "usedBytes": %d, |
| 77 | + "maxBytes": %d |
| 78 | + } |
| 79 | + } |
| 80 | + """, |
| 81 | + localTime, |
| 82 | + utcTime, |
| 83 | + SERVER_NAME, |
| 84 | + version, |
| 85 | + commit, |
| 86 | + responseTimeUs, |
| 87 | + usedMemory, |
| 88 | + maxMemory |
| 89 | + ); |
| 90 | + |
| 91 | + byte[] bodyBytes = jsonBody.getBytes(StandardCharsets.UTF_8); |
| 92 | + |
| 93 | + res.setStatusCode(200); |
| 94 | + res.setHeader("Content-Type", "application/json"); |
| 95 | + res.setHeader("Content-Length", String.valueOf(bodyBytes.length)); |
| 96 | + res.setBody(bodyBytes); |
| 97 | + } |
| 98 | +} |
0 commit comments