From 2cd95fe8fb6933e0748d13837157d308e3ac4ba3 Mon Sep 17 00:00:00 2001 From: Anuved Verma Date: Wed, 3 Jul 2024 16:05:40 -0700 Subject: [PATCH] add StatsHandler jetty logs --- .../com/lyft/data/proxyserver/ProxyServer.java | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/proxyserver/src/main/java/com/lyft/data/proxyserver/ProxyServer.java b/proxyserver/src/main/java/com/lyft/data/proxyserver/ProxyServer.java index 6bdc4b31..24e387c5 100644 --- a/proxyserver/src/main/java/com/lyft/data/proxyserver/ProxyServer.java +++ b/proxyserver/src/main/java/com/lyft/data/proxyserver/ProxyServer.java @@ -3,6 +3,8 @@ import java.io.Closeable; import java.io.File; import java.util.EnumSet; +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; import javax.servlet.DispatcherType; @@ -36,6 +38,9 @@ public class ProxyServer implements Closeable { private final Server server; private final ProxyServletImpl proxy; private final ProxyHandler proxyHandler; + private final ScheduledExecutorService scheduler = + Executors.newScheduledThreadPool(1); + private ServletContextHandler context; public ProxyServer(ProxyServerConfiguration config, ProxyHandler proxyHandler) { @@ -109,11 +114,12 @@ private void setupContext(ProxyServerConfiguration config) { RequestLogHandler requestLogHandler = new RequestLogHandler(); StatisticsHandler statsHandler = new StatisticsHandler(); + statsHandler.setHandler(proxyConnectHandler); + //possible not needed //requestLogHandler.setRequestLog(customRequestLog); handlers.setHandlers(new Handler[] { requestLogHandler, statsHandler, proxyConnectHandler }); - this.server.setHandler(handlers); if (proxyHandler != null) { @@ -144,6 +150,16 @@ public void start() { try { this.server.start(); + + // Schedule a task to log metrics at a fixed rate + StatisticsHandler stats = this.server.getChildHandlerByClass(StatisticsHandler.class); + this.scheduler.scheduleAtFixedRate(() -> { + log.debug("(jetty) Num requests: " + stats.getRequests()); + log.debug("(jetty) Num requests: " + stats.getRequestsActive()); + log.debug("(jetty) Responses with 4xx status: " + stats.getResponses4xx()); + log.debug("(jetty) Responses with 5xx status: " + stats.getResponses5xx()); + // Log other metrics as needed + }, 0, 5, TimeUnit.MINUTES); } catch (Exception e) { log.error("Error starting proxy server", e); throw new IllegalStateException(e);