File: TracerTest.php

package info (click to toggle)
mediawiki 1%3A1.43.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 417,464 kB
  • sloc: php: 1,062,949; javascript: 664,290; sql: 9,714; python: 5,458; xml: 3,489; sh: 1,131; makefile: 64
file content (225 lines) | stat: -rw-r--r-- 6,330 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
<?php
namespace Wikimedia\Tests\Telemetry;

use MediaWikiUnitTestCase;
use Wikimedia\Telemetry\Clock;
use Wikimedia\Telemetry\ExporterInterface;
use Wikimedia\Telemetry\NoopSpan;
use Wikimedia\Telemetry\SamplerInterface;
use Wikimedia\Telemetry\SpanContext;
use Wikimedia\Telemetry\SpanInterface;
use Wikimedia\Telemetry\Tracer;
use Wikimedia\Telemetry\TracerState;

/**
 * @covers \Wikimedia\Telemetry\Tracer
 * @covers \Wikimedia\Telemetry\Span
 */
class TracerTest extends MediaWikiUnitTestCase {
	private Clock $clock;
	private SamplerInterface $sampler;
	private ExporterInterface $exporter;
	private TracerState $tracerState;

	private Tracer $tracer;

	protected function setUp(): void {
		parent::setUp();

		$this->clock = $this->createMock( Clock::class );
		$this->sampler = $this->createMock( SamplerInterface::class );
		$this->exporter = $this->createMock( ExporterInterface::class );
		$this->tracerState = new TracerState();

		$this->clock->method( 'getCurrentNanoTime' )
			->willReturnCallback( fn () => hrtime( true ) );

		$this->tracer = new Tracer(
			$this->clock,
			$this->sampler,
			$this->exporter,
			$this->tracerState
		);
	}

	public function testSpanAndTraceIds(): void {
		$this->sampler->method( 'shouldSample' )
			->willReturn( true );

		$rootSpan = $this->tracer->createRootSpan( 'test span' )
			->start();
		$rootSpan->activate();

		$span = $this->tracer->createSpan( 'test' )
			->start();

		$explicitSpan = $this->tracer->createSpanWithParent( 'test', $span->getContext() )
			->start();

		foreach ( [ $rootSpan, $span, $explicitSpan ] as $span ) {
			$this->assertMatchesRegularExpression(
				'/^[a-f0-9]{32}$/',
				$span->getContext()->getTraceId(),
				'The trace ID should be a 128-bit hexadecimal string'
			);
			$this->assertMatchesRegularExpression(
				'/^[a-f0-9]{16}$/',
				$span->getContext()->getSpanId(),
				'The span ID should be a 64-bit hexadecimal string'
			);
		}
	}

	public function testSpanCreation(): void {
		$this->sampler->method( 'shouldSample' )
			->willReturn( true );

		$activeSpan = $this->tracer->createRootSpan( 'test active span' )
			->start();
		$activeSpan->activate();

		$span = $this->tracer->createRootSpan( 'test span', false )
			->start();

		$this->assertNull(
			$span->getContext()->getParentSpanId(),
			'The test span should have been explicitly created as a root span, ignoring the active span'
		);
		$this->assertNotEquals(
			$activeSpan->getContext()->getTraceId(),
			$span->getContext()->getTraceId(),
			'The test span should have started a new trace because it is a root span'
		);
	}

	public function testMultipleSpanCreation(): void {
		$this->sampler->method( 'shouldSample' )
			->willReturn( true );

		/** @var SpanInterface[] $spans */
		$spans = [];
		$spanIds = [];
		for ( $i = 1; $i <= 4; $i++ ) {
			$span = $i === 1 ? $this->tracer->createRootSpan( "test span #$i" ) :
				$this->tracer->createSpan( "test span #$i" );
			$span = $span->start();
			$span->activate();

			if ( $i === 1 ) {
				$this->assertNull(
					$span->getContext()->getParentSpanId(),
					'The first span should have no parent, because there is no active span'
				);
			} else {
				$this->assertSame(
					$spans[$i - 2]->getContext()->getSpanId(),
					$span->getContext()->getParentSpanId(),
					"The parent of span #$i should have been the previous span, which was active"
				);
				$this->assertSame(
					$spans[0]->getContext()->getTraceId(),
					$span->getContext()->getTraceId(),
					'All spans should be part of the same trace'
				);
			}

			$spans[] = $span;
			$spanIds[] = $span->getContext()->getSpanId();
		}

		$span = null;

		while ( array_pop( $spans ) !== null );

		$exportedSpanIds = array_map(
			fn ( SpanContext $spanContext ) => $spanContext->getSpanId(),
			$this->tracerState->getSpanContexts()
		);

		$this->assertSame(
			array_reverse( $spanIds ),
			$exportedSpanIds
		);
	}

	public function testCreatingSpansWithoutActiveSpan(): void {
		$this->clock->method( 'getCurrentNanoTime' )
			->willReturnCallback( fn () => hrtime( true ) );

		$this->sampler->method( 'shouldSample' )
			->willReturn( true );

		$traceIds = [];

		for ( $i = 1; $i <= 2; $i++ ) {
			$span = $this->tracer->createRootSpan( "test span #$i" )
				->start();

			$this->assertNull( $span->getContext()->getParentSpanId(), "span #$i should have no parent" );
			$this->assertNotContains(
				$span->getContext()->getTraceId(),
				$traceIds,
				'All spans should be root spans, starting a new trace'
			);

			$traceIds[] = $span->getContext()->getTraceId();
		}
	}

	public function testCreatingSpanWithExplicitParent(): void {
		$this->sampler->method( 'shouldSample' )
			->willReturn( true );

		$activeSpan = $this->tracer->createRootSpan( 'test active span' )
			->start();

		$parentSpan = $this->tracer->createRootSpan( 'parent span' )
			->start();

		$span = $this->tracer->createSpanWithParent( 'test span', $parentSpan->getContext() )
			->start();

		$this->assertSame(
			$parentSpan->getContext()->getSpanId(),
			$span->getContext()->getParentSpanId(),
			'The test span should have been assigned the given span as the parent, ignoring the active span'
		);
		$this->assertSame(
			$parentSpan->getContext()->getTraceId(),
			$span->getContext()->getTraceId(),
			'The test span should have been part of the same trace as its parent'
		);
	}

	public function testShouldExportSharedStateOnShutdown(): void {
		$this->exporter->expects( $this->once() )
			->method( 'export' )
			->with( $this->tracerState );

		$this->tracer->shutdown();
	}

	public function testShouldMakeSpanCreationNoopPostShutdown(): void {
		$this->sampler->method( 'shouldSample' )
			->willReturn( true );

		$this->tracer->shutdown();

		$orphanedSpan = $this->tracer->createSpan( 'orphaned span' )
			->start();

		$rootSpan = $this->tracer->createRootSpan( 'parent span' )
			->start();

		$span = $this->tracer->createSpan( 'orphaned span' )
			->start();

		$explicitParentSpan = $this->tracer->createSpanWithParent( 'test span', $orphanedSpan->getContext() )
			->start();

		$this->assertInstanceOf( NoopSpan::class, $orphanedSpan );
		$this->assertInstanceOf( NoopSpan::class, $rootSpan );
		$this->assertInstanceOf( NoopSpan::class, $span );
		$this->assertInstanceOf( NoopSpan::class, $explicitParentSpan );
	}
}