1515use OpenTelemetry \SDK \Trace \SpanProcessor \SimpleSpanProcessor ;
1616use OpenTelemetry \SDK \Trace \TracerProvider ;
1717use OpenTelemetry \SemConv \TraceAttributes ;
18+ use OpenTelemetry \TestUtils \TraceStructureAssertionTrait ;
1819use PDO ;
1920use PHPUnit \Framework \TestCase ;
2021
2122class PDOInstrumentationTest extends TestCase
2223{
24+ use TraceStructureAssertionTrait;
25+
2326 private ScopeInterface $ scope ;
2427 /** @var ArrayObject<array-key, mixed> */
2528 private ArrayObject $ storage ;
@@ -113,7 +116,7 @@ public function test_pdo_sqlite_subclass(): void
113116
114117 // Test that the subclass-specific methods work
115118 $ db ->createFunction ('test_function ' , static fn ($ value ) => strtoupper ($ value ));
116-
119+
117120 // Test that standard PDO operations still work
118121 $ db ->exec ($ this ->fillDB ());
119122 $ span = $ this ->storage ->offsetGet (1 );
@@ -288,7 +291,7 @@ public function test_span_hierarchy_with_pdo_operations(): void
288291 $ serverSpan = $ tracer ->spanBuilder ('HTTP GET /api/users ' )
289292 ->setSpanKind (SpanKind::KIND_SERVER )
290293 ->startSpan ();
291-
294+
292295 // Create scope for server span
293296 $ serverScope = Context::storage ()->attach ($ serverSpan ->storeInContext (Context::getCurrent ()));
294297
@@ -297,7 +300,7 @@ public function test_span_hierarchy_with_pdo_operations(): void
297300 $ internalSpan = $ tracer ->spanBuilder ('processUserData ' )
298301 ->setSpanKind (SpanKind::KIND_INTERNAL )
299302 ->startSpan ();
300-
303+
301304 // Create scope for internal span
302305 $ internalScope = Context::storage ()->attach ($ internalSpan ->storeInContext (Context::getCurrent ()));
303306
@@ -340,6 +343,63 @@ public function test_span_hierarchy_with_pdo_operations(): void
340343
341344 // Detach scopes
342345 $ internalScope ->detach ();
346+ $ internalSpan ->end ();
343347 $ serverScope ->detach ();
348+ $ serverSpan ->end ();
349+
350+ $ this ->assertTraceStructure (
351+ $ this ->storage ,
352+ [
353+ [
354+ 'name ' => 'HTTP GET /api/users ' ,
355+ 'kind ' => SpanKind::KIND_SERVER ,
356+ 'attributes ' => [],
357+ 'children ' => [
358+ [
359+ 'name ' => 'processUserData ' ,
360+ 'kind ' => SpanKind::KIND_INTERNAL ,
361+ 'attributes ' => [],
362+ 'children ' => [
363+ [
364+ 'name ' => 'PDO::__construct ' ,
365+ 'kind ' => SpanKind::KIND_CLIENT ,
366+ 'attributes ' => [
367+ TraceAttributes::DB_SYSTEM_NAME => 'sqlite ' ,
368+ ],
369+ ],
370+ [
371+ 'name ' => 'PDO::exec ' ,
372+ 'kind ' => SpanKind::KIND_CLIENT ,
373+ 'attributes ' => [
374+ TraceAttributes::DB_SYSTEM_NAME => 'sqlite ' ,
375+ ],
376+ ],
377+ [
378+ 'name ' => 'PDO::prepare ' ,
379+ 'kind ' => SpanKind::KIND_CLIENT ,
380+ 'attributes ' => [
381+ TraceAttributes::DB_SYSTEM_NAME => 'sqlite ' ,
382+ ],
383+ ],
384+ [
385+ 'name ' => 'PDOStatement::execute ' ,
386+ 'kind ' => SpanKind::KIND_CLIENT ,
387+ 'attributes ' => [
388+ TraceAttributes::DB_SYSTEM_NAME => 'sqlite ' ,
389+ ],
390+ ],
391+ [
392+ 'name ' => 'PDOStatement::fetchAll ' ,
393+ 'kind ' => SpanKind::KIND_CLIENT ,
394+ 'attributes ' => [
395+ TraceAttributes::DB_SYSTEM_NAME => 'sqlite ' ,
396+ ],
397+ ],
398+ ],
399+ ],
400+ ],
401+ ],
402+ ]
403+ );
344404 }
345405}
0 commit comments