Skip to content

Money in Scala

paulcleary edited this page Nov 10, 2015 · 2 revisions

Money is built primarily with Scala (and Akka), so the Money Scala API provides constructs that take advantage of Functions to make it simple to add tracing to your code.

Getting Started

Add the Money Dependencies you need to your build. Money is cross-compiled for Scala 2.10 and Scala 2.11.

libraryDependencies += "com.comcast.money" %% "money-core" % "${money.version}"

Starting and Stopping Spans

Money provides a standard function wrapper to allow you to start and stop spans around a block of code.

  import com.comcast.money.core.Tracers._

  def somethingMeaningful() = traced("something") {
    ... make it worthwhile ...
  }

"traced" is a simple wrapper function that does a "startSpan" and "stopSpan" around the code block.

Recording Notes

Recording notes is simple, using the record functions on the Tracer object. The main tracer can be found on the Money object. You should use this Tracer for all of your tracing needs.

Just like Java, you can only record Strings, Longs, Boolean, and Double types

  import com.comcast.money.core.Tracers._
  import com.comcast.momey.core.Money.tracer

  def somethingMeaningful(Long indexSize) = traced("something") {
    // associate a note "meaningful" with a value indexSize with the span named "something"
    tracer.record("meaningful", indexSize)
  }

Using Timers

Timers are a mechanism that allow you to capture the duration of some operation. The simplest way to use timers is to wrap the function that you want in a timed wrapper.

  import com.comcast.money.core.Tracers._

  def timeThisChumpie()  = timed("chumpie") {
    ... do your stuff here
  }

Besides the timed function wrapper, you can manually use the Tracer.startTimer and Tracer.stopTimer APIs.

  import com.comcast.money.core.Tracers._
  import com.comcast.momey.core.Money.tracer

  def doSomething(index:Seq[String]) = traced("chumpie") {
    tracer.startTimer("fun-with-money")
  }

  def finishIt() {
    tracer.stopTimer("fun-with-money")
  }
Clone this wiki locally