aboutsummaryrefslogtreecommitdiff
path: root/kamon-core/src/main/scala/kamon/trace/TraceCtx.scala
blob: 1e5525637fa97185e74e215de4ab526141c4ca8c (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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/*
 * =========================================================================================
 * 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.{ExtendedActorSystem, ActorSystem}
import akka.dispatch.AbstractNodeQueue
import kamon.Kamon
import kamon.metrics.{TraceMetrics, Metrics}
import java.util.concurrent.atomic.AtomicLong

sealed trait TracingLevelOfDetail
case object OnlyMetrics extends TracingLevelOfDetail
case object SimpleTrace extends TracingLevelOfDetail
case object FullTrace extends TracingLevelOfDetail

trait TraceContext {
  def token: String
  def name: String
  def rename(newName: String): Unit
  def levelOfDetail: TracingLevelOfDetail
  def startSegment
  def finish(metadata: Map[String, String])

}

trait TraceContextAware {
  def captureMark: Long
  def traceContext: Option[TraceContext]
}

object TraceContextAware {
  def default: TraceContextAware = new TraceContextAware {
    val captureMark = System.nanoTime()
    val traceContext = TraceRecorder.currentContext
  }
}

object TraceContext {

}

class SimpleMetricCollectionContext(val token: String, @volatile private var _name: String, val system: ActorSystem,
                                    metadata: Map[String, String]) extends TraceContext {
  val levelOfDetail = OnlyMetrics

  @volatile private var _isOpen = true

  val startMark = System.nanoTime()

  def name: String = _name

  def rename(newName: String): Unit = _name = newName

  def isOpen(): Boolean = _isOpen

  def finish(metadata: Map[String, String]): Unit = {
    val finishMark = System.nanoTime()

    // Store all metrics!
    val metricsExtension = Kamon(Metrics)(system)
    val metricRecorder = metricsExtension.register(name, TraceMetrics)

    metricRecorder.map { traceMetrics =>
      traceMetrics.elapsedTime.record(finishMark - startMark)
    }

  }

  override def startSegment: Unit = ???


}

private[kamon] class SegmentRecordingQueue extends AbstractNodeQueue[String]




class TraceRecorder(system: ExtendedActorSystem) {

}

object TraceRecorder {
  private val tokenCounter = new AtomicLong
  private val traceContextStorage = new ThreadLocal[Option[TraceContext]] {
    override def initialValue(): Option[TraceContext] = None
  }

  private def newTraceContext(name: String, token: Option[String], metadata: Map[String, String], system: ActorSystem): TraceContext = ???

  def setContext(context: Option[TraceContext]): Unit = traceContextStorage.set(context)

  def clearContext: Unit = traceContextStorage.set(None)

  def currentContext: Option[TraceContext] = traceContextStorage.get()

  def start(name: String, token: Option[String] = None, metadata: Map[String, String] = Map.empty)(implicit system: ActorSystem) = {
    val ctx = newTraceContext(name, token, metadata, system)
    traceContextStorage.set(Some(ctx))
  }

  def withContext[T](context: Option[TraceContext])(thunk: => T): T = {
    val oldContext = currentContext
    setContext(context)

    try thunk finally setContext(oldContext)
  }

  def finish(metadata: Map[String, String] = Map.empty): Unit = currentContext.map(_.finish(metadata))

}