From b8b511e1912e60163b4c3d73edad4a56ab74e512 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Cocula?= Date: Tue, 17 Jan 2017 23:30:54 +0100 Subject: [PATCH] WIP metrics --- .../player/filter/MetricsFilter.java | 15 ++- .../player/monitor/MetricsManager.java | 119 ++++++++++++++++++ 2 files changed, 128 insertions(+), 6 deletions(-) create mode 100644 libresonic-main/src/main/java/org/libresonic/player/monitor/MetricsManager.java 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 eb58b7bb..f3f87d74 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 @@ -1,12 +1,12 @@ package org.libresonic.player.filter; -import com.codahale.metrics.ConsoleReporter; import com.codahale.metrics.JmxReporter; import com.codahale.metrics.MetricRegistry; -import com.codahale.metrics.Timer; +import org.libresonic.player.monitor.MetricsManager; import javax.servlet.*; import javax.servlet.http.HttpServletRequest; + import java.io.IOException; import java.util.concurrent.TimeUnit; @@ -31,12 +31,15 @@ public class MetricsFilter implements Filter { public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { HttpServletRequest httpServletRequest = (HttpServletRequest)request; - Timer requestTimer = metrics.timer(MetricRegistry.name(MetricsFilter.class, httpServletRequest.getRequestURI())); - Timer.Context requestTimerContext = requestTimer.time(); + String timerName = httpServletRequest.getRequestURI(); + try (MetricsManager.Timer t = MetricsManager.buildTimer(this,timerName).condition(timerName.contains("main.view")).timer()) { + chain.doFilter(request, response); + } - chain.doFilter(request, response); - requestTimerContext.stop(); + /* 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 new file mode 100644 index 00000000..86512022 --- /dev/null +++ b/libresonic-main/src/main/java/org/libresonic/player/monitor/MetricsManager.java @@ -0,0 +1,119 @@ +package org.libresonic.player.monitor; + +import com.codahale.metrics.JmxReporter; +import com.codahale.metrics.MetricRegistry; + +import java.util.concurrent.TimeUnit; + +/** + * Created by remi on 17/01/17. + */ +public class MetricsManager { + + private static final MetricRegistry metrics = new MetricRegistry(); + private static JmxReporter reporter; + private static final NullTimer nullTimerSingleton = new NullTimer(null); + + static { + reporter = JmxReporter.forRegistry(metrics) + .convertRatesTo(TimeUnit.SECONDS.SECONDS) + .convertDurationsTo(TimeUnit.MILLISECONDS) + .build(); + reporter.start(); + } + + + public static TimerBuilder buildTimer(Class clazz, String name) { + return new TimerBuilder(clazz,name); + } + + public static TimerBuilder buildTimer(Object ref, String name) { + return new TimerBuilder(ref.getClass(),name); + } + + 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) { + if (ifTrue == false) { + theTimer = nullTimerSingleton; + } + return this; + } + + 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 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(); + } + } + + /** + * + */ + public static class Timer implements AutoCloseable { + + private com.codahale.metrics.Timer.Context timerContext; + + protected Timer(com.codahale.metrics.Timer.Context timerContext) { + this.timerContext = timerContext; + } + + @Override + public void close() { + timerContext.stop(); + } + + public Timer condition() { + return null; + } + } + + private static class NullTimer extends Timer { + + protected NullTimer(com.codahale.metrics.Timer.Context timerContext) { + super(timerContext); + } + + @Override + public void close() { + // Does nothing + } + } + + +}