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
|
<?php
namespace Wikimedia\Tests\Telemetry;
use GuzzleHttp\Client;
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\Psr7\Response;
use MediaWiki\MainConfigNames;
use MediaWikiIntegrationTestCase;
use Wikimedia\Telemetry\Clock;
use Wikimedia\Telemetry\NoopTracer;
use Wikimedia\Telemetry\SpanInterface;
use Wikimedia\Telemetry\Tracer;
use Wikimedia\Telemetry\TracerState;
/**
* @covers \Wikimedia\Telemetry\Tracer
* @covers \Wikimedia\Telemetry\OtlpHttpExporter
*/
class TelemetryIntegrationTest extends MediaWikiIntegrationTestCase {
private const EXAMPLE_TRACING_CONFIG = [
'serviceName' => 'test-service',
'samplingProbability' => 100,
'endpoint' => 'http://198.51.100.42:4318/v1/traces'
];
private const EXAMPLE_TRACING_CONFIG_NO_SAMPLING = [
'serviceName' => 'test-service',
'samplingProbability' => 0,
'endpoint' => 'http://198.51.100.42:4318/v1/traces'
];
private MockHandler $handler;
protected function setUp(): void {
parent::setUp();
$this->handler = new MockHandler();
$this->setService( '_TracerHTTPClient', new Client( [
'handler' => $this->handler,
'http_errors' => false
] ) );
}
protected function tearDown(): void {
parent::tearDown();
Clock::setMockTime( null );
TracerState::destroyInstance();
}
public function testShouldDoNothingWhenTracingDisabled(): void {
$this->overrideConfigValue( MainConfigNames::OpenTelemetryConfig, null );
$tracer = $this->getServiceContainer()->getTracer();
$span = $tracer->createSpan( 'test' )
->start();
$span->end();
$tracer->shutdown();
$this->assertInstanceOf( NoopTracer::class, $tracer );
$this->assertNull( $this->handler->getLastRequest() );
}
public function testShouldDoNothingWhenNotSampled(): void {
$this->overrideConfigValue(
MainConfigNames::OpenTelemetryConfig,
self::EXAMPLE_TRACING_CONFIG_NO_SAMPLING
);
$tracer = $this->getServiceContainer()->getTracer();
$span = $tracer->createRootSpan( 'test' )
->start();
$span->activate();
$child = $tracer->createSpan( 'child' )
->start();
$child->end();
$span->end();
$tracer->shutdown();
$this->assertInstanceOf( Tracer::class, $tracer );
$this->assertNull( $this->handler->getLastRequest() );
}
public function testShouldNotExportDataWhenNoSpansWereCreated(): void {
$this->overrideConfigValue( MainConfigNames::OpenTelemetryConfig, self::EXAMPLE_TRACING_CONFIG );
$tracer = $this->getServiceContainer()->getTracer();
$tracer->shutdown();
$this->assertInstanceOf( Tracer::class, $tracer );
$this->assertNull( $this->handler->getLastRequest() );
}
public function testShouldNotExportDataWhenTracerWasNotExplicitlyShutdown(): void {
$this->overrideConfigValue( MainConfigNames::OpenTelemetryConfig, self::EXAMPLE_TRACING_CONFIG );
$tracer = $this->getServiceContainer()->getTracer();
$span = $tracer->createRootSpan( 'test' )
->start();
$span->end();
$this->assertInstanceOf( Tracer::class, $tracer );
$this->assertNull( $this->handler->getLastRequest() );
}
public function testShouldExportDataOnShutdownWhenTracingEnabled(): void {
$this->overrideConfigValue( MainConfigNames::OpenTelemetryConfig, self::EXAMPLE_TRACING_CONFIG );
$this->handler->append( new Response( 200 ) );
$mockTime = 5481675965496;
Clock::setMockTime( $mockTime );
$tracer = $this->getServiceContainer()->getTracer();
$span = $tracer->createRootSpan( 'test' )
->setSpanKind( SpanInterface::SPAN_KIND_SERVER )
->start();
$span->activate();
$mockTime += 100;
Clock::setMockTime( $mockTime );
$childSpan = $tracer->createSpan( 'child' )
->setAttributes( [ 'some-key' => 'test', 'ignored' => new \stdClass() ] )
->start();
$mockTime += 250;
Clock::setMockTime( $mockTime );
$childSpan->end();
$mockTime += 74;
Clock::setMockTime( $mockTime );
$span->setSpanStatus( SpanInterface::SPAN_STATUS_ERROR );
$span->end();
$this->assertNull(
$this->handler->getLastRequest(),
'Exporting trace data should be deferred until the tracer is explicitly shut down'
);
$tracer->shutdown();
$request = $this->handler->getLastRequest();
$this->assertInstanceOf( Tracer::class, $tracer );
$this->assertSame( 'http://198.51.100.42:4318/v1/traces', (string)$request->getUri() );
$this->assertSame( 'application/json', $request->getHeaderLine( 'Content-Type' ) );
$expected = file_get_contents( __DIR__ . '/expected-trace-data.json' );
$expected = strtr( $expected, [
'<TRACE-ID>' => $span->getContext()->getTraceId(),
'<SPAN-1-ID>' => $span->getContext()->getSpanId(),
'<SPAN-2-ID>' => $childSpan->getContext()->getSpanId(),
'<HOST-NAME>' => wfHostname()
] );
$this->assertJsonStringEqualsJsonString(
$expected,
(string)$request->getBody()
);
}
}
|