Skip to content

Commit

Permalink
Merge pull request #218 from lyft/add-stats-handler
Browse files Browse the repository at this point in the history
add StatisticsHandler
  • Loading branch information
anuvedverma committed Jul 3, 2024
2 parents 76f038b + 2cd95fe commit 6343539
Showing 1 changed file with 20 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -26,6 +28,7 @@
import org.eclipse.jetty.server.SslConnectionFactory;
import org.eclipse.jetty.server.handler.HandlerCollection;
import org.eclipse.jetty.server.handler.RequestLogHandler;
import org.eclipse.jetty.server.handler.StatisticsHandler;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import org.eclipse.jetty.util.ssl.SslContextFactory;
Expand All @@ -35,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) {
Expand Down Expand Up @@ -107,10 +113,12 @@ private void setupContext(ProxyServerConfiguration config) {
HandlerCollection handlers = new HandlerCollection();

RequestLogHandler requestLogHandler = new RequestLogHandler();
StatisticsHandler statsHandler = new StatisticsHandler();
statsHandler.setHandler(proxyConnectHandler);

//possible not needed
//requestLogHandler.setRequestLog(customRequestLog);
handlers.setHandlers(new Handler[] { requestLogHandler, proxyConnectHandler });

handlers.setHandlers(new Handler[] { requestLogHandler, statsHandler, proxyConnectHandler });

this.server.setHandler(handlers);

Expand Down Expand Up @@ -142,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);
Expand Down

0 comments on commit 6343539

Please sign in to comment.