|
|
@ -10,10 +10,11 @@ import java.util.concurrent.TimeUnit; |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public class MetricsManager { |
|
|
|
public class MetricsManager { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Main metrics registry
|
|
|
|
private static final MetricRegistry metrics = new MetricRegistry(); |
|
|
|
private static final MetricRegistry metrics = new MetricRegistry(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Potential metrics reporters
|
|
|
|
private static JmxReporter reporter; |
|
|
|
private static JmxReporter reporter; |
|
|
|
private static final NullTimer nullTimerSingleton = new NullTimer(null); |
|
|
|
|
|
|
|
private static ConditionFalseTimerBuilder conditionFalseTimerBuilderSingleton = new ConditionFalseTimerBuilder(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static { |
|
|
|
static { |
|
|
|
reporter = JmxReporter.forRegistry(metrics) |
|
|
|
reporter = JmxReporter.forRegistry(metrics) |
|
|
@ -23,14 +24,36 @@ public class MetricsManager { |
|
|
|
reporter.start(); |
|
|
|
reporter.start(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* Creates a {@link Timer} whose name is based on a class name and a |
|
|
|
|
|
|
|
* qualified name. |
|
|
|
|
|
|
|
* @param clazz |
|
|
|
|
|
|
|
* @param name |
|
|
|
|
|
|
|
* @return |
|
|
|
|
|
|
|
*/ |
|
|
|
public static Timer timer(Class clazz, String name) { |
|
|
|
public static Timer timer(Class clazz, String name) { |
|
|
|
return new TimerBuilder().timer(clazz,name); |
|
|
|
return new TimerBuilder().timer(clazz,name); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* Creates a {@link Timer} whose name is based on an object's class name and a |
|
|
|
|
|
|
|
* qualified name. |
|
|
|
|
|
|
|
* @param ref |
|
|
|
|
|
|
|
* @param name |
|
|
|
|
|
|
|
* @return |
|
|
|
|
|
|
|
*/ |
|
|
|
public static Timer timer(Object ref, String name) { |
|
|
|
public static Timer timer(Object ref, String name) { |
|
|
|
return timer(ref.getClass(),name); |
|
|
|
return timer(ref.getClass(),name); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* Initiate a {@link TimerBuilder} using a condition. |
|
|
|
|
|
|
|
* If the condition is false, a void {@link Timer} will finally be built thus |
|
|
|
|
|
|
|
* no timer will be registered in the Metrics registry. |
|
|
|
|
|
|
|
* |
|
|
|
|
|
|
|
* @param ifTrue |
|
|
|
|
|
|
|
* @return |
|
|
|
|
|
|
|
*/ |
|
|
|
public static TimerBuilder condition(boolean ifTrue) { |
|
|
|
public static TimerBuilder condition(boolean ifTrue) { |
|
|
|
if (ifTrue == false) { |
|
|
|
if (ifTrue == false) { |
|
|
|
return conditionFalseTimerBuilderSingleton; |
|
|
|
return conditionFalseTimerBuilderSingleton; |
|
|
@ -38,19 +61,11 @@ public class MetricsManager { |
|
|
|
return new TimerBuilder(); |
|
|
|
return new TimerBuilder(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/* public interface TimerExecutor { |
|
|
|
/** |
|
|
|
void doWithTimer() throws Exception; |
|
|
|
* A class that builds a {@link Timer} |
|
|
|
} */ |
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
|
|
public static class TimerBuilder { |
|
|
|
public static class TimerBuilder { |
|
|
|
|
|
|
|
|
|
|
|
/* public TimerBuilder condition(boolean ifTrue) { |
|
|
|
|
|
|
|
if (ifTrue == false) { |
|
|
|
|
|
|
|
theTimer = nullTimerSingleton; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return this; |
|
|
|
|
|
|
|
} */ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public Timer timer(Class clazz, String name) { |
|
|
|
public Timer timer(Class clazz, String name) { |
|
|
|
com.codahale.metrics.Timer t = metrics.timer(MetricRegistry.name(clazz,name)); |
|
|
|
com.codahale.metrics.Timer t = metrics.timer(MetricRegistry.name(clazz,name)); |
|
|
|
com.codahale.metrics.Timer.Context tContext = t.time(); |
|
|
|
com.codahale.metrics.Timer.Context tContext = t.time(); |
|
|
@ -61,30 +76,11 @@ public class MetricsManager { |
|
|
|
return timer(ref.getClass(),name); |
|
|
|
return timer(ref.getClass(),name); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* public void exec(TimerExecutor executor) throws Exception { |
|
|
|
|
|
|
|
if (theTimer == null) { |
|
|
|
|
|
|
|
com.codahale.metrics.Timer t = metrics.timer(MetricRegistry.name(clazz, name)); |
|
|
|
|
|
|
|
com.codahale.metrics.Timer.Context tContext = t.time(); |
|
|
|
|
|
|
|
theTimer = new Timer(tContext); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
executor.doWithTimer(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
theTimer.close(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static class ConditionFalseTimerBuilder extends TimerBuilder { |
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
|
|
public Timer timer(Class clazz, String name) { |
|
|
|
|
|
|
|
return nullTimerSingleton; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* |
|
|
|
* A class that holds a Metrics timer context implementing {@link AutoCloseable} |
|
|
|
|
|
|
|
* thus it can be used in a try-with-resources statement. |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public static class Timer implements AutoCloseable { |
|
|
|
public static class Timer implements AutoCloseable { |
|
|
|
|
|
|
|
|
|
|
@ -101,6 +97,13 @@ public class MetricsManager { |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------
|
|
|
|
|
|
|
|
// Convenient singletons to avoid creating useless objects instances
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------
|
|
|
|
|
|
|
|
private static final NullTimer nullTimerSingleton = new NullTimer(null); |
|
|
|
|
|
|
|
private static final ConditionFalseTimerBuilder conditionFalseTimerBuilderSingleton = new ConditionFalseTimerBuilder(); |
|
|
|
|
|
|
|
|
|
|
|
private static class NullTimer extends Timer { |
|
|
|
private static class NullTimer extends Timer { |
|
|
|
|
|
|
|
|
|
|
|
protected NullTimer(com.codahale.metrics.Timer.Context timerContext) { |
|
|
|
protected NullTimer(com.codahale.metrics.Timer.Context timerContext) { |
|
|
@ -112,4 +115,12 @@ public class MetricsManager { |
|
|
|
// Does nothing
|
|
|
|
// Does nothing
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static class ConditionFalseTimerBuilder extends TimerBuilder { |
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
|
|
public Timer timer(Class clazz, String name) { |
|
|
|
|
|
|
|
return nullTimerSingleton; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|