MetricsManager is now configurable.

master
Rémi Cocula 8 years ago
parent 9a64777ede
commit 88e820d8d1
  1. 7
      libresonic-main/src/main/java/org/libresonic/player/boot/Application.java
  2. 16
      libresonic-main/src/main/java/org/libresonic/player/filter/MetricsFilter.java
  3. 67
      libresonic-main/src/main/java/org/libresonic/player/monitor/MetricsManager.java
  4. 4
      libresonic-main/src/main/resources/applicationContext-service.xml

@ -146,10 +146,15 @@ public class Application extends SpringBootServletInitializer {
return registration; return registration;
} }
@Bean
public Filter metricsFilter() {
return new MetricsFilter();
}
@Bean @Bean
public FilterRegistrationBean metricsFilterRegistration() { public FilterRegistrationBean metricsFilterRegistration() {
FilterRegistrationBean registration = new FilterRegistrationBean(); FilterRegistrationBean registration = new FilterRegistrationBean();
registration.setFilter(new MetricsFilter()); registration.setFilter(metricsFilter());
registration.setOrder(7); registration.setOrder(7);
return registration; return registration;
} }

@ -1,30 +1,23 @@
package org.libresonic.player.filter; package org.libresonic.player.filter;
import com.codahale.metrics.JmxReporter;
import com.codahale.metrics.MetricRegistry;
import org.libresonic.player.monitor.MetricsManager; import org.libresonic.player.monitor.MetricsManager;
import org.springframework.beans.factory.annotation.Autowired;
import javax.servlet.*; import javax.servlet.*;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import java.io.IOException; import java.io.IOException;
import java.util.concurrent.TimeUnit;
/** /**
* Created by remi on 12/01/17. * Created by remi on 12/01/17.
*/ */
public class MetricsFilter implements Filter { public class MetricsFilter implements Filter {
private final MetricRegistry metrics = new MetricRegistry(); @Autowired
JmxReporter reporter; private MetricsManager metricsManager;
@Override @Override
public void init(FilterConfig filterConfig) throws ServletException { public void init(FilterConfig filterConfig) throws ServletException {
reporter = JmxReporter.forRegistry(metrics)
.convertRatesTo(TimeUnit.SECONDS.SECONDS)
.convertDurationsTo(TimeUnit.MILLISECONDS)
.build();
reporter.start();
} }
@Override @Override
@ -32,13 +25,12 @@ public class MetricsFilter implements Filter {
HttpServletRequest httpServletRequest = (HttpServletRequest)request; HttpServletRequest httpServletRequest = (HttpServletRequest)request;
String timerName = httpServletRequest.getRequestURI(); 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); chain.doFilter(request, response);
} }
} }
@Override @Override
public void destroy() { public void destroy() {
reporter.stop();
} }
} }

@ -2,6 +2,7 @@ package org.libresonic.player.monitor;
import com.codahale.metrics.JmxReporter; import com.codahale.metrics.JmxReporter;
import com.codahale.metrics.MetricRegistry; import com.codahale.metrics.MetricRegistry;
import org.libresonic.player.service.ApacheCommonsConfigurationService;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
@ -10,18 +11,41 @@ import java.util.concurrent.TimeUnit;
*/ */
public class MetricsManager { public class MetricsManager {
private ApacheCommonsConfigurationService configurationService;
// Main metrics registry // Main metrics registry
private static final MetricRegistry metrics = new MetricRegistry(); private static final MetricRegistry metrics = new MetricRegistry();
private static Boolean metricsActivatedByConfiguration = null;
private static Object _lock = new Object();
// Potential metrics reporters // Potential metrics reporters
private static JmxReporter reporter; private static JmxReporter reporter;
static { private void configureMetricsActivation() {
reporter = JmxReporter.forRegistry(metrics) if (configurationService.containsKey("Metrics")) {
.convertRatesTo(TimeUnit.SECONDS.SECONDS) metricsActivatedByConfiguration = Boolean.TRUE;
.convertDurationsTo(TimeUnit.MILLISECONDS)
.build(); // Start a Metrics JMX reporter
reporter.start(); 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 * @param name
* @return * @return
*/ */
public static Timer timer(Class clazz, String name) { public Timer timer(Class clazz, String name) {
return new TimerBuilder().timer(clazz,name); if (metricsActivatedByConfiguration()) {
return new TimerBuilder().timer(clazz, name);
} else {
return nullTimerSingleton;
}
} }
/** /**
@ -42,7 +70,7 @@ public class MetricsManager {
* @param name * @param name
* @return * @return
*/ */
public static Timer timer(Object ref, String name) { public Timer timer(Object ref, String name) {
return timer(ref.getClass(),name); return timer(ref.getClass(),name);
} }
@ -54,11 +82,19 @@ public class MetricsManager {
* @param ifTrue * @param ifTrue
* @return * @return
*/ */
public static TimerBuilder condition(boolean ifTrue) { public TimerBuilder condition(boolean ifTrue) {
if (ifTrue == false) { if (metricsActivatedByConfiguration()) {
return conditionFalseTimerBuilderSingleton; 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 // Convenient singletons to avoid creating useless objects instances
// ----------------------------------------------------------------- // -----------------------------------------------------------------
private static final NullTimer nullTimerSingleton = new NullTimer(null); 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 { 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 @Override
public Timer timer(Class clazz, String name) { public Timer timer(Class clazz, String name) {
return nullTimerSingleton; return nullTimerSingleton;

@ -92,6 +92,10 @@
<bean id="configurationService" class="org.libresonic.player.service.ApacheCommonsConfigurationService" /> <bean id="configurationService" class="org.libresonic.player.service.ApacheCommonsConfigurationService" />
<bean id="metricsManager" class="org.libresonic.player.monitor.MetricsManager">
<property name="configurationService" ref="configurationService" />
</bean>
<bean id="settingsService" class="org.libresonic.player.service.SettingsService" init-method="init"> <bean id="settingsService" class="org.libresonic.player.service.SettingsService" init-method="init">
<property name="internetRadioDao" ref="internetRadioDao"/> <property name="internetRadioDao" ref="internetRadioDao"/>
<property name="musicFolderDao" ref="musicFolderDao"/> <property name="musicFolderDao" ref="musicFolderDao"/>

Loading…
Cancel
Save