Version: 9.4.32.v20200930 |

Statistics Handler
Previous | Chapter 18. Provided Servlets, Filters, and Handlers Home | Next

##### Contact the core Jetty developers at www.webtide.com

private support for your internal/customer projects ... custom extensions and distributions ... versioned snapshots for indefinite support ... scalability guidance for your apps and Ajax/Comet projects ... development services for sponsored feature development

## Statistics Handler

### Info

- Classname: `org.eclipse.jetty.server.handler.StatisticsHandler`
- Maven Artifact: org.eclipse.jetty:jetty-server
- Javadoc: https://eclipse.org/jetty/javadoc/9.4.32.v20200930/org/eclipse/jetty/server/handler/StatisticsHandler.html

### Usage

Jetty currently has two main statistics collection mechanisms:

- Instances of `ConnectionStatistics` can collect statistics for each connection of a connector.
- The `StatisticsHandler` class may be used to collect statistics for HTTP requests.

The `StatisticsHandler` and `ConnectionStatistics` are not included in the default Jetty configuration, these need to be configured manually or enabled using the Jetty `stats` module on the command line.

    $ java -jar {$jetty.home}/start.jar --add-to-start=stats

In addition to these, the `SessionHandler` and `DefaultSessionCache` classes collect statistics for sessions. These statistics are enabled by default and are accessible via JMX interface.

> ### Note

> To view statistics, you have to be able to connect to Jetty using either JConsole or some other JMX agent. See Using JMX with Jetty for more information.

### Request Statistics

To collect request statistics a `StatisticsHandler` must be configured as one of the handlers of the server. Typically this can be done as the top level handler, but you may choose to configure a statistics handler for just one context by creating a context configuration file. You can enable the `StatisticsHandler` by activating the `stats` modules on the command line.

Alternately, if you are making multiple changes to the Jetty configuration, you could include statistics handler configuration into your own Jetty xml configuration. The following fragment shows how to configure a top level statistics handler:

    <Configure id="Server" class="org.eclipse.jetty.server.Server">
      <Call name="insertHandler">
        <Arg>
          <New id="StatsHandler" class="org.eclipse.jetty.server.handler.StatisticsHandler"/>
        </Arg>
      </Call>
    </Configure>

### Connection Statistics

Detailed statistics on connection duration and number of messages are only collated when a connection is closed. The current and maximum number of connections are the only "live" statistics.

The following example shows how to turn on connection statistics in the Jetty XML format.

    <Configure id="Server" class="org.eclipse.jetty.server.Server">
      <Call name="addBeanToAllConnectors">
        <Arg>
          <New class="org.eclipse.jetty.io.ConnectionStatistics"/>
        </Arg>
      </Call>
    </Configure>

A special variant of `ConnectionStatistics` called `IncludeExcludeConnectionStatistics` allows you to refine which types of connection you want to collect statistics for.

The following example shows how this can be used to record statistics only for WebSocket connections.

    <Configure id="Server" class="org.eclipse.jetty.server.Server">
      <Call name="addBeanToAllConnectors">
        <Arg>
          <New class="org.eclipse.jetty.io.IncludeExcludeConnectionStatistics">
            <Call name="include" arg="org.eclipse.jetty.websocket.common.io.AbstractWebSocketConnection"/>
          </New>
        </Arg>
      </Call>
    </Configure>

### Session Statistics

Session handling is built into Jetty for any servlet or webapp context. Detailed statistics on session duration are only collated when a session is closed. The current, minimum, and maximum number of sessions are the only "live" statistics. The session statistics are enabled by default and do not need to be configured.

Previous | Top | Next
Debug Handler | Home | IP Access Handler

See an error or something missing? Contribute to this documentation at Github!(Generated: 2020-09-30)