|
6 | 6 |
|
7 | 7 | use ArrayObject; |
8 | 8 | use OpenTelemetry\API\Instrumentation\Configurator; |
| 9 | +use OpenTelemetry\API\Trace\SpanInterface; |
| 10 | +use OpenTelemetry\API\Trace\SpanKind; |
| 11 | +use OpenTelemetry\Context\Context; |
9 | 12 | use OpenTelemetry\Context\ScopeInterface; |
10 | 13 | use OpenTelemetry\SDK\Trace\ImmutableSpan; |
11 | 14 | use OpenTelemetry\SDK\Trace\SpanExporter\InMemoryExporter; |
|
18 | 21 | class PDOInstrumentationTest extends TestCase |
19 | 22 | { |
20 | 23 | private ScopeInterface $scope; |
21 | | - /** @var ArrayObject<int, ImmutableSpan> */ |
| 24 | + /** @var ArrayObject<array-key, mixed> */ |
22 | 25 | private ArrayObject $storage; |
23 | 26 |
|
24 | 27 | private function createDB(): PDO |
@@ -269,4 +272,74 @@ public function test_encode_db_statement_as_utf8(): void |
269 | 272 | $this->assertTrue(mb_check_encoding($span_db_exec->getAttributes()->get(TraceAttributes::DB_QUERY_TEXT), 'UTF-8')); |
270 | 273 | $this->assertCount(5, $this->storage); |
271 | 274 | } |
| 275 | + |
| 276 | + public function test_span_hierarchy_with_pdo_operations(): void |
| 277 | + { |
| 278 | + $this->assertCount(0, $this->storage); |
| 279 | + |
| 280 | + // Create a server span |
| 281 | + $tracerProvider = new TracerProvider( |
| 282 | + new SimpleSpanProcessor( |
| 283 | + new InMemoryExporter($this->storage) |
| 284 | + ) |
| 285 | + ); |
| 286 | + $tracer = $tracerProvider->getTracer('test'); |
| 287 | + /** @var SpanInterface $serverSpan */ |
| 288 | + $serverSpan = $tracer->spanBuilder('HTTP GET /api/users') |
| 289 | + ->setSpanKind(SpanKind::KIND_SERVER) |
| 290 | + ->startSpan(); |
| 291 | + |
| 292 | + // Create scope for server span |
| 293 | + $serverScope = Context::storage()->attach($serverSpan->storeInContext(Context::getCurrent())); |
| 294 | + |
| 295 | + // Create an internal span (simulating business logic) |
| 296 | + /** @var SpanInterface $internalSpan */ |
| 297 | + $internalSpan = $tracer->spanBuilder('processUserData') |
| 298 | + ->setSpanKind(SpanKind::KIND_INTERNAL) |
| 299 | + ->startSpan(); |
| 300 | + |
| 301 | + // Create scope for internal span |
| 302 | + $internalScope = Context::storage()->attach($internalSpan->storeInContext(Context::getCurrent())); |
| 303 | + |
| 304 | + // Perform PDO operations within the internal span context |
| 305 | + $db = self::createDB(); |
| 306 | + $this->assertCount(1, $this->storage); // PDO constructor span |
| 307 | + |
| 308 | + // Create and populate test table |
| 309 | + $db->exec($this->fillDB()); |
| 310 | + $this->assertCount(2, $this->storage); // PDO exec span |
| 311 | + |
| 312 | + // Query data |
| 313 | + $stmt = $db->prepare('SELECT * FROM technology WHERE name = ?'); |
| 314 | + $this->assertCount(3, $this->storage); // PDO prepare span |
| 315 | + |
| 316 | + $stmt->execute(['PHP']); |
| 317 | + $this->assertCount(4, $this->storage); // PDOStatement execute span |
| 318 | + |
| 319 | + $result = $stmt->fetchAll(); |
| 320 | + $this->assertCount(5, $this->storage); // PDOStatement fetchAll span |
| 321 | + |
| 322 | + // Verify span hierarchy |
| 323 | + /** @var ImmutableSpan $pdoSpan */ |
| 324 | + $pdoSpan = $this->storage->offsetGet(0); |
| 325 | + /** @var ImmutableSpan $execSpan */ |
| 326 | + $execSpan = $this->storage->offsetGet(1); |
| 327 | + /** @var ImmutableSpan $prepareSpan */ |
| 328 | + $prepareSpan = $this->storage->offsetGet(2); |
| 329 | + /** @var ImmutableSpan $executeSpan */ |
| 330 | + $executeSpan = $this->storage->offsetGet(3); |
| 331 | + /** @var ImmutableSpan $fetchAllSpan */ |
| 332 | + $fetchAllSpan = $this->storage->offsetGet(4); |
| 333 | + |
| 334 | + // All PDO spans should be children of the internal span |
| 335 | + $this->assertEquals($internalSpan->getContext()->getSpanId(), $pdoSpan->getParentSpanId()); |
| 336 | + $this->assertEquals($internalSpan->getContext()->getSpanId(), $execSpan->getParentSpanId()); |
| 337 | + $this->assertEquals($internalSpan->getContext()->getSpanId(), $prepareSpan->getParentSpanId()); |
| 338 | + $this->assertEquals($internalSpan->getContext()->getSpanId(), $executeSpan->getParentSpanId()); |
| 339 | + $this->assertEquals($internalSpan->getContext()->getSpanId(), $fetchAllSpan->getParentSpanId()); |
| 340 | + |
| 341 | + // Detach scopes |
| 342 | + $internalScope->detach(); |
| 343 | + $serverScope->detach(); |
| 344 | + } |
272 | 345 | } |
0 commit comments