Skip to content

Commit

Permalink
add /jpos/q2/status endpoint when metrics are enabled
Browse files Browse the repository at this point in the history
A call to /jpos/q2/status will return either 'running' or 'stopping'.
  • Loading branch information
ar committed Jan 12, 2025
1 parent 68eff1d commit a721d79
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
18 changes: 16 additions & 2 deletions jpos/src/main/java/org/jpos/metrics/PrometheusService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
Expand All @@ -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")
Expand Down
3 changes: 2 additions & 1 deletion jpos/src/main/java/org/jpos/q2/Q2.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}

Expand Down

0 comments on commit a721d79

Please sign in to comment.