Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.yammer.metrics.core;
  
  
  import java.util.*;
A registry of metric instances.
 
 public class MetricsRegistry {
     private static final int EXPECTED_METRIC_COUNT = 1024;
     private final Clock clock;
     private final ConcurrentMap<MetricNameMetricmetrics;
     private final ThreadPools threadPools;
     private final List<MetricsRegistryListenerlisteners;

    
Creates a new MetricsRegistry.
 
     public MetricsRegistry() {
         this(Clock.defaultClock());
     }

    
Creates a new MetricsRegistry with the given Clock instance.

Parameters:
clock a Clock instance
 
     public MetricsRegistry(Clock clock) {
         this. = clock;
         this. = newMetricsMap();
         this. = new ThreadPools();
         this. = new CopyOnWriteArrayList<MetricsRegistryListener>();
     }

    
Given a new Gauge, registers it under the given class and name.

Parameters:
klass the class which owns the metric
name the name of the metric
metric the metric
<T> the type of the value returned by the metric
Returns:
metric
 
     public <T> Gauge<T> newGauge(Class<?> klass,
                                  String name,
                                  Gauge<T> metric) {
         return newGauge(klassnamenullmetric);
     }

    
Given a new Gauge, 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
metric the metric
<T> the type of the value returned by the metric
Returns:
metric
 
     public <T> Gauge<T> newGauge(Class<?> klass,
                                  String name,
                                  String scope,
                                  Gauge<T> metric) {
         return newGauge(createName(klassnamescope), metric);
     }

    
Given a new Gauge, registers it under the given metric name.

Parameters:
metricName the name of the metric
metric the metric
<T> the type of the value returned by the metric
Returns:
metric
 
     public <T> Gauge<T> newGauge(MetricName metricName,
                                  Gauge<T> metric) {
         return getOrAdd(metricNamemetric);
     }

    
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
 
     public Counter newCounter(Class<?> klass,
                               String name) {
         return newCounter(klassnamenull);
     }

    
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
    public Counter newCounter(Class<?> klass,
                              String name,
                              String scope) {
        return newCounter(createName(klassnamescope));
    }

    
Creates a new Counter and registers it under the given metric name.

Parameters:
metricName the name of the metric
Returns:
a new Counter
    public Counter newCounter(MetricName metricName) {
        return getOrAdd(metricNamenew Counter());
    }

    
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
    public Histogram newHistogram(Class<?> klass,
                                  String name,
                                  boolean biased) {
        return newHistogram(klassnamenullbiased);
    }

    
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
    public Histogram newHistogram(Class<?> klass,
                                  String name,
                                  String scope,
                                  boolean biased) {
        return newHistogram(createName(klassnamescope), biased);
    }

    
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
    public Histogram newHistogram(Class<?> klass,
                                  String name) {
        return newHistogram(klassnamefalse);
    }

    
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
    public Histogram newHistogram(Class<?> klass,
                                  String name,
                                  String scope) {
        return newHistogram(klassnamescopefalse);
    }

    
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
    public Histogram newHistogram(MetricName metricName,
                                  boolean biased) {
        return getOrAdd(metricName,
                        new Histogram(biased ? . : .));
    }

    
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
    public Meter newMeter(Class<?> klass,
                          String name,
                          String eventType,
                          TimeUnit unit) {
        return newMeter(klassnamenulleventTypeunit);
    }

    
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
    public Meter newMeter(Class<?> klass,
                          String name,
                          String scope,
                          String eventType,
                          TimeUnit unit) {
        return newMeter(createName(klassnamescope), eventTypeunit);
    }

    
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
    public Meter newMeter(MetricName metricName,
                          String eventType,
                          TimeUnit unit) {
        final Metric existingMetric = .get(metricName);
        if (existingMetric != null) {
            return (MeterexistingMetric;
        }
        return getOrAdd(metricNamenew Meter(newMeterTickThreadPool(), eventTypeunit));
    }

    
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
    public Timer newTimer(Class<?> klass,
                          String name) {
        return newTimer(klassnamenull..);
    }

    
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
    public Timer newTimer(Class<?> klass,
                          String name,
                          TimeUnit durationUnit,
                          TimeUnit rateUnit) {
        return newTimer(klassnamenulldurationUnitrateUnit);
    }

    
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
    public Timer newTimer(Class<?> klass,
                          String name,
                          String scope) {
        return newTimer(klassnamescope..);
    }

    
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
    public Timer newTimer(Class<?> klass,
                          String name,
                          String scope,
                          TimeUnit durationUnit,
                          TimeUnit rateUnit) {
        return newTimer(createName(klassnamescope), durationUnitrateUnit);
    }

    
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
    public Timer newTimer(MetricName metricName,
                          TimeUnit durationUnit,
                          TimeUnit rateUnit) {
        final Metric existingMetric = .get(metricName);
        if (existingMetric != null) {
            return (TimerexistingMetric;
        }
        return getOrAdd(metricName,
                        new Timer(newMeterTickThreadPool(), durationUnitrateUnit));
    }

    
Returns an unmodifiable map of all metrics and their names.

Returns:
an unmodifiable map of all metrics and their names
    public Map<MetricNameMetricallMetrics() {
        return Collections.unmodifiableMap();
    }

    
Returns a grouped and sorted map of all registered metrics.

Returns:
all registered metrics, grouped by name and sorted
        return groupedMetrics(.);
    }

    
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
        final SortedMap<StringSortedMap<MetricNameMetric>> groups =
                new TreeMap<StringSortedMap<MetricNameMetric>>();
        for (Map.Entry<MetricNameMetricentry : .entrySet()) {
            final String qualifiedTypeName = entry.getKey().getGroup() + "." + entry.getKey()
                                                                                    .getType();
            if (predicate.matches(entry.getKey(), entry.getValue())) {
                final String scopedName;
                if (entry.getKey().hasScope()) {
                    scopedName = qualifiedTypeName + "." + entry.getKey().getScope();
                } else {
                    scopedName = qualifiedTypeName;
                }
                SortedMap<MetricNameMetricgroup = groups.get(scopedName);
                if (group == null) {
                    group = new TreeMap<MetricNameMetric>();
                    groups.put(scopedNamegroup);
                }
                group.put(entry.getKey(), entry.getValue());
            }
        }
        return Collections.unmodifiableSortedMap(groups);
    }

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

    
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 java.util.concurrent.ScheduledExecutorService
    public ScheduledExecutorService newScheduledThreadPool(int poolSizeString name) {
        return .newScheduledThreadPool(poolSizename);
    }

    
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
    public void removeMetric(Class<?> klass,
                             String name) {
        removeMetric(klassnamenull);
    }

    
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
    public void removeMetric(Class<?> klass,
                             String name,
                             String scope) {
        removeMetric(createName(klassnamescope));
    }

    
Removes the metric with the given name.

Parameters:
name the name of the metric
    public void removeMetric(MetricName name) {
        final Metric metric = .remove(name);
        if (metric != null) {
            if (metric instanceof Stoppable) {
                ((Stoppablemetric).stop();
            }
            notifyMetricRemoved(name);
        }
    }

    
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
    public void addListener(MetricsRegistryListener listener) {
        .add(listener);
        for (Map.Entry<MetricNameMetricentry : .entrySet()) {
            listener.onMetricAdded(entry.getKey(), entry.getValue());
        }
    }

    
Removes a MetricsRegistryListener from this registry's collection of listeners.

Parameters:
listener the listener that will be removed
    public void removeListener(MetricsRegistryListener listener) {
        .remove(listener);
    }

    
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
    protected MetricName createName(Class<?> klassString nameString scope) {
        return new MetricName(klassnamescope);
    }

    
Returns a new java.util.concurrent.ConcurrentMap implementation. Subclass this to do weird things with your own MetricsRegistry implementation.

    protected ConcurrentMap<MetricNameMetricnewMetricsMap() {
    }

    
Gets any existing metric with the given name or, if none exists, adds the given metric.

Parameters:
name the metric's name
metric the new metric
<T> the type of the metric
Returns:
either the existing metric or metric
    @SuppressWarnings("unchecked")
    protected final <T extends Metric> T getOrAdd(MetricName name, T metric) {
        final Metric existingMetric = .get(name);
        if (existingMetric == null) {
            final Metric justAddedMetric = .putIfAbsent(namemetric);
            if (justAddedMetric == null) {
                notifyMetricAdded(namemetric);
                return metric;
            }
            if (metric instanceof Stoppable) {
                ((Stoppablemetric).stop();
            }
            return (T) justAddedMetric;
        }
        return (T) existingMetric;
    }
        return .newScheduledThreadPool(2, "meter-tick");
    }
    private void notifyMetricRemoved(MetricName name) {
        for (MetricsRegistryListener listener : ) {
            listener.onMetricRemoved(name);
        }
    }
    private void notifyMetricAdded(MetricName nameMetric metric) {
        for (MetricsRegistryListener listener : ) {
            listener.onMetricAdded(namemetric);
        }
    }
New to GrepCode? Check out our FAQ X