WIP metrics

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

@ -33,7 +33,7 @@ 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.buildTimer(this,timerName).condition(timerName.contains("main.view")).timer()) { try (MetricsManager.Timer t = MetricsManager.condition(timerName.contains("main.view")).timer(this,timerName)) {
chain.doFilter(request, response); chain.doFilter(request, response);
} }

@ -13,6 +13,7 @@ public class MetricsManager {
private static final MetricRegistry metrics = new MetricRegistry(); private static final MetricRegistry metrics = new MetricRegistry();
private static JmxReporter reporter; private static JmxReporter reporter;
private static final NullTimer nullTimerSingleton = new NullTimer(null); private static final NullTimer nullTimerSingleton = new NullTimer(null);
private static ConditionFalseTimerBuilder conditionFalseTimerBuilderSingleton = new ConditionFalseTimerBuilder();
static { static {
reporter = JmxReporter.forRegistry(metrics) reporter = JmxReporter.forRegistry(metrics)
@ -22,54 +23,46 @@ public class MetricsManager {
reporter.start(); reporter.start();
} }
public static Timer timer(Class clazz, String name) {
public static TimerBuilder buildTimer(Class clazz, String name) { return new TimerBuilder().timer(clazz,name);
return new TimerBuilder(clazz,name);
} }
public static TimerBuilder buildTimer(Object ref, String name) { public static Timer timer(Object ref, String name) {
return new TimerBuilder(ref.getClass(),name); return timer(ref.getClass(),name);
} }
public interface TimerExecutor { public static TimerBuilder condition(boolean ifTrue) {
void doWithTimer() throws Exception; if (ifTrue == false) {
return conditionFalseTimerBuilderSingleton;
}
return new TimerBuilder();
} }
/* public interface TimerExecutor {
void doWithTimer() throws Exception;
} */
public static class TimerBuilder { public static class TimerBuilder {
private Timer theTimer;
private Class clazz;
private String name;
public TimerBuilder() {
}
public TimerBuilder(Timer theTimer) { /* public TimerBuilder condition(boolean ifTrue) {
this.theTimer = theTimer;
}
public TimerBuilder(Class clazz, String name) {
this.clazz = clazz;
this.name = name;
}
public TimerBuilder condition(boolean ifTrue) {
if (ifTrue == false) { if (ifTrue == false) {
theTimer = nullTimerSingleton; theTimer = nullTimerSingleton;
} }
return this; return this;
} */
public Timer timer(Class clazz, String name) {
com.codahale.metrics.Timer t = metrics.timer(MetricRegistry.name(clazz,name));
com.codahale.metrics.Timer.Context tContext = t.time();
return new Timer(tContext);
} }
public Timer timer() { public Timer timer(Object ref, String name) {
if (theTimer == null) { return timer(ref.getClass(),name);
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 {
/* public void exec(TimerExecutor executor) throws Exception {
if (theTimer == null) { if (theTimer == null) {
com.codahale.metrics.Timer t = metrics.timer(MetricRegistry.name(clazz, name)); com.codahale.metrics.Timer t = metrics.timer(MetricRegistry.name(clazz, name));
com.codahale.metrics.Timer.Context tContext = t.time(); com.codahale.metrics.Timer.Context tContext = t.time();
@ -80,6 +73,14 @@ public class MetricsManager {
theTimer.close(); theTimer.close();
} }
*/
}
private static class ConditionFalseTimerBuilder extends TimerBuilder {
@Override
public Timer timer(Class clazz, String name) {
return nullTimerSingleton;
}
} }
/** /**
@ -98,9 +99,6 @@ public class MetricsManager {
timerContext.stop(); timerContext.stop();
} }
public Timer condition() {
return null;
}
} }
private static class NullTimer extends Timer { private static class NullTimer extends Timer {
@ -114,6 +112,4 @@ public class MetricsManager {
// Does nothing // Does nothing
} }
} }
} }

Loading…
Cancel
Save