aboutsummaryrefslogtreecommitdiff
path: root/src/main/scala/kamon/TraceContext.scala
blob: b137168c4bda10ba9476c349fdb771c7984d504d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package kamon

import java.util.UUID
import akka.actor.ActorPath


case class TraceContext(id: UUID, entries: List[TraceEntry]) {
  def fork = this.copy(entries = Nil)
  def withEntry(entry: TraceEntry) = this.copy(entries = entry :: entries)
}

object TraceContext {
  val current = new ThreadLocal[TraceContext]
}

trait TraceEntry
case class MessageExecutionTime(actorPath: ActorPath, initiated: Long, ended: Long)

case class CodeBlockExecutionTime(blockName: String, begin: Long, end: Long) extends TraceEntry




trait TraceSupport {
  import TraceContext.current


  def trace[T](blockName: String)(f: => T): T = {
    val before = System.currentTimeMillis

    val result = f

    val after = System.currentTimeMillis
    swapContext(current.get().withEntry(CodeBlockExecutionTime(blockName, before, after)))

    result
  }

  def swapContext(newContext: TraceContext) {
    current.set(newContext)
  }
}