|
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 |
25 | 28 | { |
26 | 29 | return new PDO('sqlite::memory:'); |
27 | 30 | } |
28 | 31 |
|
| 32 | + private function createDBWithNewSubclass(): PDO |
| 33 | + { |
| 34 | + if (!class_exists('Pdo\Sqlite')) { |
| 35 | + $this->markTestSkipped('Pdo\Sqlite class is not available in this PHP version'); |
| 36 | + } |
| 37 | + |
| 38 | + /** @psalm-suppress UndefinedMethod */ |
| 39 | + return PDO::connect('sqlite::memory:'); |
| 40 | + } |
| 41 | + |
29 | 42 | private function fillDB():string |
30 | 43 | { |
31 | 44 | return <<<SQL |
@@ -72,6 +85,51 @@ public function test_pdo_construct(): void |
72 | 85 | $this->assertEquals('sqlite', $span->getAttributes()->get(TraceAttributes::DB_SYSTEM_NAME)); |
73 | 86 | } |
74 | 87 |
|
| 88 | + /** |
| 89 | + * @psalm-suppress UndefinedClass |
| 90 | + * @psalm-suppress InvalidClass |
| 91 | + */ |
| 92 | + public function test_pdo_sqlite_subclass(): void |
| 93 | + { |
| 94 | + // skip if php version is less than 8.4 |
| 95 | + if (version_compare(PHP_VERSION, '8.4', '<')) { |
| 96 | + $this->markTestSkipped('Pdo\Sqlite class is not available in this PHP version'); |
| 97 | + } |
| 98 | + |
| 99 | + $this->assertCount(0, $this->storage); |
| 100 | + |
| 101 | + /** |
| 102 | + * Need to suppress because of different casing of the class name |
| 103 | + * |
| 104 | + * @psalm-suppress UndefinedClass |
| 105 | + * @psalm-suppress InvalidClass |
| 106 | + * @var Pdo\Sqlite $db |
| 107 | + */ |
| 108 | + $db = self::createDBWithNewSubclass(); |
| 109 | + $this->assertCount(1, $this->storage); |
| 110 | + $span = $this->storage->offsetGet(0); |
| 111 | + $this->assertSame('PDO::connect', $span->getName()); |
| 112 | + $this->assertEquals('sqlite', $span->getAttributes()->get(TraceAttributes::DB_SYSTEM_NAME)); |
| 113 | + |
| 114 | + // Test that the subclass-specific methods work |
| 115 | + $db->createFunction('test_function', static fn ($value) => strtoupper($value)); |
| 116 | + |
| 117 | + // Test that standard PDO operations still work |
| 118 | + $db->exec($this->fillDB()); |
| 119 | + $span = $this->storage->offsetGet(1); |
| 120 | + $this->assertSame('PDO::exec', $span->getName()); |
| 121 | + $this->assertEquals('sqlite', $span->getAttributes()->get(TraceAttributes::DB_SYSTEM_NAME)); |
| 122 | + $this->assertCount(2, $this->storage); |
| 123 | + |
| 124 | + // Test that the custom function works |
| 125 | + $result = $db->query("SELECT test_function('hello')")->fetchColumn(); |
| 126 | + $this->assertEquals('HELLO', $result); |
| 127 | + $span = $this->storage->offsetGet(2); |
| 128 | + $this->assertSame('PDO::query', $span->getName()); |
| 129 | + $this->assertEquals('sqlite', $span->getAttributes()->get(TraceAttributes::DB_SYSTEM_NAME)); |
| 130 | + $this->assertCount(3, $this->storage); |
| 131 | + } |
| 132 | + |
75 | 133 | public function test_constructor_exception(): void |
76 | 134 | { |
77 | 135 | $this->expectException(\PDOException::class); |
@@ -214,4 +272,74 @@ public function test_encode_db_statement_as_utf8(): void |
214 | 272 | $this->assertTrue(mb_check_encoding($span_db_exec->getAttributes()->get(TraceAttributes::DB_QUERY_TEXT), 'UTF-8')); |
215 | 273 | $this->assertCount(5, $this->storage); |
216 | 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 | + } |
217 | 345 | } |
0 commit comments