From 844b5de4270a98337552a63645caf6082de134a1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Cocula?= Date: Wed, 18 Jan 2017 00:07:32 +0100 Subject: [PATCH] WIP metrics --- .../player/filter/MetricsFilter.java | 2 +- .../player/monitor/MetricsManager.java | 68 +++++++++---------- 2 files changed, 33 insertions(+), 37 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 f3f87d74..59539ff2 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 @@ -33,7 +33,7 @@ public class MetricsFilter implements Filter { HttpServletRequest httpServletRequest = (HttpServletRequest)request; String timerName = httpServletRequest.getRequestURI(); - try (MetricsManager.Timer t = MetricsManager.buildTimer(this,timerName).condition(timerName.contains("main.view")).timer()) { + try (MetricsManager.Timer t = MetricsManager.condition(timerName.contains("main.view")).timer(this,timerName)) { chain.doFilter(request, response); } 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 86512022..d5632a55 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 @@ -13,6 +13,7 @@ public class MetricsManager { private static final MetricRegistry metrics = new MetricRegistry(); private static JmxReporter reporter; private static final NullTimer nullTimerSingleton = new NullTimer(null); + private static ConditionFalseTimerBuilder conditionFalseTimerBuilderSingleton = new ConditionFalseTimerBuilder(); static { reporter = JmxReporter.forRegistry(metrics) @@ -22,54 +23,46 @@ public class MetricsManager { reporter.start(); } - - public static TimerBuilder buildTimer(Class clazz, String name) { - return new TimerBuilder(clazz,name); + public static Timer timer(Class clazz, String name) { + return new TimerBuilder().timer(clazz,name); } - public static TimerBuilder buildTimer(Object ref, String name) { - return new TimerBuilder(ref.getClass(),name); + public static Timer timer(Object ref, String name) { + return timer(ref.getClass(),name); } - public interface TimerExecutor { - void doWithTimer() throws Exception; + public static TimerBuilder condition(boolean ifTrue) { + if (ifTrue == false) { + return conditionFalseTimerBuilderSingleton; + } + return new TimerBuilder(); } + /* public interface TimerExecutor { + void doWithTimer() throws Exception; + } */ public static class TimerBuilder { - private Timer theTimer; - private Class clazz; - private String name; - - public TimerBuilder() { - } - public TimerBuilder(Timer theTimer) { - this.theTimer = theTimer; - } - - public TimerBuilder(Class clazz, String name) { - this.clazz = clazz; - this.name = name; - } - - public TimerBuilder condition(boolean ifTrue) { + /* 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(); + return new Timer(tContext); } - public Timer timer() { - 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); - } - return theTimer; + public Timer timer(Object ref, String name) { + return timer(ref.getClass(),name); } - public void exec(TimerExecutor executor) throws Exception { + + /* 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(); @@ -80,6 +73,14 @@ public class MetricsManager { theTimer.close(); } + */ + } + + private static class ConditionFalseTimerBuilder extends TimerBuilder { + @Override + public Timer timer(Class clazz, String name) { + return nullTimerSingleton; + } } /** @@ -98,9 +99,6 @@ public class MetricsManager { timerContext.stop(); } - public Timer condition() { - return null; - } } private static class NullTimer extends Timer { @@ -114,6 +112,4 @@ public class MetricsManager { // Does nothing } } - - }