From 1f9d11c5a58dc69695184ca25f104813e51a347d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Cocula?= Date: Wed, 18 Jan 2017 00:32:31 +0100 Subject: [PATCH] Code cleanup and comment. --- .../player/filter/MetricsFilter.java | 5 -- .../player/monitor/MetricsManager.java | 79 +++++++++++-------- 2 files changed, 45 insertions(+), 39 deletions(-) diff --git a/libresonic-main/src/main/java/org/libresonic/player/filter/MetricsFilter.java b/libresonic-main/src/main/java/org/libresonic/player/filter/MetricsFilter.java index 59539ff2..ec88be54 100644 --- a/libresonic-main/src/main/java/org/libresonic/player/filter/MetricsFilter.java +++ b/libresonic-main/src/main/java/org/libresonic/player/filter/MetricsFilter.java @@ -29,17 +29,12 @@ public class MetricsFilter implements Filter { @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { - HttpServletRequest httpServletRequest = (HttpServletRequest)request; String timerName = httpServletRequest.getRequestURI(); try (MetricsManager.Timer t = MetricsManager.condition(timerName.contains("main.view")).timer(this,timerName)) { chain.doFilter(request, response); } - - /* MetricsManager.buildTimer(MetricsFilter.class,timerName).condition(timerName.contains("main.view")).exec(() -> { - chain.doFilter(request,response); - }); */ } @Override diff --git a/libresonic-main/src/main/java/org/libresonic/player/monitor/MetricsManager.java b/libresonic-main/src/main/java/org/libresonic/player/monitor/MetricsManager.java index d5632a55..8f2d358f 100644 --- a/libresonic-main/src/main/java/org/libresonic/player/monitor/MetricsManager.java +++ b/libresonic-main/src/main/java/org/libresonic/player/monitor/MetricsManager.java @@ -10,10 +10,11 @@ import java.util.concurrent.TimeUnit; */ public class MetricsManager { + // Main metrics registry private static final MetricRegistry metrics = new MetricRegistry(); + + // Potential metrics reporters private static JmxReporter reporter; - private static final NullTimer nullTimerSingleton = new NullTimer(null); - private static ConditionFalseTimerBuilder conditionFalseTimerBuilderSingleton = new ConditionFalseTimerBuilder(); static { reporter = JmxReporter.forRegistry(metrics) @@ -23,14 +24,36 @@ public class MetricsManager { 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) { 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) { 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) { if (ifTrue == false) { return conditionFalseTimerBuilderSingleton; @@ -38,19 +61,11 @@ public class MetricsManager { return new TimerBuilder(); } - /* public interface TimerExecutor { - void doWithTimer() throws Exception; - } */ - + /** + * A class that builds a {@link Timer} + */ public static class TimerBuilder { - /* public TimerBuilder condition(boolean ifTrue) { - if (ifTrue == false) { - theTimer = nullTimerSingleton; - } - return this; - } */ - public Timer timer(Class clazz, String name) { com.codahale.metrics.Timer t = metrics.timer(MetricRegistry.name(clazz,name)); com.codahale.metrics.Timer.Context tContext = t.time(); @@ -61,30 +76,11 @@ public class MetricsManager { 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 { @@ -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 { protected NullTimer(com.codahale.metrics.Timer.Context timerContext) { @@ -112,4 +115,12 @@ public class MetricsManager { // Does nothing } } + + private static class ConditionFalseTimerBuilder extends TimerBuilder { + @Override + public Timer timer(Class clazz, String name) { + return nullTimerSingleton; + } + } + }