0

I am following the Prometheus Java client library to expose metrics of my Jetty server. From here . But when I start the server I get the error -

Exception in thread "main" java.lang.RuntimeException: Unable to start jetty server
Caused by: java.lang.IllegalStateException: StatisticsHandler has no Wrapped Handler

To fix this I had to do -

    StatisticsHandler stats = new StatisticsHandler();
    stats.setServer(server.getServer());
    HandlerWrapper hw = new HandlerWrapper();
    stats.setHandler(hw);
    new JettyStatisticsCollector(stats).register();

    contextCollectionHandler.addHandler(stats);
    server.setHandler(contextCollectionHandler);

    try {
        server.start();
    }
    catch (final Exception e) {
        throw new RuntimeException("Unable to start jetty server", e);
    }

But, I dont see the stats incrementing, when I make requests to my server through Postman. I get responses, 200, 404 etc. But none of the stats such as jetty_requests_total or jetty_responses_total are incrementing. They stay at 0. The only thing that changes is jetty_stats_seconds. What am I doing wrong ?

Harrish A
  • 53
  • 8

1 Answers1

0

ContextHandlerCollection can only hold/use/process ContextHandler based handlers.

Your StatisticsHandler and Handler.Wrapper are not ContextHandler implementations, they will never be called.

The Handler tree is just that, a tree. Think of it like that and it will start to make sense.

See past answer

If you look at StatisticsHandler you'll find that it's a HandlerWrapper implementation.

See: Javadoc for org.eclipse.jetty.server.handler.StatisticsHandler in Jetty 11.0.11

In short, you want that StatisticsHandler to be somewhere early in your Handler tree, and have it wrap whatever handlers you want it to operate on.

Something like this ...

Server.setHandler
  HandlerList
    StatisticsHandler
      GzipHandler
        ContextHandlerCollection
          ContextHandler("/")
          ServletContextHandler("/rest")
          WebAppContext("/app")
    DefaultHandler
Joakim Erdfelt
  • 46,896
  • 7
  • 86
  • 136