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. 49
      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;
}
@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;
}

@ -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();
}
}

@ -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 {
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) {
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) {
public TimerBuilder condition(boolean ifTrue) {
if (metricsActivatedByConfiguration()) {
if (ifTrue == false) {
return conditionFalseTimerBuilderSingleton;
}
return new TimerBuilder();
} else {
return nullTimerBuilderSingleton;
}
}
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;

@ -92,6 +92,10 @@
<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">
<property name="internetRadioDao" ref="internetRadioDao"/>
<property name="musicFolderDao" ref="musicFolderDao"/>

Loading…
Cancel
Save