Skip to content

Commit 1e91ce8

Browse files
committed
Add span manager, rework interaction with tracer
1 parent a879f80 commit 1e91ce8

5 files changed

Lines changed: 134 additions & 47 deletions

File tree

src/Span/Span.php

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
use Jaeger\Tag\OutOfScopeTag;
99
use Jaeger\Thrift\Log;
1010
use Jaeger\Thrift\Tag;
11-
use Jaeger\Tracer\TracerInterface;
11+
use Jaeger\Tracer\FinishableInterface;
1212

1313
class Span extends \Jaeger\Thrift\Span implements SpanInterface
1414
{
@@ -17,7 +17,7 @@ class Span extends \Jaeger\Thrift\Span implements SpanInterface
1717
private $context;
1818

1919
public function __construct(
20-
TracerInterface $tracer,
20+
FinishableInterface $tracer,
2121
SpanContext $context,
2222
string $operationName,
2323
int $startTime,
@@ -67,12 +67,9 @@ public function start(int $startTimeUsec): SpanInterface
6767

6868
public function finish(int $durationUsec = 0): SpanInterface
6969
{
70-
if (0 === $durationUsec) {
71-
$durationUsec = (int)(microtime(true) * 1000000) - $this->startTime;
72-
}
73-
$this->duration = $durationUsec;
70+
$this->duration = $durationUsec ?: (microtime(true) * 1000000) - $this->startTime;
7471

75-
return $this;
72+
return $this->tracer->finish($this, -1);
7673
}
7774

7875
public function addTag(Tag $tag): SpanInterface

src/Span/SpanManager.php

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace Jaeger\Span;
5+
6+
use Jaeger\Span\Context\ContextAwareInterface;
7+
use Jaeger\Span\Context\SpanContext;
8+
use Jaeger\Tracer\InjectableInterface;
9+
use Jaeger\Tracer\ResettableInterface;
10+
11+
class SpanManager implements InjectableInterface, ContextAwareInterface, ResettableInterface
12+
{
13+
private $stack;
14+
15+
private $context;
16+
17+
public function __construct()
18+
{
19+
$this->stack = new \SplStack();
20+
}
21+
22+
/**
23+
* @return self
24+
*/
25+
public function reset(): ResettableInterface
26+
{
27+
$this->stack = new \SplStack();
28+
29+
return $this;
30+
}
31+
32+
/**
33+
* @param SpanContext $context
34+
*
35+
* @return self
36+
*/
37+
public function assign(SpanContext $context): InjectableInterface
38+
{
39+
$this->context = $context;
40+
41+
return $this->reset();
42+
}
43+
44+
/**
45+
* @param SpanContext $context
46+
*
47+
* @return self
48+
*/
49+
public function remove(SpanContext $context): InjectableInterface
50+
{
51+
while ($this->stack->valid()) {
52+
if (spl_object_hash($this->stack->top()) !== spl_object_hash($context)) {
53+
$this->stack->pop();
54+
continue;
55+
}
56+
break;
57+
}
58+
59+
return $this;
60+
}
61+
62+
public function getSpan(): ?SpanInterface
63+
{
64+
return $this->stack->count() ? $this->stack->top() : null;
65+
}
66+
67+
public function push(SpanInterface $span): void
68+
{
69+
$this->stack->push($span);
70+
}
71+
72+
public function pop(): ?SpanInterface
73+
{
74+
return $this->stack->count() ? $this->stack->pop() : null;
75+
}
76+
77+
public function getContext(): ?SpanContext
78+
{
79+
return ($span = $this->getSpan()) ? $span->getContext() : $this->context;
80+
}
81+
}

src/Tracer/FinishableInterface.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace Jaeger\Tracer;
5+
6+
use Jaeger\Span\SpanInterface;
7+
8+
/**
9+
* Interface FinishableInterface
10+
* @internal use SpanInterface::finish(int $duration = 0)
11+
*/
12+
interface FinishableInterface
13+
{
14+
/**
15+
* @param SpanInterface $span
16+
* @param int $duration
17+
*
18+
* @return mixed
19+
*/
20+
public function finish(SpanInterface $span, int $duration = 0): void;
21+
}

src/Tracer/Tracer.php

Lines changed: 27 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,27 @@
88
use Jaeger\Span\Context\SpanContext;
99
use Jaeger\Span\Factory\SpanFactoryInterface;
1010
use 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
}

src/Tracer/TracerInterface.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@
66
use Jaeger\Span\Context\SpanContext;
77
use Jaeger\Span\SpanInterface;
88

9-
interface TracerInterface
9+
interface TracerInterface extends FinishableInterface
1010
{
1111
public function start(string $operationName, array $tags = [], SpanContext $context = null): SpanInterface;
12-
13-
public function finish(SpanInterface $span, int $duration = 0);
1412
}

0 commit comments

Comments
 (0)