From 88e820d8d13365e6def0a25f34d0ab06eee35f76 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Cocula?= Date: Wed, 25 Jan 2017 22:53:13 +0100 Subject: [PATCH] MetricsManager is now configurable. --- .../libresonic/player/boot/Application.java | 7 +- .../player/filter/MetricsFilter.java | 16 ++--- .../player/monitor/MetricsManager.java | 67 ++++++++++++++----- .../resources/applicationContext-service.xml | 4 ++ 4 files changed, 66 insertions(+), 28 deletions(-) diff --git a/libresonic-main/src/main/java/org/libresonic/player/boot/Application.java b/libresonic-main/src/main/java/org/libresonic/player/boot/Application.java index a5492d81..7e35eeb5 100644 --- a/libresonic-main/src/main/java/org/libresonic/player/boot/Application.java +++ b/libresonic-main/src/main/java/org/libresonic/player/boot/Application.java @@ -146,10 +146,15 @@ public class Application extends SpringBootServletInitializer { return registration; } + @Bean + public Filter metricsFilter() { + return new MetricsFilter(); + } + @Bean public FilterRegistrationBean metricsFilterRegistration() { FilterRegistrationBean registration = new FilterRegistrationBean(); - registration.setFilter(new MetricsFilter()); + registration.setFilter(metricsFilter()); registration.setOrder(7); return registration; } 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 ec88be54..528e6039 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,30 +1,23 @@ package org.libresonic.player.filter; -import com.codahale.metrics.JmxReporter; -import com.codahale.metrics.MetricRegistry; import org.libresonic.player.monitor.MetricsManager; +import org.springframework.beans.factory.annotation.Autowired; import javax.servlet.*; import javax.servlet.http.HttpServletRequest; import java.io.IOException; -import java.util.concurrent.TimeUnit; /** * Created by remi on 12/01/17. */ public class MetricsFilter implements Filter { - private final MetricRegistry metrics = new MetricRegistry(); - JmxReporter reporter; + @Autowired + private MetricsManager metricsManager; @Override public void init(FilterConfig filterConfig) throws ServletException { - reporter = JmxReporter.forRegistry(metrics) - .convertRatesTo(TimeUnit.SECONDS.SECONDS) - .convertDurationsTo(TimeUnit.MILLISECONDS) - .build(); - reporter.start(); } @Override @@ -32,13 +25,12 @@ public class MetricsFilter implements Filter { HttpServletRequest httpServletRequest = (HttpServletRequest)request; String timerName = httpServletRequest.getRequestURI(); - try (MetricsManager.Timer t = MetricsManager.condition(timerName.contains("main.view")).timer(this,timerName)) { + try (MetricsManager.Timer t = metricsManager.condition(timerName.contains("main.view")).timer(this,timerName)) { chain.doFilter(request, response); } } @Override public void destroy() { - reporter.stop(); } } 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 8f2d358f..31c5f8fb 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 @@ -2,6 +2,7 @@ package org.libresonic.player.monitor; import com.codahale.metrics.JmxReporter; import com.codahale.metrics.MetricRegistry; +import org.libresonic.player.service.ApacheCommonsConfigurationService; import java.util.concurrent.TimeUnit; @@ -10,18 +11,41 @@ import java.util.concurrent.TimeUnit; */ public class MetricsManager { + private ApacheCommonsConfigurationService configurationService; + // Main metrics registry private static final MetricRegistry metrics = new MetricRegistry(); + private static Boolean metricsActivatedByConfiguration = null; + private static Object _lock = new Object(); + // Potential metrics reporters private static JmxReporter reporter; - static { - reporter = JmxReporter.forRegistry(metrics) - .convertRatesTo(TimeUnit.SECONDS.SECONDS) - .convertDurationsTo(TimeUnit.MILLISECONDS) - .build(); - reporter.start(); + private void configureMetricsActivation() { + if (configurationService.containsKey("Metrics")) { + metricsActivatedByConfiguration = Boolean.TRUE; + + // Start a Metrics JMX reporter + reporter = JmxReporter.forRegistry(metrics) + .convertRatesTo(TimeUnit.SECONDS.SECONDS) + .convertDurationsTo(TimeUnit.MILLISECONDS) + .build(); + reporter.start(); + } else { + metricsActivatedByConfiguration = Boolean.FALSE; + } + } + + private boolean metricsActivatedByConfiguration() { + if (metricsActivatedByConfiguration == null) { + synchronized (_lock) { + if (metricsActivatedByConfiguration == null) { + configureMetricsActivation(); + } + } + } + return metricsActivatedByConfiguration.booleanValue(); } /** @@ -31,8 +55,12 @@ public class MetricsManager { * @param name * @return */ - public static Timer timer(Class clazz, String name) { - return new TimerBuilder().timer(clazz,name); + public Timer timer(Class clazz, String name) { + if (metricsActivatedByConfiguration()) { + return new TimerBuilder().timer(clazz, name); + } else { + return nullTimerSingleton; + } } /** @@ -42,7 +70,7 @@ public class MetricsManager { * @param name * @return */ - public static Timer timer(Object ref, String name) { + public Timer timer(Object ref, String name) { return timer(ref.getClass(),name); } @@ -54,11 +82,19 @@ public class MetricsManager { * @param ifTrue * @return */ - public static TimerBuilder condition(boolean ifTrue) { - if (ifTrue == false) { - return conditionFalseTimerBuilderSingleton; + public TimerBuilder condition(boolean ifTrue) { + if (metricsActivatedByConfiguration()) { + if (ifTrue == false) { + return conditionFalseTimerBuilderSingleton; + } + return new TimerBuilder(); + } else { + return nullTimerBuilderSingleton; } - return new TimerBuilder(); + } + + public void setConfigurationService(ApacheCommonsConfigurationService configurationService) { + this.configurationService = configurationService; } /** @@ -102,7 +138,8 @@ 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 final NullTimerBuilder conditionFalseTimerBuilderSingleton = new NullTimerBuilder(); + private static final NullTimerBuilder nullTimerBuilderSingleton = new NullTimerBuilder(); private static class NullTimer extends Timer { @@ -116,7 +153,7 @@ public class MetricsManager { } } - private static class ConditionFalseTimerBuilder extends TimerBuilder { + private static class NullTimerBuilder extends TimerBuilder { @Override public Timer timer(Class clazz, String name) { return nullTimerSingleton; diff --git a/libresonic-main/src/main/resources/applicationContext-service.xml b/libresonic-main/src/main/resources/applicationContext-service.xml index a966a9cf..300bbd52 100644 --- a/libresonic-main/src/main/resources/applicationContext-service.xml +++ b/libresonic-main/src/main/resources/applicationContext-service.xml @@ -92,6 +92,10 @@ + + + +