88use Jaeger \Span \Context \SpanContext ;
99use Jaeger \Span \Factory \SpanFactoryInterface ;
1010use Jaeger \Span \SpanInterface ;
11-
12- class Tracer implements TracerInterface,
13- ContextAwareInterface,
14- InjectableInterface,
15- FlushableInterface,
16- ResettableInterface,
17- DebuggableInterface
11+ use Jaeger \Span \SpanManager ;
12+
13+ class Tracer implements
14+ TracerInterface,
15+ ContextAwareInterface,
16+ InjectableInterface,
17+ FlushableInterface,
18+ ResettableInterface,
19+ DebuggableInterface
1820{
19- private $ stack ;
21+ private $ manager ;
2022
2123 private $ debugId = '' ;
2224
2325 private $ factory ;
2426
2527 private $ client ;
2628
27- public function __construct (\ SplStack $ stack , SpanFactoryInterface $ factory , ClientInterface $ client )
29+ public function __construct (SpanManager $ manager , SpanFactoryInterface $ factory , ClientInterface $ client )
2830 {
29- $ this ->stack = $ stack ;
31+ $ this ->manager = $ manager ;
3032 $ this ->factory = $ factory ;
3133 $ this ->client = $ client ;
3234 }
@@ -54,27 +56,21 @@ public function flush(): FlushableInterface
5456
5557 public function assign (SpanContext $ context ): InjectableInterface
5658 {
57- $ this ->stack -> push ($ context );
59+ $ this ->manager -> assign ($ context );
5860
5961 return $ this ;
6062 }
6163
6264 public function reset (): ResettableInterface
6365 {
64- $ this ->stack = new \ SplStack ();
66+ $ this ->manager -> reset ();
6567
6668 return $ this ;
6769 }
6870
6971 public function remove (SpanContext $ context ): InjectableInterface
7072 {
71- while ($ this ->stack ->valid ()) {
72- if (spl_object_hash ($ this ->stack ->top ()) !== spl_object_hash ($ context )) {
73- $ this ->stack ->pop ();
74- continue ;
75- }
76- break ;
77- }
73+ $ this ->manager ->remove ($ context );
7874
7975 return $ this ;
8076 }
@@ -87,45 +83,39 @@ public function getClient(): ClientInterface
8783 public function debug (string $ operationName , array $ tags = []): SpanInterface
8884 {
8985 $ span = $ this ->factory ->parent ($ this , $ operationName , str_shuffle ('01234567890abcdef ' ), $ tags );
90- $ this ->stack ->push ($ span-> getContext () );
86+ $ this ->manager ->push ($ span );
9187
9288 return $ span ;
9389 }
9490
9591 public function start (string $ operationName , array $ tags = [], SpanContext $ userContext = null ): SpanInterface
9692 {
97- if (null === ($ context = $ this ->getContext ($ userContext ))) {
93+ if (null === ($ context = $ userContext ?: $ this ->manager -> getContext ())) {
9894 $ span = $ this ->factory ->parent ($ this , $ operationName , $ this ->debugId , $ tags );
9995 } else {
10096 $ span = $ this ->factory ->child ($ this , $ operationName , $ context , $ tags );
10197 }
102- $ this ->stack ->push ($ span-> getContext () );
98+ $ this ->manager ->push ($ span );
10399
104100 return $ span ;
105101 }
106102
107103 public function getContext (SpanContext $ userContext = null ): ?SpanContext
108104 {
109- if (null !== $ userContext ) {
110- return $ userContext ;
111- }
112- if (0 !== $ this ->stack ->count ()) {
113- return $ this ->stack ->top ();
114- }
115-
116- return null ;
105+ return $ this ->manager ->getContext ();
117106 }
118107
119- public function finish (SpanInterface $ span , int $ duration = 0 ): TracerInterface
108+ public function finish (SpanInterface $ span , int $ duration = 0 ): void
120109 {
121- if (0 !== $ this ->stack ->count ()) {
122- $ this ->stack ->pop ();
110+ if (-1 !== $ duration ) {
111+ $ span ->finish ($ duration );
112+
113+ return ;
123114 }
124- if (false === $ span ->finish ($ duration )->isSampled ()) {
125- return $ this ;
115+ $ this ->manager ->pop ();
116+ if (false === $ span ->isSampled ()) {
117+ return ;
126118 }
127119 $ this ->client ->add ($ span );
128-
129- return $ this ;
130120 }
131121}
0 commit comments