From a721d79b9889ec08f51cbad8497ea2ba8ce0f1d5 Mon Sep 17 00:00:00 2001 From: Alejandro Revilla Date: Sun, 12 Jan 2025 18:28:34 -0300 Subject: [PATCH] add /jpos/q2/status endpoint when metrics are enabled A call to /jpos/q2/status will return either 'running' or 'stopping'. --- .../org/jpos/metrics/PrometheusService.java | 18 ++++++++++++++++-- jpos/src/main/java/org/jpos/q2/Q2.java | 3 ++- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/jpos/src/main/java/org/jpos/metrics/PrometheusService.java b/jpos/src/main/java/org/jpos/metrics/PrometheusService.java index 9770fead2a..c88259f923 100644 --- a/jpos/src/main/java/org/jpos/metrics/PrometheusService.java +++ b/jpos/src/main/java/org/jpos/metrics/PrometheusService.java @@ -33,6 +33,8 @@ public class PrometheusService extends QBeanSupport { private int port; @Config("path") private String path; + @Config("status-path") + private String statusPath; private HttpServer server; @Override @@ -48,6 +50,16 @@ protected void startService() throws ConfigurationException { os.write(response.getBytes()); } }); + if (statusPath != null) { + server.createContext(statusPath, httpExchange -> { + String response = getServer().running() ? "running\n" : "stopping\n"; + httpExchange.getResponseHeaders().add("Content-Type", "text/plain"); + httpExchange.sendResponseHeaders(200, response.getBytes().length); + try (OutputStream os = httpExchange.getResponseBody()) { + os.write(response.getBytes()); + } + }); + } Thread.ofVirtual().start(server::start); } catch (IOException e) { getLog().warn(e); @@ -60,10 +72,12 @@ protected void stopService() { server.stop(2); } - public static Element createDescriptor (int port, String path) { + public static Element createDescriptor (int port, String path, String statusPath) { return new Element("prometheus") .addContent(createProperty ("port", Integer.toString(port))) - .addContent (createProperty ("path", path)); + .addContent (createProperty ("path", path)) + .addContent (createProperty ("status-path", statusPath)); + } private static Element createProperty (String name, String value) { return new Element ("property") diff --git a/jpos/src/main/java/org/jpos/q2/Q2.java b/jpos/src/main/java/org/jpos/q2/Q2.java index 30d7896287..6318160ab9 100644 --- a/jpos/src/main/java/org/jpos/q2/Q2.java +++ b/jpos/src/main/java/org/jpos/q2/Q2.java @@ -158,6 +158,7 @@ public class Q2 implements FileFilter, Runnable { private PrometheusMeterRegistry prometheusRegistry = new PrometheusMeterRegistry(PrometheusConfig.DEFAULT); private int metricsPort; private String metricsPath; + private final String statusPath = "/jpos/q2/status"; private Counter instancesCounter = Metrics.counter("jpos.q2.instances"); private boolean noShutdownHook; @@ -244,7 +245,7 @@ public void run () { } if (metricsPort != 0) { deployElement( - PrometheusService.createDescriptor(metricsPort, metricsPath), + PrometheusService.createDescriptor(metricsPort, metricsPath, statusPath), "00_prometheus-" + getInstanceId() + ".xml", false, true); }