All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.dropwizard.metrics5.jetty10.InstrumentedConnectionFactory Maven / Gradle / Ivy

Go to download

A set of extensions for Jetty 10.x and higher which provide instrumentation of thread pools, connector metrics, and application latency and utilization.

There is a newer version: 5.0.0-rc22
Show newest version
package io.dropwizard.metrics5.jetty10;

import io.dropwizard.metrics5.Counter;
import io.dropwizard.metrics5.Timer;
import org.eclipse.jetty.io.Connection;
import org.eclipse.jetty.io.EndPoint;
import org.eclipse.jetty.server.ConnectionFactory;
import org.eclipse.jetty.server.Connector;
import org.eclipse.jetty.util.component.ContainerLifeCycle;

import java.util.List;

public class InstrumentedConnectionFactory extends ContainerLifeCycle implements ConnectionFactory {
    private final ConnectionFactory connectionFactory;
    private final Timer timer;
    private final Counter counter;

    public InstrumentedConnectionFactory(ConnectionFactory connectionFactory, Timer timer) {
        this(connectionFactory, timer, null);
    }

    public InstrumentedConnectionFactory(ConnectionFactory connectionFactory, Timer timer, Counter counter) {
        this.connectionFactory = connectionFactory;
        this.timer = timer;
        this.counter = counter;
        addBean(connectionFactory);
    }

    @Override
    public String getProtocol() {
        return connectionFactory.getProtocol();
    }

    @Override
    public List getProtocols() {
        return connectionFactory.getProtocols();
    }

    @Override
    public Connection newConnection(Connector connector, EndPoint endPoint) {
        final Connection connection = connectionFactory.newConnection(connector, endPoint);
        connection.addEventListener(new Connection.Listener() {
            private Timer.Context context;

            @Override
            public void onOpened(Connection connection) {
                this.context = timer.time();
                if (counter != null) {
                    counter.inc();
                }
            }

            @Override
            public void onClosed(Connection connection) {
                context.stop();
                if (counter != null) {
                    counter.dec();
                }
            }
        });
        return connection;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy