Skip to content

Instantly share code, notes, and snippets.

@berngp
Created January 4, 2012 01:32
Show Gist options
  • Save berngp/1557974 to your computer and use it in GitHub Desktop.
Save berngp/1557974 to your computer and use it in GitHub Desktop.
Trait with NewRelic and Scala Metrics.
import com.newrelic.api.agent.NewRelic
import collection.mutable.{HashMap, SynchronizedMap}
import com.yammer.metrics.{Timer, MetricsGroup}
trait Measurable {
protected lazy val metricsGroup = new MetricsGroup(this.getClass)
private val timers = new HashMap[String, Timer] with SynchronizedMap[String, Timer]
protected def measure[A](name:String)(f: => A): A = {
val timer = timers.getOrElseUpdate(name, ( metricsGroup.timer (name)) )
timer.time( NewRelic.time(name, f) )
}
}
public class NewRelic {
public static <T> T time(String name, Function<? extends T> f) {
double startTime = System.nanoTime;
try {
return f.get();
} finally {
double delta = System.nanoTime - startTime;
//... NewRelic magic taking into consideration the `name` provided as an argument.
}
}
//this could just be a java.util.concurrent.Future
public interface Function<V> {
V get() throws java.lang.InterruptedException, java.util.concurrent.ExecutionException;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment