From 5cf079e72bc49ebbdd4e430f0765a068f9cfe5c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Cocula?= Date: Thu, 12 Jan 2017 21:53:52 +0100 Subject: [PATCH] Add metrics filter --- libresonic-main/pom.xml | 1 - .../libresonic/player/boot/Application.java | 15 ++++-- .../player/filter/MetricsFilter.java | 46 +++++++++++++++++++ .../src/main/resources/application.properties | 1 + 4 files changed, 57 insertions(+), 6 deletions(-) create mode 100644 libresonic-main/src/main/java/org/libresonic/player/filter/MetricsFilter.java diff --git a/libresonic-main/pom.xml b/libresonic-main/pom.xml index ac1fe759..53820f2c 100644 --- a/libresonic-main/pom.xml +++ b/libresonic-main/pom.xml @@ -38,7 +38,6 @@ io.dropwizard.metrics metrics-core ${metrics.version} - test 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 fa8cfe93..6a396c17 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 @@ -4,11 +4,7 @@ import javax.servlet.Filter; import javax.servlet.ServletContextListener; import net.sf.ehcache.constructs.web.ShutdownListener; import org.directwebremoting.servlet.DwrServlet; -import org.libresonic.player.filter.BootstrapVerificationFilter; -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.filter.*; import org.libresonic.player.spring.AdditionalPropertySourceConfigurer; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.jmx.JmxAutoConfiguration; @@ -140,6 +136,15 @@ public class Application extends SpringBootServletInitializer { return registration; } + @Bean + public FilterRegistrationBean metricsFilterRegistration() { + FilterRegistrationBean registration = new FilterRegistrationBean(); + registration.setFilter(new MetricsFilter()); + registration.setOrder(7); + return registration; + } + + @Bean public Filter noCacheFilter() { return new ResponseHeaderFilter(); 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 new file mode 100644 index 00000000..eb58b7bb --- /dev/null +++ b/libresonic-main/src/main/java/org/libresonic/player/filter/MetricsFilter.java @@ -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(); + } +} diff --git a/libresonic-main/src/main/resources/application.properties b/libresonic-main/src/main/resources/application.properties index d8a35e3b..52c2cb5c 100644 --- a/libresonic-main/src/main/resources/application.properties +++ b/libresonic-main/src/main/resources/application.properties @@ -1,3 +1,4 @@ spring.mvc.view.prefix: /WEB-INF/jsp/ spring.mvc.view.suffix: .jsp server.error.includeStacktrace: ALWAYS +logging.level.org.springframework.web=DEBUG \ No newline at end of file