@@ -14,14 +14,100 @@ import brave.propagation.TraceContextOrSamplingFlags
14
14
import zipkin2 .codec .SpanBytesEncoder .JSON_V1
15
15
import zipkin2 .codec .SpanBytesEncoder .JSON_V2
16
16
17
- final class BraveTracer private (
17
+ sealed trait BraveTracer {
18
+ def startNewChildTracer (name : String , tags : (String , String )* ): BraveTracer
19
+
20
+ def trace [T ](name : String , tags : (String , String )* )(
21
+ thunk : BraveTracer => T
22
+ ): T
23
+
24
+ def traceVerbose [T ](name : String , tags : (String , String )* )(
25
+ thunk : BraveTracer => T
26
+ ): T
27
+
28
+ def traceTask [T ](name : String , tags : (String , String )* )(
29
+ thunk : BraveTracer => Task [T ]
30
+ ): Task [T ]
31
+
32
+ def traceTaskVerbose [T ](name : String , tags : (String , String )* )(
33
+ thunk : BraveTracer => Task [T ]
34
+ ): Task [T ]
35
+
36
+ def terminate (): Unit
37
+
38
+ def currentSpan : Option [Span ]
39
+
40
+ def toIndependentTracer (
41
+ name : String ,
42
+ traceProperties : TraceProperties ,
43
+ tags : (String , String )*
44
+ ): BraveTracer
45
+
46
+ }
47
+
48
+ object NoopTracer extends BraveTracer {
49
+
50
+ override def startNewChildTracer (name : String , tags : (String , String )* ): BraveTracer = this
51
+
52
+ override def trace [T ](name : String , tags : (String , String )* )(thunk : BraveTracer => T ): T = thunk(
53
+ this
54
+ )
55
+
56
+ override def traceVerbose [T ](name : String , tags : (String , String )* )(thunk : BraveTracer => T ): T =
57
+ thunk(this )
58
+
59
+ override def traceTask [T ](name : String , tags : (String , String )* )(
60
+ thunk : BraveTracer => Task [T ]
61
+ ): Task [T ] = thunk(this )
62
+
63
+ override def traceTaskVerbose [T ](name : String , tags : (String , String )* )(
64
+ thunk : BraveTracer => Task [T ]
65
+ ): Task [T ] = thunk(this )
66
+
67
+ override def terminate (): Unit = ()
68
+
69
+ override def currentSpan : Option [Span ] = None
70
+
71
+ def toIndependentTracer (
72
+ name : String ,
73
+ traceProperties : TraceProperties ,
74
+ tags : (String , String )*
75
+ ): BraveTracer = this
76
+
77
+ }
78
+
79
+ object BraveTracer {
80
+
81
+ def apply (name : String , properties : TraceProperties , tags : (String , String )* ): BraveTracer = {
82
+ BraveTracer (name, properties, None , tags : _* )
83
+ }
84
+
85
+ def apply (
86
+ name : String ,
87
+ properties : TraceProperties ,
88
+ ctx : Option [TraceContext ],
89
+ tags : (String , String )*
90
+ ): BraveTracer = {
91
+ if (properties.enabled) {
92
+ BraveTracerInternal (name, properties, ctx, tags : _* )
93
+ } else {
94
+ NoopTracer
95
+ }
96
+
97
+ }
98
+ }
99
+
100
+ final class BraveTracerInternal private (
18
101
tracer : Tracer ,
19
- val currentSpan : Span ,
102
+ val _currentSpan : Span ,
20
103
closeCurrentSpan : () => Unit ,
21
104
properties : TraceProperties
22
- ) {
105
+ ) extends BraveTracer {
106
+
107
+ def currentSpan = Some (_currentSpan)
108
+
23
109
def startNewChildTracer (name : String , tags : (String , String )* ): BraveTracer = {
24
- val span = tags.foldLeft(tracer.newChild(currentSpan .context).name(name)) {
110
+ val span = tags.foldLeft(tracer.newChild(_currentSpan .context).name(name)) {
25
111
case (span, (tagKey, tagValue)) => span.tag(tagKey, tagValue)
26
112
}
27
113
@@ -32,7 +118,7 @@ final class BraveTracer private (
32
118
span.finish()
33
119
}
34
120
35
- new BraveTracer (tracer, span, closeHandler, properties)
121
+ new BraveTracerInternal (tracer, span, closeHandler, properties)
36
122
}
37
123
38
124
def trace [T ](name : String , tags : (String , String )* )(
@@ -67,7 +153,7 @@ final class BraveTracer private (
67
153
try thunk(newTracer) // Don't catch and report errors in spans
68
154
catch {
69
155
case NonFatal (t) =>
70
- newTracer.currentSpan.error(t)
156
+ newTracer.currentSpan.foreach(_. error(t) )
71
157
throw t
72
158
} finally {
73
159
try newTracer.terminate()
@@ -91,7 +177,7 @@ final class BraveTracer private (
91
177
case None => Task .eval(newTracer.terminate())
92
178
case Some (value) =>
93
179
Task .eval {
94
- newTracer.currentSpan.error(value)
180
+ newTracer.currentSpan.foreach(_. error(value) )
95
181
newTracer.terminate()
96
182
}
97
183
}
@@ -116,10 +202,10 @@ final class BraveTracer private (
116
202
traceProperties : TraceProperties ,
117
203
tags : (String , String )*
118
204
): BraveTracer =
119
- BraveTracer (name, traceProperties, Some (currentSpan .context), tags : _* )
205
+ BraveTracer (name, traceProperties, Some (_currentSpan .context), tags : _* )
120
206
}
121
207
122
- object BraveTracer {
208
+ object BraveTracerInternal {
123
209
import brave ._
124
210
import zipkin2 .reporter .AsyncReporter
125
211
import zipkin2 .reporter .urlconnection .URLConnectionSender
@@ -134,10 +220,6 @@ object BraveTracer {
134
220
reporterCache.computeIfAbsent(url, newReporter)
135
221
}
136
222
137
- def apply (name : String , properties : TraceProperties , tags : (String , String )* ): BraveTracer = {
138
- BraveTracer (name, properties, None , tags : _* )
139
- }
140
-
141
223
def apply (
142
224
name : String ,
143
225
properties : TraceProperties ,
@@ -178,6 +260,6 @@ object BraveTracer {
178
260
spanReporter.flush()
179
261
}
180
262
181
- new BraveTracer (tracer, rootSpan, closeEverything, properties)
263
+ new BraveTracerInternal (tracer, rootSpan, closeEverything, properties)
182
264
}
183
265
}
0 commit comments