Skip to content
This repository was archived by the owner on Aug 11, 2025. It is now read-only.

Commit 19591d4

Browse files
authored
Merge pull request #61 from opentracing/60_changes_in_ipcp
[#60] Changes the API for the In process context propagation
2 parents 7846f72 + 77c0efa commit 19591d4

15 files changed

+105
-139
lines changed

README.md

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ composer require opentracing/opentracing
2727

2828
When consuming this library one really only need to worry about a couple of key
2929
abstractions: the `Tracer::startActiveSpan` and `Tracer::startSpan` method,
30-
the `Span` interface, and binding a `Tracer` at bootstrap time. Here are code snippets
30+
the `Span` interface, the `Scope` interface and binding a `Tracer` at bootstrap time. Here are code snippets
3131
demonstrating some important use cases:
3232

3333
### Singleton initialization
@@ -58,7 +58,7 @@ $spanContext = GlobalTracer::get()->extract(
5858
function doSomething() {
5959
...
6060

61-
$span = GlobalTracer::get()->startActiveSpan('my_span', ['child_of' => $spanContext]);
61+
$span = GlobalTracer::get()->startSpan('my_span', ['child_of' => $spanContext]);
6262

6363
...
6464

@@ -77,7 +77,7 @@ function doSomething() {
7777
It's always possible to create a "root" `Span` with no parent or other causal reference.
7878

7979
```php
80-
$span = $tracer->startActiveSpan('my_first_span');
80+
$span = $tracer->startSpan('my_first_span');
8181
...
8282
$span->finish();
8383
```
@@ -95,27 +95,34 @@ Unless you are using asynchronous code that tracks multiple spans at the same
9595
time, such as when using cURL Multi Exec or MySQLi Polling you are better
9696
of just using `Tracer::startActiveSpan` everywhere in your application.
9797

98-
The currently active span gets automatically closed and deactivated from the scope
99-
when you call `$span->finish()` as you can see in the previous example.
98+
The currently active span gets automatically finished when you call `$scope->close()`
99+
as you can see in the previous example.
100100

101101
If you don't want a span to automatically close when `Span::finish()` is called
102-
then you must pass the option `'close_span_on_finish'=> false,` to the `$options`
102+
then you must pass the option `'finish_span_on_close'=> false,` to the `$options`
103103
argument of `startActiveSpan`.
104104

105105
An example of a linear, two level deep span tree using active spans looks like
106106
this in PHP code:
107107

108108
```php
109-
$root = $tracer->startActiveSpan('php');
110-
111-
$controller = $tracer->startActiveSpan('controller');
112-
113-
$http = $tracer->startActiveSpan('http');
114-
file_get_contents('http://php.net');
115-
$http->finish();
109+
// At dispatcher level
110+
$scope = $tracer->startActiveSpan('request');
111+
...
112+
$scope->close();
113+
```
114+
```php
115+
// At controller level
116+
$scope = $tracer->startActiveSpan('controller');
117+
...
118+
$scope->close();
119+
```
116120

117-
$controller->finish();
118-
$root->finish();
121+
```php
122+
// At RPC calls level
123+
$scope = $tracer->startActiveSpan('http');
124+
file_get_contents('http://php.net');
125+
$scope->close();
119126
```
120127

121128
#### Creating a child span assigning parent manually
@@ -153,11 +160,11 @@ $child = GlobalTracer::get()->startActiveSpan('my_second_span');
153160

154161
...
155162

156-
$child->finish();
163+
$child->close();
157164

158165
...
159166

160-
$parent->finish();
167+
$parent->close();
161168
```
162169

163170
### Serializing to the wire
@@ -237,7 +244,7 @@ register_shutdown_function(function() {
237244
This is optional, tracers can decide to immediately send finished spans to a
238245
backend. The flush call can be implemented as a NO-OP for these tracers.
239246

240-
### Using Span Options
247+
### Using `StartSpanOptions`
241248

242249
Passing options to the pass can be done using either an array or the
243250
SpanOptions wrapper object. The following keys are valid:
@@ -246,6 +253,8 @@ SpanOptions wrapper object. The following keys are valid:
246253
- `child_of` is an object of type `OpenTracing\SpanContext` or `OpenTracing\Span`.
247254
- `references` is an array of `OpenTracing\Reference`.
248255
- `tags` is an array with string keys and scalar values that represent OpenTracing tags.
256+
- `finish_span_on_close` is a boolean that determines whether a span should be finished or not when the
257+
scope is closed.
249258

250259
```php
251260
$span = $tracer->startActiveSpan('my_span', [

src/OpenTracing/Exceptions/InvalidSpanOption.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,10 @@ public static function forInvalidReferenceSet($value)
103103
* @param mixed $value
104104
* @return InvalidSpanOption
105105
*/
106-
public static function forCloseSpanOnFinish($value)
106+
public static function forFinishSpanOnClose($value)
107107
{
108108
return new self(sprintf(
109-
'Invalid type for close_span_on_finish. Expected bool, got %s',
109+
'Invalid type for finish_span_on_close. Expected bool, got %s',
110110
is_object($value) ? get_class($value) : gettype($value)
111111
));
112112
}

src/OpenTracing/Mock/MockScope.php

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,35 @@ final class MockScope implements Scope
1717
*/
1818
private $scopeManager;
1919

20-
public function __construct(MockScopeManager $scopeManager, Span $span)
21-
{
20+
/**
21+
* @var bool
22+
*/
23+
private $finishSpanOnClose;
24+
25+
/**
26+
* @param MockScopeManager $scopeManager
27+
* @param Span $span
28+
* @param bool $finishSpanOnClose
29+
*/
30+
public function __construct(
31+
MockScopeManager $scopeManager,
32+
Span $span,
33+
$finishSpanOnClose
34+
) {
2235
$this->scopeManager = $scopeManager;
2336
$this->span = $span;
37+
$this->finishSpanOnClose = $finishSpanOnClose;
2438
}
2539

2640
/**
2741
* {@inheritdoc}
2842
*/
2943
public function close()
3044
{
45+
if ($this->finishSpanOnClose) {
46+
$this->span->finish();
47+
}
48+
3149
$this->scopeManager->deactivate($this);
3250
}
3351

src/OpenTracing/Mock/MockScopeManager.php

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,11 @@ final class MockScopeManager implements ScopeManager
1616
/**
1717
* {@inheritdoc}
1818
*/
19-
public function activate(Span $span)
19+
public function activate(Span $span, $finishSpanOnClose = true)
2020
{
21-
$this->scopes[] = new MockScope($this, $span);
21+
$scope = new MockScope($this, $span, $finishSpanOnClose);
22+
$this->scopes[] = $scope;
23+
return $scope;
2224
}
2325

2426
/**
@@ -33,23 +35,6 @@ public function getActive()
3335
return $this->scopes[count($this->scopes) - 1];
3436
}
3537

36-
/**
37-
* {@inheritdoc}
38-
* @param Span|MockSpan $span
39-
*/
40-
public function getScope(Span $span)
41-
{
42-
$scopeLength = count($this->scopes);
43-
44-
for ($i = 0; $i < $scopeLength; $i++) {
45-
if ($span === $this->scopes[$i]->getSpan()) {
46-
return $this->scopes[$i];
47-
}
48-
}
49-
50-
return null;
51-
}
52-
5338
public function deactivate(MockScope $scope)
5439
{
5540
$scopeLength = count($this->scopes);

src/OpenTracing/Mock/MockSpan.php

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -38,28 +38,14 @@ final class MockSpan implements Span
3838
*/
3939
private $duration;
4040

41-
/**
42-
* @var bool
43-
*/
44-
private $closeOnFinish;
45-
46-
/**
47-
* @var ScopeManager
48-
*/
49-
private $scopeManager;
50-
5141
public function __construct(
52-
ScopeManager $scopeManager,
5342
$operationName,
5443
MockSpanContext $context,
55-
$startTime = null,
56-
$closeOnFinish = false
44+
$startTime = null
5745
) {
58-
$this->scopeManager = $scopeManager;
5946
$this->operationName = $operationName;
6047
$this->context = $context;
6148
$this->startTime = $startTime ?: time();
62-
$this->closeOnFinish = $closeOnFinish;
6349
}
6450

6551
/**
@@ -91,14 +77,6 @@ public function finish($finishTime = null)
9177
{
9278
$finishTime = ($finishTime ?: time());
9379
$this->duration = $finishTime - $this->startTime;
94-
95-
if (!$this->closeOnFinish) {
96-
return;
97-
}
98-
99-
if (($scope = $this->scopeManager->getScope($this)) !== null) {
100-
$scope->close();
101-
}
10280
}
10381

10482
public function isFinished()

src/OpenTracing/Mock/MockTracer.php

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use OpenTracing\Exceptions\UnsupportedFormat;
66
use OpenTracing\ScopeManager;
7-
use OpenTracing\SpanOptions;
7+
use OpenTracing\StartSpanOptions;
88
use OpenTracing\Tracer;
99
use OpenTracing\SpanContext;
1010

@@ -42,8 +42,8 @@ public function __construct(array $injectors = [], array $extractors = [])
4242
*/
4343
public function startActiveSpan($operationName, $options = [])
4444
{
45-
if (!($options instanceof SpanOptions)) {
46-
$options = SpanOptions::create($options);
45+
if (!($options instanceof StartSpanOptions)) {
46+
$options = StartSpanOptions::create($options);
4747
}
4848

4949
if (($activeSpan = $this->getActiveSpan()) !== null) {
@@ -52,18 +52,16 @@ public function startActiveSpan($operationName, $options = [])
5252

5353
$span = $this->startSpan($operationName, $options);
5454

55-
$this->scopeManager->activate($span);
56-
57-
return $span;
55+
return $this->scopeManager->activate($span, $options->shouldFinishSpanOnClose());
5856
}
5957

6058
/**
6159
* {@inheritdoc}
6260
*/
6361
public function startSpan($operationName, $options = [])
6462
{
65-
if (!($options instanceof SpanOptions)) {
66-
$options = SpanOptions::create($options);
63+
if (!($options instanceof StartSpanOptions)) {
64+
$options = StartSpanOptions::create($options);
6765
}
6866

6967
if (empty($options->getReferences())) {
@@ -73,7 +71,6 @@ public function startSpan($operationName, $options = [])
7371
}
7472

7573
$span = new MockSpan(
76-
$this->scopeManager,
7774
$operationName,
7875
$spanContext,
7976
$options->getStartTime()

src/OpenTracing/NoopScopeManager.php

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public static function create()
1212
/**
1313
* {@inheritdoc}
1414
*/
15-
public function activate(Span $span)
15+
public function activate(Span $span, $finishSpanOnClose)
1616
{
1717
}
1818

@@ -23,12 +23,4 @@ public function getActive()
2323
{
2424
return NoopScope::create();
2525
}
26-
27-
/**
28-
* {@inheritdoc}
29-
*/
30-
public function getScope(Span $span)
31-
{
32-
return NoopScope::create();
33-
}
3426
}

src/OpenTracing/NoopTracer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public function startSpan($operationName, $options = [])
3838
public function startActiveSpan($operationName, $finishSpanOnClose = true, $options = [])
3939
{
4040

41-
return NoopSpan::create();
41+
return NoopScope::create();
4242
}
4343

4444
/**

src/OpenTracing/ScopeManager.php

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,12 @@ interface ScopeManager
1313
* that previous spans can be resumed after a deactivation.
1414
*
1515
* @param Span $span the {@link Span} that should become the {@link #active()}
16+
* @param bool $finishSpanOnClose whether span should automatically be finished when {@link Scope#close()} is called
1617
*
17-
* Weather the span automatically closes when finish is called depends on
18-
* the span options set during the creation of the span. See
19-
* {@link SpanOptions#closeSpanOnFinish}
20-
*
21-
* @return Scope
18+
* @return Scope instance to control the end of the active period for the {@link Span}. It is a
19+
* programming error to neglect to call {@link Scope#close()} on the returned instance.
2220
*/
23-
public function activate(Span $span);
21+
public function activate(Span $span, $finishSpanOnClose);
2422

2523
/**
2624
* Return the currently active {@link Scope} which can be used to access the
@@ -31,16 +29,7 @@ public function activate(Span $span);
3129
* newly-created {@link Span} at {@link Tracer.SpanBuilder#startActive(boolean)} or {@link SpanBuilder#start()}
3230
* time rather than at {@link Tracer#buildSpan(String)} time.
3331
*
34-
* @return \OpenTracing\Scope|null
32+
* @return Scope|null
3533
*/
3634
public function getActive();
37-
38-
/**
39-
* Access the scope of a given Span if available.
40-
*
41-
* @param Span $span
42-
*
43-
* @return \OpenTracing\Scope|null
44-
*/
45-
public function getScope(Span $span);
4635
}

0 commit comments

Comments
 (0)