WIP metrics

master
Rémi Cocula 8 years ago
parent 1b79f3ce62
commit b8b511e191
  1. 15
      libresonic-main/src/main/java/org/libresonic/player/filter/MetricsFilter.java
  2. 119
      libresonic-main/src/main/java/org/libresonic/player/monitor/MetricsManager.java

@ -1,12 +1,12 @@
package org.libresonic.player.filter; package org.libresonic.player.filter;
import com.codahale.metrics.ConsoleReporter;
import com.codahale.metrics.JmxReporter; import com.codahale.metrics.JmxReporter;
import com.codahale.metrics.MetricRegistry; import com.codahale.metrics.MetricRegistry;
import com.codahale.metrics.Timer; import org.libresonic.player.monitor.MetricsManager;
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; import java.util.concurrent.TimeUnit;
@ -31,12 +31,15 @@ public class MetricsFilter implements Filter {
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
HttpServletRequest httpServletRequest = (HttpServletRequest)request; HttpServletRequest httpServletRequest = (HttpServletRequest)request;
Timer requestTimer = metrics.timer(MetricRegistry.name(MetricsFilter.class, httpServletRequest.getRequestURI()));
Timer.Context requestTimerContext = requestTimer.time(); String timerName = httpServletRequest.getRequestURI();
try (MetricsManager.Timer t = MetricsManager.buildTimer(this,timerName).condition(timerName.contains("main.view")).timer()) {
chain.doFilter(request, response);
}
chain.doFilter(request, response); /* MetricsManager.buildTimer(MetricsFilter.class,timerName).condition(timerName.contains("main.view")).exec(() -> {
requestTimerContext.stop(); chain.doFilter(request,response);
}); */
} }
@Override @Override

@ -0,0 +1,119 @@
package org.libresonic.player.monitor;
import com.codahale.metrics.JmxReporter;
import com.codahale.metrics.MetricRegistry;
import java.util.concurrent.TimeUnit;
/**
* Created by remi on 17/01/17.
*/
public class MetricsManager {
private static final MetricRegistry metrics = new MetricRegistry();
private static JmxReporter reporter;
private static final NullTimer nullTimerSingleton = new NullTimer(null);
static {
reporter = JmxReporter.forRegistry(metrics)
.convertRatesTo(TimeUnit.SECONDS.SECONDS)
.convertDurationsTo(TimeUnit.MILLISECONDS)
.build();
reporter.start();
}
public static TimerBuilder buildTimer(Class clazz, String name) {
return new TimerBuilder(clazz,name);
}
public static TimerBuilder buildTimer(Object ref, String name) {
return new TimerBuilder(ref.getClass(),name);
}
public interface TimerExecutor {
void doWithTimer() throws Exception;
}
public static class TimerBuilder {
private Timer theTimer;
private Class clazz;
private String name;
public TimerBuilder() {
}
public TimerBuilder(Timer theTimer) {
this.theTimer = theTimer;
}
public TimerBuilder(Class clazz, String name) {
this.clazz = clazz;
this.name = name;
}
public TimerBuilder condition(boolean ifTrue) {
if (ifTrue == false) {
theTimer = nullTimerSingleton;
}
return this;
}
public Timer timer() {
if (theTimer == null) {
com.codahale.metrics.Timer t = metrics.timer(MetricRegistry.name(clazz,name));
com.codahale.metrics.Timer.Context tContext = t.time();
theTimer = new Timer(tContext);
}
return theTimer;
}
public void exec(TimerExecutor executor) throws Exception {
if (theTimer == null) {
com.codahale.metrics.Timer t = metrics.timer(MetricRegistry.name(clazz, name));
com.codahale.metrics.Timer.Context tContext = t.time();
theTimer = new Timer(tContext);
}
executor.doWithTimer();
theTimer.close();
}
}
/**
*
*/
public static class Timer implements AutoCloseable {
private com.codahale.metrics.Timer.Context timerContext;
protected Timer(com.codahale.metrics.Timer.Context timerContext) {
this.timerContext = timerContext;
}
@Override
public void close() {
timerContext.stop();
}
public Timer condition() {
return null;
}
}
private static class NullTimer extends Timer {
protected NullTimer(com.codahale.metrics.Timer.Context timerContext) {
super(timerContext);
}
@Override
public void close() {
// Does nothing
}
}
}
Loading…
Cancel
Save