Skip to content

Commit 6a14cf9

Browse files
authored
Tracer references tests (#759)
* adding tests for tracer provider behaviour a recent bug highlighted that if there is no reference to a tracer provider, then the shared state will shutdown, even if that shared state is being used by active tracers. Adding a phpdoc comment explaining this behavior, and some tests to demonstrate it * moving tests into integration * removing accidental covers annotation
1 parent 305c1c7 commit 6a14cf9

File tree

3 files changed

+56
-3
lines changed

3 files changed

+56
-3
lines changed

examples/traces/demo/src/index.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,12 @@
7070

7171
//middleware starts root span based on route pattern, sets status from http code
7272
$app->add(function (Request $request, RequestHandler $handler) use ($tracer) {
73-
$carrier = TraceContextPropagator::getInstance()->extract($request->getHeaders());
73+
$parent = TraceContextPropagator::getInstance()->extract($request->getHeaders());
7474
$routeContext = RouteContext::fromRequest($request);
7575
$route = $routeContext->getRoute();
7676
$root = $tracer->spanBuilder($route->getPattern())
7777
->setStartTimestamp((int) ($request->getServerParams()['REQUEST_TIME_FLOAT'] * 1e9))
78-
->setParent($carrier)
78+
->setParent($parent)
7979
->setSpanKind(SpanKind::KIND_SERVER)
8080
->startSpan();
8181
$root->activate();

src/SDK/Trace/TracerProvider.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,11 @@ public function forceFlush(): bool
6363
return $this->tracerSharedState->getSpanProcessor()->forceFlush();
6464
}
6565

66-
/** @inheritDoc */
66+
/**
67+
* @inheritDoc
68+
* @note Getting a tracer without keeping a strong reference to the TracerProvider will cause the TracerProvider to
69+
* immediately shut itself down including its shared state, ie don't do this: $tracer = (new TracerProvider())->getTracer('foo')
70+
*/
6771
public function getTracer(
6872
string $name,
6973
?string $version = null,
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace OpenTelemetry\Tests\Integration\SDK;
6+
7+
use OpenTelemetry\SDK\Trace\SpanProcessorInterface;
8+
use OpenTelemetry\SDK\Trace\TracerProvider;
9+
use PHPUnit\Framework\TestCase;
10+
use Prophecy\Prophet;
11+
12+
/**
13+
* @coversNothing
14+
*/
15+
class TracerProviderTest extends TestCase
16+
{
17+
/**
18+
* @doesNotPerformAssertions
19+
*/
20+
public function test_tracer_shuts_down_immediately_when_out_of_scope(): void
21+
{
22+
$prophet = new Prophet();
23+
$spanProcessor = $prophet->prophesize(SpanProcessorInterface::class);
24+
// @phpstan-ignore-next-line
25+
$spanProcessor->shutdown()->shouldBeCalledTimes(1);
26+
27+
/* Because no reference is kept to the TracerProvider, it will immediately __destruct and shutdown,
28+
which will also shut down span processors in shared state. */
29+
$tracer = (new TracerProvider($spanProcessor->reveal()))->getTracer('test');
30+
31+
$spanProcessor->checkProphecyMethodsPredictions();
32+
}
33+
34+
/**
35+
* @doesNotPerformAssertions
36+
*/
37+
public function test_tracer_remains_in_scope(): void
38+
{
39+
$prophet = new Prophet();
40+
$spanProcessor = $prophet->prophesize(SpanProcessorInterface::class);
41+
// @phpstan-ignore-next-line
42+
$spanProcessor->shutdown()->shouldBeCalledTimes(0);
43+
44+
$tracerProvider = new TracerProvider($spanProcessor->reveal());
45+
$tracer = $tracerProvider->getTracer('test');
46+
47+
$spanProcessor->checkProphecyMethodsPredictions();
48+
}
49+
}

0 commit comments

Comments
 (0)