From eb9cf6f25652ae1262e7bdb05a8a19d20d32d941 Mon Sep 17 00:00:00 2001 From: Simon Detheridge Date: Mon, 21 Jun 2021 17:11:49 +0100 Subject: [PATCH] Export prometheus metrics --- services/git-bridge/pom.xml | 18 ++++++ .../wlgitbridge/server/GitBridgeServer.java | 1 + .../wlgitbridge/server/PrometheusHandler.java | 55 +++++++++++++++++++ 3 files changed, 74 insertions(+) create mode 100644 services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/server/PrometheusHandler.java diff --git a/services/git-bridge/pom.xml b/services/git-bridge/pom.xml index e02cc6dce7..2a11cff9ce 100644 --- a/services/git-bridge/pom.xml +++ b/services/git-bridge/pom.xml @@ -194,5 +194,23 @@ commons-compress 1.15 + + + io.prometheus + simpleclient + 0.10.0 + + + + io.prometheus + simpleclient_hotspot + 0.10.0 + + + + io.prometheus + simpleclient_servlet + 0.10.0 + diff --git a/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/server/GitBridgeServer.java b/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/server/GitBridgeServer.java index e97bc57fdf..0411c67250 100644 --- a/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/server/GitBridgeServer.java +++ b/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/server/GitBridgeServer.java @@ -127,6 +127,7 @@ public class GitBridgeServer { handlers.addHandler(new StatusHandler(bridge)); handlers.addHandler(new HealthCheckHandler(bridge)); handlers.addHandler(new GitLfsHandler(bridge)); + handlers.addHandler(new PrometheusHandler(bridge)); base.setHandler(handlers); return base; } diff --git a/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/server/PrometheusHandler.java b/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/server/PrometheusHandler.java new file mode 100644 index 0000000000..afe18924fe --- /dev/null +++ b/services/git-bridge/src/main/java/uk/ac/ic/wlgitbridge/server/PrometheusHandler.java @@ -0,0 +1,55 @@ +package uk.ac.ic.wlgitbridge.server; + +import io.prometheus.client.exporter.MetricsServlet; +import io.prometheus.client.hotspot.DefaultExports; + +import org.eclipse.jetty.server.HttpConnection; +import org.eclipse.jetty.server.Request; +import org.eclipse.jetty.server.handler.AbstractHandler; +import org.eclipse.jetty.servlet.ServletHolder; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import uk.ac.ic.wlgitbridge.bridge.Bridge; +import uk.ac.ic.wlgitbridge.util.Log; + +import javax.servlet.ServletException; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; +import java.util.Arrays; + +public class PrometheusHandler extends AbstractHandler { + + private final Bridge bridge; + private final MetricsServlet metricsServlet; + private final ServletHolder holder; + + public PrometheusHandler(Bridge bridge) { + this.bridge = bridge; + this.metricsServlet = new MetricsServlet(); + this.holder = new ServletHolder(metricsServlet); + DefaultExports.initialize(); + } + + @Override + public void handle( + String target, + Request baseRequest, + HttpServletRequest request, + HttpServletResponse response + ) throws IOException, ServletException { + String method = baseRequest.getMethod(); + if ( + ("GET".equals(method)) + && target != null + && target.matches("^/metrics/?$") + ) { + Log.info(method + " <- /metrics"); + response.setContentType("application/vnd.git-lfs+json"); + response.setStatus(200); + this.holder.handle(baseRequest, request, response); + baseRequest.setHandled(true); + } + } + +}