aboutsummaryrefslogtreecommitdiff
path: root/kamon-core/src/main/scala/kamon/trace/UowTracing.scala
blob: b38d3d950759e82807781796a2ad32efbb8d085a (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
43
44
45
package kamon.trace

import akka.actor.{Props, ActorRef, Actor}
import kamon.trace.UowTracing.{Start, Finish, Rename}
import scala.concurrent.duration.Duration

sealed trait UowSegment {
  def timestamp: Long
}

trait AutoTimestamp extends UowSegment {
  val timestamp = System.nanoTime
}

object UowTracing {
  case class Start() extends AutoTimestamp
  case class Finish() extends AutoTimestamp
  case class Rename(name: String) extends AutoTimestamp
}

case class UowTrace(name: String, segments: Seq[UowSegment])


class UowTraceAggregator(reporting: ActorRef, aggregationTimeout: Duration) extends Actor {
  context.setReceiveTimeout(aggregationTimeout)
  self ! Start()

  var name: Option[String] = None
  var segments: Seq[UowSegment] = Nil

  def receive = {
    case finish: Finish       => segments = segments :+ finish; finishTracing()
    case Rename(newName)      => name = Some(newName)
    case segment: UowSegment  => segments = segments :+ segment
  }

  def finishTracing(): Unit = {
    reporting ! UowTrace(name.getOrElse("UNKNOWN"), segments)
    context.stop(self)
  }
}

object UowTraceAggregator {
  def props(reporting: ActorRef, aggregationTimeout: Duration) = Props(classOf[UowTraceAggregator], reporting, aggregationTimeout)
}