Created
January 4, 2012 01:32
-
-
Save berngp/1557974 to your computer and use it in GitHub Desktop.
Trait with NewRelic and Scala Metrics.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) ) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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