Package com.codahale.metrics
Class MetricRegistry
- java.lang.Object
-
- com.codahale.metrics.MetricRegistry
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static interfaceMetricRegistry.MetricBuilder<T extends Metric>A quick and easy way of capturing the notion of default metrics.
-
Field Summary
Fields Modifier and Type Field Description private java.util.List<MetricRegistryListener>listenersprivate java.util.concurrent.ConcurrentMap<java.lang.String,Metric>metrics
-
Constructor Summary
Constructors Constructor Description MetricRegistry()Creates a newMetricRegistry.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddListener(MetricRegistryListener listener)Adds aMetricRegistryListenerto a collection of listeners that will be notified on metric creation.private static voidappend(java.lang.StringBuilder builder, java.lang.String part)protected java.util.concurrent.ConcurrentMap<java.lang.String,Metric>buildMap()Creates a newConcurrentMapimplementation for use inside the registry.Countercounter(java.lang.String name)Creates a newCounterand registers it under the given name.java.util.SortedMap<java.lang.String,Counter>getCounters()Returns a map of all the counters in the registry and their names.java.util.SortedMap<java.lang.String,Counter>getCounters(MetricFilter filter)Returns a map of all the counters in the registry and their names which match the given filter.java.util.SortedMap<java.lang.String,Gauge>getGauges()Returns a map of all the gauges in the registry and their names.java.util.SortedMap<java.lang.String,Gauge>getGauges(MetricFilter filter)Returns a map of all the gauges in the registry and their names which match the given filter.java.util.SortedMap<java.lang.String,Histogram>getHistograms()Returns a map of all the histograms in the registry and their names.java.util.SortedMap<java.lang.String,Histogram>getHistograms(MetricFilter filter)Returns a map of all the histograms in the registry and their names which match the given filter.java.util.SortedMap<java.lang.String,Meter>getMeters()Returns a map of all the meters in the registry and their names.java.util.SortedMap<java.lang.String,Meter>getMeters(MetricFilter filter)Returns a map of all the meters in the registry and their names which match the given filter.java.util.Map<java.lang.String,Metric>getMetrics()A map of metric names to metrics.private <T extends Metric>
java.util.SortedMap<java.lang.String,T>getMetrics(java.lang.Class<T> klass, MetricFilter filter)java.util.SortedSet<java.lang.String>getNames()Returns a set of the names of all the metrics in the registry.private <T extends Metric>
TgetOrAdd(java.lang.String name, MetricRegistry.MetricBuilder<T> builder)java.util.SortedMap<java.lang.String,Timer>getTimers()Returns a map of all the timers in the registry and their names.java.util.SortedMap<java.lang.String,Timer>getTimers(MetricFilter filter)Returns a map of all the timers in the registry and their names which match the given filter.Histogramhistogram(java.lang.String name)Creates a newHistogramand registers it under the given name.Metermeter(java.lang.String name)Creates a newMeterand registers it under the given name.static java.lang.Stringname(java.lang.Class<?> klass, java.lang.String... names)Concatenates a class name and elements to form a dotted name, eliding any null values or empty strings.static java.lang.Stringname(java.lang.String name, java.lang.String... names)Concatenates elements to form a dotted name, eliding any null values or empty strings.private voidnotifyListenerOfAddedMetric(MetricRegistryListener listener, Metric metric, java.lang.String name)private voidnotifyListenerOfRemovedMetric(java.lang.String name, Metric metric, MetricRegistryListener listener)private voidonMetricAdded(java.lang.String name, Metric metric)private voidonMetricRemoved(java.lang.String name, Metric metric)<T extends Metric>
Tregister(java.lang.String name, T metric)Given aMetric, registers it under the given name.voidregisterAll(MetricSet metrics)Given a metric set, registers them.private voidregisterAll(java.lang.String prefix, MetricSet metrics)booleanremove(java.lang.String name)Removes the metric with the given name.voidremoveListener(MetricRegistryListener listener)Removes aMetricRegistryListenerfrom this registry's collection of listeners.voidremoveMatching(MetricFilter filter)Removes all metrics which match the given filter.Timertimer(java.lang.String name)Creates a newTimerand registers it under the given name.
-
-
-
Field Detail
-
metrics
private final java.util.concurrent.ConcurrentMap<java.lang.String,Metric> metrics
-
listeners
private final java.util.List<MetricRegistryListener> listeners
-
-
Constructor Detail
-
MetricRegistry
public MetricRegistry()
Creates a newMetricRegistry.
-
-
Method Detail
-
name
public static java.lang.String name(java.lang.String name, java.lang.String... names)Concatenates elements to form a dotted name, eliding any null values or empty strings.- Parameters:
name- the first element of the namenames- the remaining elements of the name- Returns:
nameandnamesconcatenated by periods
-
name
public static java.lang.String name(java.lang.Class<?> klass, java.lang.String... names)Concatenates a class name and elements to form a dotted name, eliding any null values or empty strings.- Parameters:
klass- the first element of the namenames- the remaining elements of the name- Returns:
klassandnamesconcatenated by periods
-
append
private static void append(java.lang.StringBuilder builder, java.lang.String part)
-
buildMap
protected java.util.concurrent.ConcurrentMap<java.lang.String,Metric> buildMap()
Creates a newConcurrentMapimplementation for use inside the registry. Override this to create aMetricRegistrywith space- or time-bounded metric lifecycles, for example.- Returns:
- a new
ConcurrentMap
-
register
public <T extends Metric> T register(java.lang.String name, T metric) throws java.lang.IllegalArgumentException
Given aMetric, registers it under the given name.- Type Parameters:
T- the type of the metric- Parameters:
name- the name of the metricmetric- the metric- Returns:
metric- Throws:
java.lang.IllegalArgumentException- if the name is already registered
-
registerAll
public void registerAll(MetricSet metrics) throws java.lang.IllegalArgumentException
Given a metric set, registers them.- Parameters:
metrics- a set of metrics- Throws:
java.lang.IllegalArgumentException- if any of the names are already registered
-
counter
public Counter counter(java.lang.String name)
Creates a newCounterand registers it under the given name.- Parameters:
name- the name of the metric- Returns:
- a new
Counter
-
histogram
public Histogram histogram(java.lang.String name)
Creates a newHistogramand registers it under the given name.- Parameters:
name- the name of the metric- Returns:
- a new
Histogram
-
meter
public Meter meter(java.lang.String name)
Creates a newMeterand registers it under the given name.- Parameters:
name- the name of the metric- Returns:
- a new
Meter
-
timer
public Timer timer(java.lang.String name)
Creates a newTimerand registers it under the given name.- Parameters:
name- the name of the metric- Returns:
- a new
Timer
-
remove
public boolean remove(java.lang.String name)
Removes the metric with the given name.- Parameters:
name- the name of the metric- Returns:
- whether or not the metric was removed
-
removeMatching
public void removeMatching(MetricFilter filter)
Removes all metrics which match the given filter.- Parameters:
filter- a filter
-
addListener
public void addListener(MetricRegistryListener listener)
Adds aMetricRegistryListenerto 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(MetricRegistryListener listener)
Removes aMetricRegistryListenerfrom this registry's collection of listeners.- Parameters:
listener- the listener that will be removed
-
getNames
public java.util.SortedSet<java.lang.String> getNames()
Returns a set of the names of all the metrics in the registry.- Returns:
- the names of all the metrics
-
getGauges
public java.util.SortedMap<java.lang.String,Gauge> getGauges()
Returns a map of all the gauges in the registry and their names.- Returns:
- all the gauges in the registry
-
getGauges
public java.util.SortedMap<java.lang.String,Gauge> getGauges(MetricFilter filter)
Returns a map of all the gauges in the registry and their names which match the given filter.- Parameters:
filter- the metric filter to match- Returns:
- all the gauges in the registry
-
getCounters
public java.util.SortedMap<java.lang.String,Counter> getCounters()
Returns a map of all the counters in the registry and their names.- Returns:
- all the counters in the registry
-
getCounters
public java.util.SortedMap<java.lang.String,Counter> getCounters(MetricFilter filter)
Returns a map of all the counters in the registry and their names which match the given filter.- Parameters:
filter- the metric filter to match- Returns:
- all the counters in the registry
-
getHistograms
public java.util.SortedMap<java.lang.String,Histogram> getHistograms()
Returns a map of all the histograms in the registry and their names.- Returns:
- all the histograms in the registry
-
getHistograms
public java.util.SortedMap<java.lang.String,Histogram> getHistograms(MetricFilter filter)
Returns a map of all the histograms in the registry and their names which match the given filter.- Parameters:
filter- the metric filter to match- Returns:
- all the histograms in the registry
-
getMeters
public java.util.SortedMap<java.lang.String,Meter> getMeters()
Returns a map of all the meters in the registry and their names.- Returns:
- all the meters in the registry
-
getMeters
public java.util.SortedMap<java.lang.String,Meter> getMeters(MetricFilter filter)
Returns a map of all the meters in the registry and their names which match the given filter.- Parameters:
filter- the metric filter to match- Returns:
- all the meters in the registry
-
getTimers
public java.util.SortedMap<java.lang.String,Timer> getTimers()
Returns a map of all the timers in the registry and their names.- Returns:
- all the timers in the registry
-
getTimers
public java.util.SortedMap<java.lang.String,Timer> getTimers(MetricFilter filter)
Returns a map of all the timers in the registry and their names which match the given filter.- Parameters:
filter- the metric filter to match- Returns:
- all the timers in the registry
-
getOrAdd
private <T extends Metric> T getOrAdd(java.lang.String name, MetricRegistry.MetricBuilder<T> builder)
-
getMetrics
private <T extends Metric> java.util.SortedMap<java.lang.String,T> getMetrics(java.lang.Class<T> klass, MetricFilter filter)
-
onMetricAdded
private void onMetricAdded(java.lang.String name, Metric metric)
-
notifyListenerOfAddedMetric
private void notifyListenerOfAddedMetric(MetricRegistryListener listener, Metric metric, java.lang.String name)
-
onMetricRemoved
private void onMetricRemoved(java.lang.String name, Metric metric)
-
notifyListenerOfRemovedMetric
private void notifyListenerOfRemovedMetric(java.lang.String name, Metric metric, MetricRegistryListener listener)
-
registerAll
private void registerAll(java.lang.String prefix, MetricSet metrics) throws java.lang.IllegalArgumentException- Throws:
java.lang.IllegalArgumentException
-
getMetrics
public java.util.Map<java.lang.String,Metric> getMetrics()
Description copied from interface:MetricSetA map of metric names to metrics.- Specified by:
getMetricsin interfaceMetricSet- Returns:
- the metrics
-
-