aboutsummaryrefslogtreecommitdiff
path: root/kamon-core/src/main/scala/kamon/trace
diff options
context:
space:
mode:
authorIvan Topolnak <itopolnak@despegar.com>2014-01-02 18:09:53 -0300
committerIvan Topolnak <itopolnak@despegar.com>2014-01-13 17:37:20 -0300
commit25d9d514e413a9b6361dba26a9b94bee886e15bd (patch)
treee460f1e9a08eb7e0f2558169777259fd260c2b1a /kamon-core/src/main/scala/kamon/trace
parent0915ccaf0586e29b0e223c55fdb7acf23fc7264f (diff)
downloadKamon-25d9d514e413a9b6361dba26a9b94bee886e15bd.tar.gz
Kamon-25d9d514e413a9b6361dba26a9b94bee886e15bd.tar.bz2
Kamon-25d9d514e413a9b6361dba26a9b94bee886e15bd.zip
integrate trace and metrics into the base project
Diffstat (limited to 'kamon-core/src/main/scala/kamon/trace')
-rw-r--r--kamon-core/src/main/scala/kamon/trace/Segments.scala38
-rw-r--r--kamon-core/src/main/scala/kamon/trace/Trace.scala114
-rw-r--r--kamon-core/src/main/scala/kamon/trace/TraceContext.scala51
-rw-r--r--kamon-core/src/main/scala/kamon/trace/UowTracing.scala82
-rw-r--r--kamon-core/src/main/scala/kamon/trace/logging/LogbackUowConverter.scala24
5 files changed, 309 insertions, 0 deletions
diff --git a/kamon-core/src/main/scala/kamon/trace/Segments.scala b/kamon-core/src/main/scala/kamon/trace/Segments.scala
new file mode 100644
index 00000000..0bc68ee7
--- /dev/null
+++ b/kamon-core/src/main/scala/kamon/trace/Segments.scala
@@ -0,0 +1,38 @@
+/* ===================================================
+ * Copyright © 2013 the kamon project <http://kamon.io/>
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ========================================================== */
+
+package kamon.trace
+
+import kamon.trace.Trace.SegmentCompletionHandle
+
+object Segments {
+
+ trait Category
+ case object HttpClientRequest extends Category
+
+ case class Start(category: Category, description: String = "",
+ attributes: Map[String, String] = Map(), timestamp: Long = System.nanoTime())
+
+ case class End(attributes: Map[String, String] = Map(), timestamp: Long = System.nanoTime())
+
+ case class Segment(start: Start, end: End)
+
+ trait SegmentCompletionHandleAware {
+ var completionHandle: Option[SegmentCompletionHandle]
+ }
+
+ trait ContextAndSegmentCompletionAware extends ContextAware with SegmentCompletionHandleAware
+}
diff --git a/kamon-core/src/main/scala/kamon/trace/Trace.scala b/kamon-core/src/main/scala/kamon/trace/Trace.scala
new file mode 100644
index 00000000..31e8185a
--- /dev/null
+++ b/kamon-core/src/main/scala/kamon/trace/Trace.scala
@@ -0,0 +1,114 @@
+/* ===================================================
+ * Copyright © 2013 the kamon project <http://kamon.io/>
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ========================================================== */
+package kamon.trace
+
+import kamon.Kamon
+import akka.actor._
+import scala.Some
+import kamon.trace.Trace.Register
+import scala.concurrent.duration._
+import java.util.concurrent.atomic.AtomicLong
+
+object Trace extends ExtensionId[TraceExtension] with ExtensionIdProvider {
+ def lookup(): ExtensionId[_ <: Extension] = Trace
+ def createExtension(system: ExtendedActorSystem): TraceExtension = new TraceExtension(system)
+
+ /*** Protocol */
+ case object Register
+
+ /** User API */
+ //private[trace] val traceContext = new DynamicVariable[Option[TraceContext]](None)
+ private[trace] val traceContext = new ThreadLocal[Option[TraceContext]] {
+ override def initialValue(): Option[TraceContext] = None
+ }
+ private[trace] val tranid = new AtomicLong()
+
+ def context() = traceContext.get
+ private def set(ctx: Option[TraceContext]) = traceContext.set(ctx)
+
+ def clear: Unit = traceContext.remove()
+ def start(name: String)(implicit system: ActorSystem): TraceContext = {
+ val ctx = newTraceContext(name)
+ ctx.start(name)
+ set(Some(ctx))
+
+ ctx
+ }
+
+ def withContext[T](ctx: Option[TraceContext])(thunk: ⇒ T): T = {
+ val oldval = context
+ set(ctx)
+
+ try thunk
+ finally set(oldval)
+ }
+
+ def transformContext(f: TraceContext ⇒ TraceContext): Unit = {
+ context.map(f).foreach(ctx ⇒ set(Some(ctx)))
+ }
+
+ def finish(): Option[TraceContext] = {
+ val ctx = context()
+ ctx.map(_.finish)
+ clear
+ ctx
+ }
+
+ // TODO: FIX
+ def newTraceContext(name: String)(implicit system: ActorSystem): TraceContext = TraceContext(Kamon(Trace).api, tranid.getAndIncrement, name)
+
+ def startSegment(category: Segments.Category, description: String = "", attributes: Map[String, String] = Map()): SegmentCompletionHandle = {
+ val start = Segments.Start(category, description, attributes)
+ SegmentCompletionHandle(start)
+ }
+
+ def startSegment(start: Segments.Start): SegmentCompletionHandle = SegmentCompletionHandle(start)
+
+ case class SegmentCompletionHandle(start: Segments.Start) {
+ def complete(): Unit = {
+ val end = Segments.End()
+ println(s"Completing the Segment: $start - $end")
+ }
+ def complete(end: Segments.End): Unit = {
+ println(s"Completing the Segment: $start - $end")
+ }
+ }
+}
+
+class TraceExtension(system: ExtendedActorSystem) extends Kamon.Extension {
+ val api: ActorRef = system.actorOf(Props[TraceManager], "kamon-trace")
+}
+
+class TraceManager extends Actor with ActorLogging {
+ var listeners: Seq[ActorRef] = Seq.empty
+
+ def receive = {
+ case Register ⇒
+ listeners = sender +: listeners
+ log.info("Registered [{}] as listener for Kamon traces", sender)
+
+ case segment: UowSegment ⇒
+ val tracerName = segment.id.toString
+ context.child(tracerName).getOrElse(newTracer(tracerName)) ! segment
+
+ case trace: UowTrace ⇒
+ listeners foreach (_ ! trace)
+ }
+
+ def newTracer(name: String): ActorRef = {
+ context.actorOf(UowTraceAggregator.props(self, 30 seconds), name)
+ }
+}
diff --git a/kamon-core/src/main/scala/kamon/trace/TraceContext.scala b/kamon-core/src/main/scala/kamon/trace/TraceContext.scala
new file mode 100644
index 00000000..3e68a816
--- /dev/null
+++ b/kamon-core/src/main/scala/kamon/trace/TraceContext.scala
@@ -0,0 +1,51 @@
+/* ===================================================
+ * Copyright © 2013 the kamon project <http://kamon.io/>
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ========================================================== */
+package kamon.trace
+
+import java.util.UUID
+import akka.actor._
+import java.util.concurrent.atomic.AtomicLong
+import scala.concurrent.duration._
+import kamon.Kamon
+import kamon.trace.UowTracing.{ Finish, Start }
+
+// TODO: Decide if we need or not an ID, generating it takes time and it doesn't seem necessary.
+case class TraceContext(private val collector: ActorRef, id: Long, uow: String = "", userContext: Option[Any] = None) {
+
+ def start(name: String) = {
+ collector ! Start(id, name)
+ }
+
+ def finish: Unit = {
+ collector ! Finish(id)
+ }
+
+}
+
+trait ContextAware {
+ def traceContext: Option[TraceContext]
+}
+
+object ContextAware {
+ def default: ContextAware = new ContextAware {
+ val traceContext: Option[TraceContext] = Trace.context()
+ }
+}
+
+trait TimedContextAware {
+ def timestamp: Long
+ def traceContext: Option[TraceContext]
+}
diff --git a/kamon-core/src/main/scala/kamon/trace/UowTracing.scala b/kamon-core/src/main/scala/kamon/trace/UowTracing.scala
new file mode 100644
index 00000000..20cce830
--- /dev/null
+++ b/kamon-core/src/main/scala/kamon/trace/UowTracing.scala
@@ -0,0 +1,82 @@
+/* ===================================================
+ * Copyright © 2013 the kamon project <http://kamon.io/>
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ========================================================== */
+package kamon.trace
+
+import akka.actor._
+import scala.concurrent.duration.Duration
+import kamon.trace.UowTracing._
+
+sealed trait UowSegment {
+ def id: Long
+ def timestamp: Long
+}
+
+trait AutoTimestamp extends UowSegment {
+ val timestamp = System.nanoTime
+}
+
+object UowTracing {
+ case class Start(id: Long, name: String) extends AutoTimestamp
+ case class Finish(id: Long) extends AutoTimestamp
+ case class Rename(id: Long, name: String) extends AutoTimestamp
+ case class WebExternalStart(id: Long, host: String) extends AutoTimestamp
+ case class WebExternalFinish(id: Long) extends AutoTimestamp
+ case class WebExternal(id: Long, start: Long, finish: Long, host: String) extends AutoTimestamp
+}
+
+case class UowTrace(name: String, uow: String, start: Long, end: Long, segments: Seq[UowSegment]) {
+ def elapsed: Long = end - start
+}
+
+class UowTraceAggregator(reporting: ActorRef, aggregationTimeout: Duration) extends Actor with ActorLogging {
+ context.setReceiveTimeout(aggregationTimeout)
+
+ var name: String = "UNKNOWN"
+ var segments: Seq[UowSegment] = Nil
+
+ var pendingExternal = List[WebExternalStart]()
+
+ var start = 0L
+ var end = 0L
+
+ def receive = {
+ case start: Start ⇒
+ this.start = start.timestamp
+ segments = segments :+ start
+ name = start.name
+ case finish: Finish ⇒
+ end = finish.timestamp
+ segments = segments :+ finish; finishTracing()
+ case wes: WebExternalStart ⇒ pendingExternal = pendingExternal :+ wes
+ case finish @ WebExternalFinish(id) ⇒ pendingExternal.find(_.id == id).map(start ⇒ {
+ segments = segments :+ WebExternal(finish.id, start.timestamp, finish.timestamp, start.host)
+ })
+ case Rename(id, newName) ⇒ name = newName
+ case segment: UowSegment ⇒ segments = segments :+ segment
+ case ReceiveTimeout ⇒
+ log.warning("Transaction {} did not complete properly, the recorded segments are: {}", name, segments)
+ context.stop(self)
+ }
+
+ def finishTracing(): Unit = {
+ reporting ! UowTrace(name, "", start, end, segments)
+ context.stop(self)
+ }
+}
+
+object UowTraceAggregator {
+ def props(reporting: ActorRef, aggregationTimeout: Duration) = Props(classOf[UowTraceAggregator], reporting, aggregationTimeout)
+}
diff --git a/kamon-core/src/main/scala/kamon/trace/logging/LogbackUowConverter.scala b/kamon-core/src/main/scala/kamon/trace/logging/LogbackUowConverter.scala
new file mode 100644
index 00000000..add47fdf
--- /dev/null
+++ b/kamon-core/src/main/scala/kamon/trace/logging/LogbackUowConverter.scala
@@ -0,0 +1,24 @@
+/* ===================================================
+ * Copyright © 2013 the kamon project <http://kamon.io/>
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ========================================================== */
+package kamon.trace.logging
+
+import ch.qos.logback.classic.pattern.ClassicConverter
+import ch.qos.logback.classic.spi.ILoggingEvent
+import kamon.trace.Trace
+
+class LogbackUowConverter extends ClassicConverter {
+ def convert(event: ILoggingEvent): String = Trace.context().map(_.uow).getOrElse("undefined")
+}