Class MetricRegistry

java.lang.Object
com.codahale.metrics.MetricRegistry
All Implemented Interfaces:
Metric, MetricSet
Direct Known Subclasses:
NoopMetricRegistry

public class MetricRegistry extends Object implements MetricSet
A registry of metric instances.
  • Constructor Details

    • MetricRegistry

      public MetricRegistry()
      Creates a new MetricRegistry.
  • Method Details

    • name

      public static String name(String name, 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 String name(Class<?> klass, 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
    • buildMap

      protected ConcurrentMap<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
    • registerGauge

      public <T> Gauge<T> registerGauge(String name, Gauge<T> metric) throws IllegalArgumentException
      Given a Gauge, registers it under the given name and returns it
      Type Parameters:
      T - the type of the gauge's value
      Parameters:
      name - the name of the gauge
      Returns:
      the registered Gauge
      Throws:
      IllegalArgumentException
      Since:
      4.2.10
    • register

      public <T extends Metric> T register(String name, T metric) throws 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:
      IllegalArgumentException - if the name is already registered or metric variable is null
    • registerAll

      public void registerAll(MetricSet metrics) throws IllegalArgumentException
      Given a metric set, registers them.
      Parameters:
      metrics - a set of metrics
      Throws:
      IllegalArgumentException - if any of the names are already registered
    • counter

      public Counter counter(String name)
      Return the Counter registered under this name; or create and register a new Counter if none is registered.
      Parameters:
      name - the name of the metric
      Returns:
      a new or pre-existing Counter
    • counter

      public Counter counter(String name, MetricRegistry.MetricSupplier<Counter> supplier)
      Return the Counter registered under this name; or create and register a new Counter using the provided MetricSupplier if none is registered.
      Parameters:
      name - the name of the metric
      supplier - a MetricSupplier that can be used to manufacture a counter.
      Returns:
      a new or pre-existing Counter
    • histogram

      public Histogram histogram(String name)
      Return the Histogram registered under this name; or create and register a new Histogram if none is registered.
      Parameters:
      name - the name of the metric
      Returns:
      a new or pre-existing Histogram
    • histogram

      public Histogram histogram(String name, MetricRegistry.MetricSupplier<Histogram> supplier)
      Return the Histogram registered under this name; or create and register a new Histogram using the provided MetricSupplier if none is registered.
      Parameters:
      name - the name of the metric
      supplier - a MetricSupplier that can be used to manufacture a histogram
      Returns:
      a new or pre-existing Histogram
    • meter

      public Meter meter(String name)
      Return the Meter registered under this name; or create and register a new Meter if none is registered.
      Parameters:
      name - the name of the metric
      Returns:
      a new or pre-existing Meter
    • meter

      public Meter meter(String name, MetricRegistry.MetricSupplier<Meter> supplier)
      Return the Meter registered under this name; or create and register a new Meter using the provided MetricSupplier if none is registered.
      Parameters:
      name - the name of the metric
      supplier - a MetricSupplier that can be used to manufacture a Meter
      Returns:
      a new or pre-existing Meter
    • timer

      public Timer timer(String name)
      Return the Timer registered under this name; or create and register a new Timer if none is registered.
      Parameters:
      name - the name of the metric
      Returns:
      a new or pre-existing Timer
    • timer

      public Timer timer(String name, MetricRegistry.MetricSupplier<Timer> supplier)
      Return the Timer registered under this name; or create and register a new Timer using the provided MetricSupplier if none is registered.
      Parameters:
      name - the name of the metric
      supplier - a MetricSupplier that can be used to manufacture a Timer
      Returns:
      a new or pre-existing Timer
    • gauge

      public <T extends Gauge> T gauge(String name)
      Return the Gauge registered under this name; or create and register a new SettableGauge if none is registered.
      Parameters:
      name - the name of the metric
      Returns:
      a pre-existing Gauge or a new SettableGauge
      Since:
      4.2
    • gauge

      public <T extends Gauge> T gauge(String name, MetricRegistry.MetricSupplier<T> supplier)
      Return the Gauge registered under this name; or create and register a new Gauge using the provided MetricSupplier if none is registered.
      Parameters:
      name - the name of the metric
      supplier - a MetricSupplier that can be used to manufacture a Gauge
      Returns:
      a new or pre-existing Gauge
    • remove

      public boolean remove(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
    • removeListener

      public void removeListener(MetricRegistryListener listener)
      Removes a MetricRegistryListener from this registry's collection of listeners.
      Parameters:
      listener - the listener that will be removed
    • getNames

      public SortedSet<String> getNames()
      Returns a set of the names of all the metrics in the registry.
      Returns:
      the names of all the metrics
    • getGauges

      public SortedMap<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 SortedMap<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 SortedMap<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 SortedMap<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 SortedMap<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 SortedMap<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 SortedMap<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 SortedMap<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 SortedMap<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 SortedMap<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
    • registerAll

      public void registerAll(String prefix, MetricSet metrics) throws IllegalArgumentException
      Given a metric set, registers them with the given prefix prepended to their names.
      Parameters:
      prefix - a name prefix
      metrics - a set of metrics
      Throws:
      IllegalArgumentException - if any of the names are already registered
    • getMetrics

      public Map<String,Metric> getMetrics()
      Description copied from interface: MetricSet
      A map of metric names to metrics.
      Specified by:
      getMetrics in interface MetricSet
      Returns:
      the metrics