com.twitter.common.net.monitoring
Class TrafficMonitor<K>

java.lang.Object
  extended by com.twitter.common.net.monitoring.TrafficMonitor<K>
All Implemented Interfaces:
RequestTracker<K>, ConnectionMonitor<K>

public class TrafficMonitor<K>
extends Object
implements ConnectionMonitor<K>, RequestTracker<K>

Monitors activity on established connections between two hosts. This can be used for a server to track inbound clients, or for a client to track requests sent to different servers. The monitor will retain information for hosts that may no longer be active, but will expunge information for hosts that have been idle for more than five minutes.


Nested Class Summary
 class TrafficMonitor.TrafficInfo
          Information about traffic obsserved to/from a specific host.
 
Nested classes/interfaces inherited from interface com.twitter.common.net.loadbalancing.RequestTracker
RequestTracker.RequestResult
 
Constructor Summary
TrafficMonitor(String serviceName)
          Creates a new traffic monitor using the default cleanup interval.
TrafficMonitor(String serviceName, Amount<Long,Time> gcInterval)
          Creates a new traffic monitor with a custom cleanup interval.
 
Method Summary
 void connected(K key)
          Instructs the monitor that a connection was established.
static
<T> TrafficMonitor<T>
create(String serviceName)
          Convenience method to create a typed traffic monitor.
 long getLifetimeRequestCount()
          Gets the total number of requests that this monitor has observed, for all remote hosts.
 String getServiceName()
          Gets the name of the service that this monitor is monitoring.
 Map<K,TrafficMonitor.TrafficInfo> getTrafficInfo()
          Fetches all current traffic information.
 void released(K key)
          Informs the monitor that a connection was released.
 void requestResult(K key, RequestTracker.RequestResult result, long requestTimeNanos)
          Informs the tracker of a completed request.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

TrafficMonitor

public TrafficMonitor(String serviceName)
Creates a new traffic monitor using the default cleanup interval.

Parameters:
serviceName - Name of the service to monitor, used for creating variable names.

TrafficMonitor

public TrafficMonitor(String serviceName,
                      Amount<Long,Time> gcInterval)
Creates a new traffic monitor with a custom cleanup interval.

Parameters:
serviceName - Service name for the monitor.
gcInterval - Interval on which the remote host garbage collector should run.
Method Detail

create

public static <T> TrafficMonitor<T> create(String serviceName)
Convenience method to create a typed traffic monitor.

Type Parameters:
T - Monitor type.
Parameters:
serviceName - Service name for the monitor.
Returns:
A new traffic monitor.

getServiceName

public String getServiceName()
Gets the name of the service that this monitor is monitoring.

Returns:
Monitor's service name.

getLifetimeRequestCount

public long getLifetimeRequestCount()
Gets the total number of requests that this monitor has observed, for all remote hosts.

Returns:
Total number of requests observed.

getTrafficInfo

public Map<K,TrafficMonitor.TrafficInfo> getTrafficInfo()
Fetches all current traffic information.

Returns:
A map from the host key type to information about that host.

connected

public void connected(K key)
Description copied from interface: ConnectionMonitor
Instructs the monitor that a connection was established.

Specified by:
connected in interface ConnectionMonitor<K>
Parameters:
key - Key for the host that a connection was established with.

released

public void released(K key)
Description copied from interface: ConnectionMonitor
Informs the monitor that a connection was released.

Specified by:
released in interface ConnectionMonitor<K>
Parameters:
key - Key for the host that a connection was released for.

requestResult

public void requestResult(K key,
                          RequestTracker.RequestResult result,
                          long requestTimeNanos)
Description copied from interface: RequestTracker
Informs the tracker of a completed request.

Specified by:
requestResult in interface RequestTracker<K>
Parameters:
key - Key to identify the owner of the request.
result - Result of the request.
requestTimeNanos - Time duration spent waiting for the request to complete.