Skip to content

Commit

Permalink
add StatsHandler jetty logs
Browse files Browse the repository at this point in the history
  • Loading branch information
anuvedverma committed Jul 3, 2024
1 parent 7d60671 commit 2cd95fe
Showing 1 changed file with 17 additions and 1 deletion.
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 Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 2cd95fe

Please sign in to comment.