Class MetricRegistry

  • All Implemented Interfaces:
    Metric, MetricSet

    public class MetricRegistry
    extends java.lang.Object
    implements MetricSet
    A registry of metric instances.
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      private static interface  MetricRegistry.MetricBuilder<T extends Metric>
      A quick and easy way of capturing the notion of default metrics.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void addListener​(MetricRegistryListener listener)
      Adds a MetricRegistryListener to a collection of listeners that will be notified on metric creation.
      private static void append​(java.lang.StringBuilder builder, java.lang.String part)  
      protected java.util.concurrent.ConcurrentMap<java.lang.String,​Metric> buildMap()
      Creates a new ConcurrentMap implementation for use inside the registry.
      Counter counter​(java.lang.String name)
      Creates a new Counter and 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>
      T
      getOrAdd​(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.
      Histogram histogram​(java.lang.String name)
      Creates a new Histogram and registers it under the given name.
      Meter meter​(java.lang.String name)
      Creates a new Meter and registers it under the given name.
      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.
      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.
      private void notifyListenerOfAddedMetric​(MetricRegistryListener listener, Metric metric, java.lang.String name)  
      private void notifyListenerOfRemovedMetric​(java.lang.String name, Metric metric, MetricRegistryListener listener)  
      private void onMetricAdded​(java.lang.String name, Metric metric)  
      private void onMetricRemoved​(java.lang.String name, Metric metric)  
      <T extends Metric>
      T
      register​(java.lang.String name, T metric)
      Given a Metric, registers it under the given name.
      void registerAll​(MetricSet metrics)
      Given a metric set, registers them.
      private void registerAll​(java.lang.String prefix, MetricSet metrics)  
      boolean remove​(java.lang.String name)
      Removes the metric with the given name.
      void removeListener​(MetricRegistryListener listener)
      Removes a MetricRegistryListener from this registry's collection of listeners.
      void removeMatching​(MetricFilter filter)
      Removes all metrics which match the given filter.
      Timer timer​(java.lang.String name)
      Creates a new Timer and registers it under the given name.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • metrics

        private final java.util.concurrent.ConcurrentMap<java.lang.String,​Metric> metrics
    • Constructor Detail

      • MetricRegistry

        public MetricRegistry()
        Creates a new MetricRegistry.
    • 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 name
        names - the remaining elements of the name
        Returns:
        name and names concatenated 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 name
        names - the remaining elements of the name
        Returns:
        klass and names concatenated 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 new ConcurrentMap implementation for use inside the registry. Override this to create a MetricRegistry with 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 a Metric, registers it under the given name.
        Type Parameters:
        T - the type of the metric
        Parameters:
        name - the name of the metric
        metric - 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 new Counter and 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 new Histogram and 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 new Meter and 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 new Timer and 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 a MetricRegistryListener 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
      • 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
      • 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: MetricSet
        A map of metric names to metrics.
        Specified by:
        getMetrics in interface MetricSet
        Returns:
        the metrics