com.yammer.metrics.core
Class MetricsRegistry

java.lang.Object
  extended by com.yammer.metrics.core.MetricsRegistry

public class MetricsRegistry
extends Object

A registry of metric instances.


Constructor Summary
MetricsRegistry()
          Creates a new MetricsRegistry.
MetricsRegistry(Clock clock)
          Creates a new MetricsRegistry with the given Clock instance.
 
Method Summary
 void addListener(MetricsRegistryListener listener)
          Adds a MetricsRegistryListener to a collection of listeners that will be notified on metric creation.
 Map<MetricName,Metric> allMetrics()
          Returns an unmodifiable map of all metrics and their names.
protected  MetricName createName(Class<?> klass, String name, String scope)
          Override to customize how MetricNames are created.
protected
<T extends Metric>
T
getOrAdd(MetricName name, T metric)
          Gets any existing metric with the given name or, if none exists, adds the given metric.
 SortedMap<String,SortedMap<MetricName,Metric>> groupedMetrics()
          Returns a grouped and sorted map of all registered metrics.
 SortedMap<String,SortedMap<MetricName,Metric>> groupedMetrics(MetricPredicate predicate)
          Returns a grouped and sorted map of all registered metrics which match then given MetricPredicate.
 Counter newCounter(Class<?> klass, String name)
          Creates a new Counter and registers it under the given class and name.
 Counter newCounter(Class<?> klass, String name, String scope)
          Creates a new Counter and registers it under the given class and name.
 Counter newCounter(MetricName metricName)
          Creates a new Counter and registers it under the given metric name.
<T> Gauge<T>
newGauge(Class<?> klass, String name, Gauge<T> metric)
          Given a new Gauge, registers it under the given class and name.
<T> Gauge<T>
newGauge(Class<?> klass, String name, String scope, Gauge<T> metric)
          Given a new Gauge, registers it under the given class and name.
<T> Gauge<T>
newGauge(MetricName metricName, Gauge<T> metric)
          Given a new Gauge, registers it under the given metric name.
 Histogram newHistogram(Class<?> klass, String name)
          Creates a new non-biased Histogram and registers it under the given class and name.
 Histogram newHistogram(Class<?> klass, String name, boolean biased)
          Creates a new Histogram and registers it under the given class and name.
 Histogram newHistogram(Class<?> klass, String name, String scope)
          Creates a new non-biased Histogram and registers it under the given class, name, and scope.
 Histogram newHistogram(Class<?> klass, String name, String scope, boolean biased)
          Creates a new Histogram and registers it under the given class, name, and scope.
 Histogram newHistogram(MetricName metricName, boolean biased)
          Creates a new Histogram and registers it under the given metric name.
 Meter newMeter(Class<?> klass, String name, String scope, String eventType, TimeUnit unit)
          Creates a new Meter and registers it under the given class, name, and scope.
 Meter newMeter(Class<?> klass, String name, String eventType, TimeUnit unit)
          Creates a new Meter and registers it under the given class and name.
 Meter newMeter(MetricName metricName, String eventType, TimeUnit unit)
          Creates a new Meter and registers it under the given metric name.
protected  ConcurrentMap<MetricName,Metric> newMetricsMap()
          Returns a new ConcurrentMap implementation.
 ScheduledExecutorService newScheduledThreadPool(int poolSize, String name)
          Creates a new scheduled thread pool of a given size with the given name, or returns an existing thread pool if one was already created with the same name.
 Timer newTimer(Class<?> klass, String name)
          Creates a new Timer and registers it under the given class and name, measuring elapsed time in milliseconds and invocations per second.
 Timer newTimer(Class<?> klass, String name, String scope)
          Creates a new Timer and registers it under the given class, name, and scope, measuring elapsed time in milliseconds and invocations per second.
 Timer newTimer(Class<?> klass, String name, String scope, TimeUnit durationUnit, TimeUnit rateUnit)
          Creates a new Timer and registers it under the given class, name, and scope.
 Timer newTimer(Class<?> klass, String name, TimeUnit durationUnit, TimeUnit rateUnit)
          Creates a new Timer and registers it under the given class and name.
 Timer newTimer(MetricName metricName, TimeUnit durationUnit, TimeUnit rateUnit)
          Creates a new Timer and registers it under the given metric name.
 void removeListener(MetricsRegistryListener listener)
          Removes a MetricsRegistryListener from this registry's collection of listeners.
 void removeMetric(Class<?> klass, String name)
          Removes the metric for the given class with the given name.
 void removeMetric(Class<?> klass, String name, String scope)
          Removes the metric for the given class with the given name and scope.
 void removeMetric(MetricName name)
          Removes the metric with the given name.
 void shutdown()
          Shut down this registry's thread pools.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

MetricsRegistry

public MetricsRegistry()
Creates a new MetricsRegistry.


MetricsRegistry

public MetricsRegistry(Clock clock)
Creates a new MetricsRegistry with the given Clock instance.

Parameters:
clock - a Clock instance
Method Detail

newGauge

public <T> Gauge<T> newGauge(Class<?> klass,
                             String name,
                             Gauge<T> metric)
Given a new Gauge, registers it under the given class and name.

Type Parameters:
T - the type of the value returned by the metric
Parameters:
klass - the class which owns the metric
name - the name of the metric
metric - the metric
Returns:
metric

newGauge

public <T> Gauge<T> newGauge(Class<?> klass,
                             String name,
                             String scope,
                             Gauge<T> metric)
Given a new Gauge, registers it under the given class and name.

Type Parameters:
T - the type of the value returned by the metric
Parameters:
klass - the class which owns the metric
name - the name of the metric
scope - the scope of the metric
metric - the metric
Returns:
metric

newGauge

public <T> Gauge<T> newGauge(MetricName metricName,
                             Gauge<T> metric)
Given a new Gauge, registers it under the given metric name.

Type Parameters:
T - the type of the value returned by the metric
Parameters:
metricName - the name of the metric
metric - the metric
Returns:
metric

newCounter

public Counter newCounter(Class<?> klass,
                          String name)
Creates a new Counter and registers it under the given class and name.

Parameters:
klass - the class which owns the metric
name - the name of the metric
Returns:
a new Counter

newCounter

public Counter newCounter(Class<?> klass,
                          String name,
                          String scope)
Creates a new Counter and registers it under the given class and name.

Parameters:
klass - the class which owns the metric
name - the name of the metric
scope - the scope of the metric
Returns:
a new Counter

newCounter

public Counter newCounter(MetricName metricName)
Creates a new Counter and registers it under the given metric name.

Parameters:
metricName - the name of the metric
Returns:
a new Counter

newHistogram

public Histogram newHistogram(Class<?> klass,
                              String name,
                              boolean biased)
Creates a new Histogram and registers it under the given class and name.

Parameters:
klass - the class which owns the metric
name - the name of the metric
biased - whether or not the histogram should be biased
Returns:
a new Histogram

newHistogram

public Histogram newHistogram(Class<?> klass,
                              String name,
                              String scope,
                              boolean biased)
Creates a new Histogram and registers it under the given class, name, and scope.

Parameters:
klass - the class which owns the metric
name - the name of the metric
scope - the scope of the metric
biased - whether or not the histogram should be biased
Returns:
a new Histogram

newHistogram

public Histogram newHistogram(Class<?> klass,
                              String name)
Creates a new non-biased Histogram and registers it under the given class and name.

Parameters:
klass - the class which owns the metric
name - the name of the metric
Returns:
a new Histogram

newHistogram

public Histogram newHistogram(Class<?> klass,
                              String name,
                              String scope)
Creates a new non-biased Histogram and registers it under the given class, name, and scope.

Parameters:
klass - the class which owns the metric
name - the name of the metric
scope - the scope of the metric
Returns:
a new Histogram

newHistogram

public Histogram newHistogram(MetricName metricName,
                              boolean biased)
Creates a new Histogram and registers it under the given metric name.

Parameters:
metricName - the name of the metric
biased - whether or not the histogram should be biased
Returns:
a new Histogram

newMeter

public Meter newMeter(Class<?> klass,
                      String name,
                      String eventType,
                      TimeUnit unit)
Creates a new Meter and registers it under the given class and name.

Parameters:
klass - the class which owns the metric
name - the name of the metric
eventType - the plural name of the type of events the meter is measuring (e.g., "requests")
unit - the rate unit of the new meter
Returns:
a new Meter

newMeter

public Meter newMeter(Class<?> klass,
                      String name,
                      String scope,
                      String eventType,
                      TimeUnit unit)
Creates a new Meter and registers it under the given class, name, and scope.

Parameters:
klass - the class which owns the metric
name - the name of the metric
scope - the scope of the metric
eventType - the plural name of the type of events the meter is measuring (e.g., "requests")
unit - the rate unit of the new meter
Returns:
a new Meter

newMeter

public Meter newMeter(MetricName metricName,
                      String eventType,
                      TimeUnit unit)
Creates a new Meter and registers it under the given metric name.

Parameters:
metricName - the name of the metric
eventType - the plural name of the type of events the meter is measuring (e.g., "requests")
unit - the rate unit of the new meter
Returns:
a new Meter

newTimer

public Timer newTimer(Class<?> klass,
                      String name)
Creates a new Timer and registers it under the given class and name, measuring elapsed time in milliseconds and invocations per second.

Parameters:
klass - the class which owns the metric
name - the name of the metric
Returns:
a new Timer

newTimer

public Timer newTimer(Class<?> klass,
                      String name,
                      TimeUnit durationUnit,
                      TimeUnit rateUnit)
Creates a new Timer and registers it under the given class and name.

Parameters:
klass - the class which owns the metric
name - the name of the metric
durationUnit - the duration scale unit of the new timer
rateUnit - the rate scale unit of the new timer
Returns:
a new Timer

newTimer

public Timer newTimer(Class<?> klass,
                      String name,
                      String scope)
Creates a new Timer and registers it under the given class, name, and scope, measuring elapsed time in milliseconds and invocations per second.

Parameters:
klass - the class which owns the metric
name - the name of the metric
scope - the scope of the metric
Returns:
a new Timer

newTimer

public Timer newTimer(Class<?> klass,
                      String name,
                      String scope,
                      TimeUnit durationUnit,
                      TimeUnit rateUnit)
Creates a new Timer and registers it under the given class, name, and scope.

Parameters:
klass - the class which owns the metric
name - the name of the metric
scope - the scope of the metric
durationUnit - the duration scale unit of the new timer
rateUnit - the rate scale unit of the new timer
Returns:
a new Timer

newTimer

public Timer newTimer(MetricName metricName,
                      TimeUnit durationUnit,
                      TimeUnit rateUnit)
Creates a new Timer and registers it under the given metric name.

Parameters:
metricName - the name of the metric
durationUnit - the duration scale unit of the new timer
rateUnit - the rate scale unit of the new timer
Returns:
a new Timer

allMetrics

public Map<MetricName,Metric> allMetrics()
Returns an unmodifiable map of all metrics and their names.

Returns:
an unmodifiable map of all metrics and their names

groupedMetrics

public SortedMap<String,SortedMap<MetricName,Metric>> groupedMetrics()
Returns a grouped and sorted map of all registered metrics.

Returns:
all registered metrics, grouped by name and sorted

groupedMetrics

public SortedMap<String,SortedMap<MetricName,Metric>> groupedMetrics(MetricPredicate predicate)
Returns a grouped and sorted map of all registered metrics which match then given MetricPredicate.

Parameters:
predicate - a predicate which metrics have to match to be in the results
Returns:
all registered metrics which match predicate, sorted by name

shutdown

public void shutdown()
Shut down this registry's thread pools.


newScheduledThreadPool

public ScheduledExecutorService newScheduledThreadPool(int poolSize,
                                                       String name)
Creates a new scheduled thread pool of a given size with the given name, or returns an existing thread pool if one was already created with the same name.

Parameters:
poolSize - the number of threads to create
name - the name of the pool
Returns:
a new ScheduledExecutorService

removeMetric

public void removeMetric(Class<?> klass,
                         String name)
Removes the metric for the given class with the given name.

Parameters:
klass - the klass the metric is associated with
name - the name of the metric

removeMetric

public void removeMetric(Class<?> klass,
                         String name,
                         String scope)
Removes the metric for the given class with the given name and scope.

Parameters:
klass - the klass the metric is associated with
name - the name of the metric
scope - the scope of the metric

removeMetric

public void removeMetric(MetricName name)
Removes the metric with the given name.

Parameters:
name - the name of the metric

addListener

public void addListener(MetricsRegistryListener listener)
Adds a MetricsRegistryListener to a collection of listeners that will be notified on metric creation. Listeners will be notified in the order in which they are added.

N.B.: The listener will be notified of all existing metrics when it first registers.

Parameters:
listener - the listener that will be notified

removeListener

public void removeListener(MetricsRegistryListener listener)
Removes a MetricsRegistryListener from this registry's collection of listeners.

Parameters:
listener - the listener that will be removed

createName

protected MetricName createName(Class<?> klass,
                                String name,
                                String scope)
Override to customize how MetricNames are created.

Parameters:
klass - the class which owns the metric
name - the name of the metric
scope - the metric's scope
Returns:
the metric's full name

newMetricsMap

protected ConcurrentMap<MetricName,Metric> newMetricsMap()
Returns a new ConcurrentMap implementation. Subclass this to do weird things with your own MetricsRegistry implementation.

Returns:
a new ConcurrentMap

getOrAdd

protected final <T extends Metric> T getOrAdd(MetricName name,
                                              T metric)
Gets any existing metric with the given name or, if none exists, adds the given metric.

Type Parameters:
T - the type of the metric
Parameters:
name - the metric's name
metric - the new metric
Returns:
either the existing metric or metric


Copyright © 2012. All Rights Reserved.