Clean up the APIMetricsRegistry

This is a clean up in the APIMetricsRegistry. It replaces snake case to
camel case, and perform a better formatting of the code.

Signed-off-by: Amnon Heiman <amnon@cloudius-systems.com>
This commit is contained in:
Amnon Heiman 2015-07-14 17:57:04 +03:00
parent 2d97995229
commit 479a0769d1

View File

@ -23,18 +23,19 @@ import com.yammer.metrics.core.Histogram.SampleType;
*/ */
public class APIMetricsRegistry extends MetricsRegistry { public class APIMetricsRegistry extends MetricsRegistry {
Field field_metrics; Field fieldMetrics;
Field field_clock; Field fieldClock;
Field field_thread_pool; Field fieldThreadPool;
public APIMetricsRegistry() { public APIMetricsRegistry() {
try { try {
field_metrics = MetricsRegistry.class.getDeclaredField("metrics"); fieldMetrics = MetricsRegistry.class.getDeclaredField("metrics");
field_metrics.setAccessible(true); fieldMetrics.setAccessible(true);
field_clock = MetricsRegistry.class.getDeclaredField("clock"); fieldClock = MetricsRegistry.class.getDeclaredField("clock");
field_clock.setAccessible(true); fieldClock.setAccessible(true);
field_thread_pool = MetricsRegistry.class.getDeclaredField("threadPools"); fieldThreadPool = MetricsRegistry.class
field_thread_pool.setAccessible(true); .getDeclaredField("threadPools");
fieldThreadPool.setAccessible(true);
} catch (NoSuchFieldException | SecurityException e) { } catch (NoSuchFieldException | SecurityException e) {
// TODO Auto-generated catch block // TODO Auto-generated catch block
e.printStackTrace(); e.printStackTrace();
@ -43,7 +44,7 @@ public class APIMetricsRegistry extends MetricsRegistry {
public ThreadPools getThreadPools() { public ThreadPools getThreadPools() {
try { try {
return (ThreadPools)field_thread_pool.get(this); return (ThreadPools) fieldThreadPool.get(this);
} catch (IllegalArgumentException | IllegalAccessException e) { } catch (IllegalArgumentException | IllegalAccessException e) {
e.printStackTrace(); e.printStackTrace();
} }
@ -52,7 +53,7 @@ public class APIMetricsRegistry extends MetricsRegistry {
public Clock getClock() { public Clock getClock() {
try { try {
return (Clock)field_clock.get(this); return (Clock) fieldClock.get(this);
} catch (IllegalArgumentException | IllegalAccessException e) { } catch (IllegalArgumentException | IllegalAccessException e) {
e.printStackTrace(); e.printStackTrace();
} }
@ -60,43 +61,52 @@ public class APIMetricsRegistry extends MetricsRegistry {
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public ConcurrentMap<MetricName, Metric> get_metrics() { public ConcurrentMap<MetricName, Metric> getMetrics() {
try { try {
return (ConcurrentMap<MetricName, Metric>)field_metrics.get(this); return (ConcurrentMap<MetricName, Metric>) fieldMetrics.get(this);
} catch (IllegalArgumentException | IllegalAccessException e) { } catch (IllegalArgumentException | IllegalAccessException e) {
e.printStackTrace(); e.printStackTrace();
} }
return null; return null;
} }
/** /**
* Creates a new {@link Counter} and registers it under the given class and name. * Creates a new {@link Counter} and registers it under the given class and
* name.
* *
* @param klass the class which owns the metric * @param klass
* @param name the name of the metric * the class which owns the metric
* @param name
* the name of the metric
* @return a new {@link Counter} * @return a new {@link Counter}
*/ */
public Counter newCounter(String url, Class<?> klass, public Counter newCounter(String url, Class<?> klass, String name) {
String name) {
return newCounter(url, klass, name, null); return newCounter(url, klass, name, null);
} }
/** /**
* Creates a new {@link Counter} and registers it under the given class and name. * Creates a new {@link Counter} and registers it under the given class and
* name.
* *
* @param klass the class which owns the metric * @param klass
* @param name the name of the metric * the class which owns the metric
* @param scope the scope of the metric * @param name
* the name of the metric
* @param scope
* the scope of the metric
* @return a new {@link Counter} * @return a new {@link Counter}
*/ */
public Counter newCounter(String url, Class<?> klass, public Counter newCounter(String url, Class<?> klass, String name,
String name, String scope) {
String scope) {
return newCounter(url, createName(klass, name, scope)); return newCounter(url, createName(klass, name, scope));
} }
/** /**
* Creates a new {@link Counter} and registers it under the given metric name. * Creates a new {@link Counter} and registers it under the given metric
* name.
* *
* @param metricName the name of the metric * @param metricName
* the name of the metric
* @return a new {@link Counter} * @return a new {@link Counter}
*/ */
public Counter newCounter(String url, MetricName metricName) { public Counter newCounter(String url, MetricName metricName) {
@ -104,131 +114,153 @@ public class APIMetricsRegistry extends MetricsRegistry {
} }
/** /**
* Creates a new {@link Meter} and registers it under the given class and name. * Creates a new {@link Meter} and registers it under the given class and
* name.
* *
* @param klass the class which owns the metric * @param klass
* @param name the name of the metric * the class which owns the metric
* @param eventType the plural name of the type of events the meter is measuring (e.g., {@code * @param name
* "requests"}) * the name of the metric
* @param unit the rate unit of the new meter * @param eventType
* the plural name of the type of events the meter is measuring
* (e.g., {@code "requests"})
* @param unit
* the rate unit of the new meter
* @return a new {@link Meter} * @return a new {@link Meter}
*/ */
public Meter newMeter(String url, Class<?> klass, public Meter newMeter(String url, Class<?> klass, String name,
String name, String eventType, TimeUnit unit) {
String eventType,
TimeUnit unit) {
return newMeter(url, klass, name, null, eventType, unit); return newMeter(url, klass, name, null, eventType, unit);
} }
/** /**
* Creates a new {@link Meter} and registers it under the given class, name, and scope. * Creates a new {@link Meter} and registers it under the given class, name,
* and scope.
* *
* @param klass the class which owns the metric * @param klass
* @param name the name of the metric * the class which owns the metric
* @param scope the scope of the metric * @param name
* @param eventType the plural name of the type of events the meter is measuring (e.g., {@code * the name of the metric
* "requests"}) * @param scope
* @param unit the rate unit of the new meter * the scope of the metric
* @param eventType
* the plural name of the type of events the meter is measuring
* (e.g., {@code "requests"})
* @param unit
* the rate unit of the new meter
* @return a new {@link Meter} * @return a new {@link Meter}
*/ */
public Meter newMeter(String url, public Meter newMeter(String url, Class<?> klass, String name,
Class<?> klass, String scope, String eventType, TimeUnit unit) {
String name,
String scope,
String eventType,
TimeUnit unit) {
return newMeter(url, createName(klass, name, scope), eventType, unit); return newMeter(url, createName(klass, name, scope), eventType, unit);
} }
private ScheduledExecutorService newMeterTickThreadPool() { private ScheduledExecutorService newMeterTickThreadPool() {
return getThreadPools().newScheduledThreadPool(2, "meter-tick"); return getThreadPools().newScheduledThreadPool(2, "meter-tick");
} }
/** /**
* Creates a new {@link Meter} and registers it under the given metric name. * Creates a new {@link Meter} and registers it under the given metric name.
* *
* @param metricName the name of the metric * @param metricName
* @param eventType the plural name of the type of events the meter is measuring (e.g., {@code * the name of the metric
* "requests"}) * @param eventType
* @param unit the rate unit of the new meter * the plural name of the type of events the meter is measuring
* (e.g., {@code "requests"})
* @param unit
* the rate unit of the new meter
* @return a new {@link Meter} * @return a new {@link Meter}
*/ */
public Meter newMeter(String url, MetricName metricName, public Meter newMeter(String url, MetricName metricName, String eventType,
String eventType, TimeUnit unit) {
TimeUnit unit) { final Metric existingMetric = getMetrics().get(metricName);
final Metric existingMetric = get_metrics().get(metricName);
if (existingMetric != null) { if (existingMetric != null) {
return (Meter) existingMetric; return (Meter) existingMetric;
} }
return getOrAdd(metricName, new APIMeter(url, newMeterTickThreadPool(), eventType, unit, getClock())); return getOrAdd(metricName, new APIMeter(url, newMeterTickThreadPool(),
eventType, unit, getClock()));
} }
/** /**
* Creates a new {@link Histogram} and registers it under the given class and name. * Creates a new {@link Histogram} and registers it under the given class
* and name.
* *
* @param klass the class which owns the metric * @param klass
* @param name the name of the metric * the class which owns the metric
* @param biased whether or not the histogram should be biased * @param name
* the name of the metric
* @param biased
* whether or not the histogram should be biased
* @return a new {@link Histogram} * @return a new {@link Histogram}
*/ */
public Histogram newHistogram(String url, Class<?> klass, public Histogram newHistogram(String url, Class<?> klass, String name,
String name, boolean biased) {
boolean biased) {
return newHistogram(url, klass, name, null, biased); return newHistogram(url, klass, name, null, biased);
} }
/** /**
* Creates a new {@link Histogram} and registers it under the given class, name, and scope. * Creates a new {@link Histogram} and registers it under the given class,
* name, and scope.
* *
* @param klass the class which owns the metric * @param klass
* @param name the name of the metric * the class which owns the metric
* @param scope the scope of the metric * @param name
* @param biased whether or not the histogram should be biased * the name of the metric
* @param scope
* the scope of the metric
* @param biased
* whether or not the histogram should be biased
* @return a new {@link Histogram} * @return a new {@link Histogram}
*/ */
public Histogram newHistogram(String url, Class<?> klass, public Histogram newHistogram(String url, Class<?> klass, String name,
String name, String scope, boolean biased) {
String scope,
boolean biased) {
return newHistogram(url, createName(klass, name, scope), biased); return newHistogram(url, createName(klass, name, scope), biased);
} }
/** /**
* Creates a new non-biased {@link Histogram} and registers it under the given class and name. * Creates a new non-biased {@link Histogram} and registers it under the
* given class and name.
* *
* @param klass the class which owns the metric * @param klass
* @param name the name of the metric * the class which owns the metric
* @param name
* the name of the metric
* @return a new {@link Histogram} * @return a new {@link Histogram}
*/ */
public Histogram newHistogram(String url, Class<?> klass, public Histogram newHistogram(String url, Class<?> klass, String name) {
String name) {
return newHistogram(url, klass, name, false); return newHistogram(url, klass, name, false);
} }
/** /**
* Creates a new non-biased {@link Histogram} and registers it under the given class, name, and * Creates a new non-biased {@link Histogram} and registers it under the
* scope. * given class, name, and scope.
* *
* @param klass the class which owns the metric * @param klass
* @param name the name of the metric * the class which owns the metric
* @param scope the scope of the metric * @param name
* the name of the metric
* @param scope
* the scope of the metric
* @return a new {@link Histogram} * @return a new {@link Histogram}
*/ */
public Histogram newHistogram(String url, Class<?> klass, public Histogram newHistogram(String url, Class<?> klass, String name,
String name, String scope) {
String scope) {
return newHistogram(url, klass, name, scope, false); return newHistogram(url, klass, name, scope, false);
} }
/** /**
* Creates a new {@link Histogram} and registers it under the given metric name. * Creates a new {@link Histogram} and registers it under the given metric
* name.
* *
* @param metricName the name of the metric * @param metricName
* @param biased whether or not the histogram should be biased * the name of the metric
* @param biased
* whether or not the histogram should be biased
* @return a new {@link Histogram} * @return a new {@link Histogram}
*/ */
public Histogram newHistogram(String url, MetricName metricName, public Histogram newHistogram(String url, MetricName metricName,
boolean biased) { boolean biased) {
return getOrAdd(metricName, return getOrAdd(metricName, new APIHistogram(url,
new APIHistogram(url, biased ? SampleType.BIASED : SampleType.UNIFORM)); biased ? SampleType.BIASED : SampleType.UNIFORM));
} }
} }