Add metrics filter

master
Rémi Cocula 8 years ago
parent 153ba1adfa
commit 5cf079e72b
  1. 1
      libresonic-main/pom.xml
  2. 15
      libresonic-main/src/main/java/org/libresonic/player/boot/Application.java
  3. 46
      libresonic-main/src/main/java/org/libresonic/player/filter/MetricsFilter.java
  4. 1
      libresonic-main/src/main/resources/application.properties

@ -38,7 +38,6 @@
<groupId>io.dropwizard.metrics</groupId> <groupId>io.dropwizard.metrics</groupId>
<artifactId>metrics-core</artifactId> <artifactId>metrics-core</artifactId>
<version>${metrics.version}</version> <version>${metrics.version}</version>
<scope>test</scope>
</dependency> </dependency>
<!-- END Metrics --> <!-- END Metrics -->

@ -4,11 +4,7 @@ import javax.servlet.Filter;
import javax.servlet.ServletContextListener; import javax.servlet.ServletContextListener;
import net.sf.ehcache.constructs.web.ShutdownListener; import net.sf.ehcache.constructs.web.ShutdownListener;
import org.directwebremoting.servlet.DwrServlet; import org.directwebremoting.servlet.DwrServlet;
import org.libresonic.player.filter.BootstrapVerificationFilter; import org.libresonic.player.filter.*;
import org.libresonic.player.filter.ParameterDecodingFilter;
import org.libresonic.player.filter.RESTFilter;
import org.libresonic.player.filter.RequestEncodingFilter;
import org.libresonic.player.filter.ResponseHeaderFilter;
import org.libresonic.player.spring.AdditionalPropertySourceConfigurer; import org.libresonic.player.spring.AdditionalPropertySourceConfigurer;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jmx.JmxAutoConfiguration; import org.springframework.boot.autoconfigure.jmx.JmxAutoConfiguration;
@ -140,6 +136,15 @@ public class Application extends SpringBootServletInitializer {
return registration; return registration;
} }
@Bean
public FilterRegistrationBean metricsFilterRegistration() {
FilterRegistrationBean registration = new FilterRegistrationBean();
registration.setFilter(new MetricsFilter());
registration.setOrder(7);
return registration;
}
@Bean @Bean
public Filter noCacheFilter() { public Filter noCacheFilter() {
return new ResponseHeaderFilter(); return new ResponseHeaderFilter();

@ -0,0 +1,46 @@
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 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;
@Override
public void init(FilterConfig filterConfig) throws ServletException {
reporter = JmxReporter.forRegistry(metrics)
.convertRatesTo(TimeUnit.SECONDS.SECONDS)
.convertDurationsTo(TimeUnit.MILLISECONDS)
.build();
reporter.start();
}
@Override
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();
chain.doFilter(request, response);
requestTimerContext.stop();
}
@Override
public void destroy() {
reporter.stop();
}
}

@ -1,3 +1,4 @@
spring.mvc.view.prefix: /WEB-INF/jsp/ spring.mvc.view.prefix: /WEB-INF/jsp/
spring.mvc.view.suffix: .jsp spring.mvc.view.suffix: .jsp
server.error.includeStacktrace: ALWAYS server.error.includeStacktrace: ALWAYS
logging.level.org.springframework.web=DEBUG
Loading…
Cancel
Save