package com.yammer.metrics.jetty;

import com.yammer.metrics.Metrics;
import com.yammer.metrics.core.Counter;
import com.yammer.metrics.core.Meter;
import com.yammer.metrics.core.MetricsRegistry;
import com.yammer.metrics.core.Timer;
import org.eclipse.jetty.io.Connection;
import org.eclipse.jetty.server.bio.SocketConnector;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

public class InstrumentedSocketConnector extends SocketConnector {
    private final Timer duration;
    private final Meter accepts, connects, disconnects;
    private final Counter connections;

    public InstrumentedSocketConnector(int port) {
        this(Metrics.defaultRegistry(), port);
    }

    public InstrumentedSocketConnector(MetricsRegistry registry, int port) {
        super();
        setPort(port);
        this.duration = registry.newTimer(SocketConnector.class,
                                                    "connection-duration",
                                                    Integer.toString(port),
                                                    TimeUnit.MILLISECONDS,
                                                    TimeUnit.SECONDS);
        this.accepts = registry.newMeter(SocketConnector.class,
                                         "accepts",
                                         Integer.toString(port),
                                         "connections",
                                         TimeUnit.SECONDS);
        this.connects = registry.newMeter(SocketConnector.class,
                                                   "connects",
                                                   Integer.toString(port),
                                                   "connections",
                                                   TimeUnit.SECONDS);
        this.disconnects = registry.newMeter(SocketConnector.class,
                                                   "disconnects",
                                                   Integer.toString(port),
                                                   "connections",
                                                   TimeUnit.SECONDS);
        this.connections = registry.newCounter(SocketConnector.class,
                                               "active-connections",
                                               Integer.toString(port));
    }

    @Override
    public void accept(int acceptorID) throws IOException, InterruptedException {
        super.accept(acceptorID);
        accepts.mark();
    }

    @Override
    protected void connectionOpened(Connection connection) {
        connections.inc();
        super.connectionOpened(connection);
        connects.mark();
    }

    @Override
    protected void connectionClosed(Connection connection) {
        super.connectionClosed(connection);
        disconnects.mark();
        final long duration = System.currentTimeMillis() - connection.getTimeStamp();
        this.duration.update(duration, TimeUnit.MILLISECONDS);
        connections.dec();
    }
}

@@ -910,34 +910,24 @@
             
-          
 %22connection-
@@ -937,26 +937,16 @@
 ation%22,%0A
-          
         
@@ -1041,34 +1041,24 @@
             
-          
 TimeUnit.MIL
@@ -1068,26 +1068,16 @@
 ECONDS,%0A
-          
         
@@ -1529,33 +1529,24 @@
             
-         
 %22connects%22,%0A
@@ -1541,33 +1541,24 @@
 %22connects%22,%0A
-         
             
@@ -1649,33 +1649,24 @@
             
-         
 %22connections
@@ -1706,33 +1706,24 @@
             
-   
-      
 TimeUnit.SEC
@@ -1842,22 +1842,16 @@
         
-      
 %22disconn
@@ -1894,38 +1894,32 @@
                 
-   
-   
 Integer.toString
@@ -1963,38 +1963,32 @@
                 
-      
 %22connections%22,%0A 
@@ -1978,38 +1978,32 @@
  %22connections%22,%0A
-      
                 

Fix formatting in InstrumentedSocketConnector.