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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
|
package kamon
package trace
import scala.collection.JavaConverters._
import kamon.util.Clock
object Span {
val MetricCategory = "span"
val LatencyMetricName = "elapsed-time"
val ErrorMetricName = "error"
val MetricTagPrefix = "metric."
val BooleanTagTrueValue = "1"
val BooleanTagFalseValue = "0"
case class LogEntry(timestamp: Long, fields: Map[String, _])
case class CompletedSpan(
context: SpanContext,
operationName: String,
startTimestampMicros: Long,
endTimestampMicros: Long,
tags: Map[String, String],
logs: Seq[LogEntry]
)
}
class Span(spanContext: SpanContext, initialOperationName: String, initialTags: Map[String, String], startTimestampMicros: Long,
recorderRegistry: Any, reporterRegistry: ReporterRegistryImpl) extends io.opentracing.Span {
private var isOpen: Boolean = true
private val sampled: Boolean = spanContext.sampled
private var operationName: String = initialOperationName
private var endTimestampMicros: Long = 0
private var tags = initialTags
private var logs = List.empty[Span.LogEntry]
private var metricTags = Map.empty[String, String]
override def log(fields: java.util.Map[String, _]): Span =
log(fields.asScala.asInstanceOf[Map[String, _]])
def log(fields: Map[String, _]): Span = synchronized {
if (sampled && isOpen)
logs = Span.LogEntry(Clock.microTimestamp(), fields) :: logs
this
}
def log(timestampMicroseconds: Long, fields: Map[String, _]): Span = synchronized {
if(sampled && isOpen)
logs = Span.LogEntry(timestampMicroseconds, fields) :: logs
this
}
override def log(timestampMicroseconds: Long, fields: java.util.Map[String, _]): Span =
log(timestampMicroseconds, fields.asScala.asInstanceOf[Map[String, _]])
override def log(event: String): Span = synchronized {
if(sampled && isOpen)
logs = Span.LogEntry(Clock.microTimestamp(), Map("event" -> event)) :: logs
this
}
override def log(timestampMicroseconds: Long, event: String): Span = synchronized {
if(sampled && isOpen)
logs = Span.LogEntry(timestampMicroseconds, Map("event" -> event)) :: logs
this
}
override def log(eventName: String, payload: scala.Any): Span = synchronized {
if(sampled && isOpen)
logs = Span.LogEntry(Clock.microTimestamp(), Map(eventName -> payload)) :: logs
this
}
override def log(timestampMicroseconds: Long, eventName: String, payload: scala.Any): Span = synchronized {
if(sampled && isOpen)
logs = Span.LogEntry(timestampMicroseconds, Map(eventName -> payload)) :: logs
this
}
override def getBaggageItem(key: String): String =
spanContext.getBaggage(key)
override def context(): SpanContext =
spanContext
override def setTag(key: String, value: String): Span = synchronized {
if (isOpen) {
extractMetricTag(key, value)
if(sampled)
tags = tags ++ Map(key -> value)
}
this
}
override def setTag(key: String, value: Boolean): Span = {
if (isOpen) {
val tagValue = if(value) Span.BooleanTagTrueValue else Span.BooleanTagFalseValue
extractMetricTag(key, tagValue)
if(sampled)
tags = tags + (key -> tagValue)
}
this
}
override def setTag(key: String, value: Number): Span = {
if (isOpen) {
val tagValue = String.valueOf(value)
extractMetricTag(key, tagValue)
if(sampled)
tags = tags + (key -> tagValue)
}
this
}
def setMetricTag(key: String, value: String): Span = synchronized {
if (isOpen)
metricTags = metricTags ++ Map(key -> value)
this
}
override def setBaggageItem(key: String, value: String): Span = synchronized {
if (isOpen)
spanContext.addBaggageItem(key, value)
this
}
override def setOperationName(operationName: String): Span = synchronized {
if(isOpen)
this.operationName = operationName
this
}
private def extractMetricTag(tag: String, value: String): Unit =
if(tag.startsWith(Span.MetricTagPrefix))
metricTags = metricTags ++ Map(tag.substring(Span.MetricTagPrefix.length) -> value)
override def finish(): Unit =
finish(Clock.microTimestamp())
override def finish(finishMicros: Long): Unit =
if(isOpen) {
isOpen = false
endTimestampMicros = finishMicros
recordSpanMetrics()
reporterRegistry.reportSpan(completedSpan)
}
private def completedSpan: Span.CompletedSpan =
Span.CompletedSpan(spanContext, operationName, startTimestampMicros, endTimestampMicros, tags, logs)
private def recordSpanMetrics(): Unit = {
val elapsedTime = endTimestampMicros - startTimestampMicros
// val entity = Entity(operationName, Span.MetricCategory, metricTags)
// val recorder = recorderRegistry.getRecorder(entity)
// recorder
// .histogram(Span.LatencyMetricName, MeasurementUnit.time.microseconds, DynamicRange.Default)
// .record(elapsedTime)
//
// tags.get("error").foreach { errorTag =>
// if(errorTag != null && errorTag.equals(Span.BooleanTagTrueValue)) {
// recorder.counter(Span.ErrorMetricName).increment()
// }
// }
}
}
|