Debian Codegraph @

tests/phpunit/unit/includes/libs/telemetry/TracerTest.php

raw symbols SCIP
1 <?php
2 namespace Wikimedia\Tests\Telemetry;
3
4 use MediaWikiUnitTestCase;
5 use Wikimedia\Telemetry\Clock;
6 use Wikimedia\Telemetry\ExporterInterface;
7 use Wikimedia\Telemetry\NoopSpan;
8 use Wikimedia\Telemetry\SamplerInterface;
9 use Wikimedia\Telemetry\SpanContext;
10 use Wikimedia\Telemetry\SpanInterface;
11 use Wikimedia\Telemetry\Tracer;
12 use Wikimedia\Telemetry\TracerState;
13
14 /**
15 * @covers \Wikimedia\Telemetry\Tracer
16 * @covers \Wikimedia\Telemetry\Span
17 */
18 class TracerTest extends MediaWikiUnitTestCase {
19 private Clock $clock;
20 private SamplerInterface $sampler;
21 private ExporterInterface $exporter;
22 private TracerState $tracerState;
23
24 private Tracer $tracer;
25
26 protected function setUp(): void {
27 parent::setUp();
28
29 $this->clock = $this->createMock( Clock::class );
30 $this->sampler = $this->createMock( SamplerInterface::class );
31 $this->exporter = $this->createMock( ExporterInterface::class );
32 $this->tracerState = new TracerState();
33
34 $this->clock->method( 'getCurrentNanoTime' )
35 ->willReturnCallback( fn () => hrtime( true ) );
36
37 $this->tracer = new Tracer(
38 $this->clock,
39 $this->sampler,
40 $this->exporter,
41 $this->tracerState
42 );
43 }
44
45 public function testSpanAndTraceIds(): void {
46 $this->sampler->method( 'shouldSample' )
47 ->willReturn( true );
48
49 $rootSpan = $this->tracer->createRootSpan( 'test span' )
50 ->start();
51 $rootSpan->activate();
52
53 $span = $this->tracer->createSpan( 'test' )
54 ->start();
55
56 $explicitSpan = $this->tracer->createSpanWithParent( 'test', $span->getContext() )
57 ->start();
58
59 foreach ( [ $rootSpan, $span, $explicitSpan ] as $span ) {
60 $this->assertMatchesRegularExpression(
61 '/^[a-f0-9]{32}$/',
62 $span->getContext()->getTraceId(),
63 'The trace ID should be a 128-bit hexadecimal string'
64 );
65 $this->assertMatchesRegularExpression(
66 '/^[a-f0-9]{16}$/',
67 $span->getContext()->getSpanId(),
68 'The span ID should be a 64-bit hexadecimal string'
69 );
70 }
71 }
72
73 public function testSpanCreation(): void {
74 $this->sampler->method( 'shouldSample' )
75 ->willReturn( true );
76
77 $activeSpan = $this->tracer->createRootSpan( 'test active span' )
78 ->start();
79 $activeSpan->activate();
80
81 $span = $this->tracer->createRootSpan( 'test span', false )
82 ->start();
83
84 $this->assertNull(
85 $span->getContext()->getParentSpanId(),
86 'The test span should have been explicitly created as a root span, ignoring the active span'
87 );
88 $this->assertNotEquals(
89 $activeSpan->getContext()->getTraceId(),
90 $span->getContext()->getTraceId(),
91 'The test span should have started a new trace because it is a root span'
92 );
93 }
94
95 public function testMultipleSpanCreation(): void {
96 $this->sampler->method( 'shouldSample' )
97 ->willReturn( true );
98
99 /** @var SpanInterface[] $spans */
100 $spans = [];
101 $spanIds = [];
102 for ( $i = 1; $i <= 4; $i++ ) {
103 $span = $i === 1 ? $this->tracer->createRootSpan( "test span #$i" ) :
104 $this->tracer->createSpan( "test span #$i" );
105 $span = $span->start();
106 $span->activate();
107
108 if ( $i === 1 ) {
109 $this->assertNull(
110 $span->getContext()->getParentSpanId(),
111 'The first span should have no parent, because there is no active span'
112 );
113 } else {
114 $this->assertSame(
115 $spans[$i - 2]->getContext()->getSpanId(),
116 $span->getContext()->getParentSpanId(),
117 "The parent of span #$i should have been the previous span, which was active"
118 );
119 $this->assertSame(
120 $spans[0]->getContext()->getTraceId(),
121 $span->getContext()->getTraceId(),
122 'All spans should be part of the same trace'
123 );
124 }
125
126 $spans[] = $span;
127 $spanIds[] = $span->getContext()->getSpanId();
128 }
129
130 $span = null;
131
132 while ( array_pop( $spans ) !== null );
133
134 $exportedSpanIds = array_map(
135 fn ( SpanContext $spanContext ) => $spanContext->getSpanId(),
136 $this->tracerState->getSpanContexts()
137 );
138
139 $this->assertSame(
140 array_reverse( $spanIds ),
141 $exportedSpanIds
142 );
143 }
144
145 public function testCreatingSpansWithoutActiveSpan(): void {
146 $this->clock->method( 'getCurrentNanoTime' )
147 ->willReturnCallback( fn () => hrtime( true ) );
148
149 $this->sampler->method( 'shouldSample' )
150 ->willReturn( true );
151
152 $traceIds = [];
153
154 for ( $i = 1; $i <= 2; $i++ ) {
155 $span = $this->tracer->createRootSpan( "test span #$i" )
156 ->start();
157
158 $this->assertNull( $span->getContext()->getParentSpanId(), "span #$i should have no parent" );
159 $this->assertNotContains(
160 $span->getContext()->getTraceId(),
161 $traceIds,
162 'All spans should be root spans, starting a new trace'
163 );
164
165 $traceIds[] = $span->getContext()->getTraceId();
166 }
167 }
168
169 public function testCreatingSpanWithExplicitParent(): void {
170 $this->sampler->method( 'shouldSample' )
171 ->willReturn( true );
172
173 $activeSpan = $this->tracer->createRootSpan( 'test active span' )
174 ->start();
175
176 $parentSpan = $this->tracer->createRootSpan( 'parent span' )
177 ->start();
178
179 $span = $this->tracer->createSpanWithParent( 'test span', $parentSpan->getContext() )
180 ->start();
181
182 $this->assertSame(
183 $parentSpan->getContext()->getSpanId(),
184 $span->getContext()->getParentSpanId(),
185 'The test span should have been assigned the given span as the parent, ignoring the active span'
186 );
187 $this->assertSame(
188 $parentSpan->getContext()->getTraceId(),
189 $span->getContext()->getTraceId(),
190 'The test span should have been part of the same trace as its parent'
191 );
192 }
193
194 public function testShouldExportSharedStateOnShutdown(): void {
195 $this->exporter->expects( $this->once() )
196 ->method( 'export' )
197 ->with( $this->tracerState );
198
199 $this->tracer->shutdown();
200 }
201
202 public function testShouldMakeSpanCreationNoopPostShutdown(): void {
203 $this->sampler->method( 'shouldSample' )
204 ->willReturn( true );
205
206 $this->tracer->shutdown();
207
208 $orphanedSpan = $this->tracer->createSpan( 'orphaned span' )
209 ->start();
210
211 $rootSpan = $this->tracer->createRootSpan( 'parent span' )
212 ->start();
213
214 $span = $this->tracer->createSpan( 'orphaned span' )
215 ->start();
216
217 $explicitParentSpan = $this->tracer->createSpanWithParent( 'test span', $orphanedSpan->getContext() )
218 ->start();
219
220 $this->assertInstanceOf( NoopSpan::class, $orphanedSpan );
221 $this->assertInstanceOf( NoopSpan::class, $rootSpan );
222 $this->assertInstanceOf( NoopSpan::class, $span );
223 $this->assertInstanceOf( NoopSpan::class, $explicitParentSpan );
224 }
225 }