Skip to content

Commit 701b882

Browse files
committed
chore: apply trace structure assertion to pdo test
1 parent 2552384 commit 701b882

File tree

2 files changed

+64
-3
lines changed

2 files changed

+64
-3
lines changed

src/Instrumentation/PDO/composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
"phpstan/phpstan-phpunit": "^1.0",
2828
"psalm/plugin-phpunit": "^0.19.2",
2929
"open-telemetry/sdk": "^1.0",
30+
"open-telemetry/test-utils": "^0.2.0",
3031
"phpunit/phpunit": "^9.5",
3132
"vimeo/psalm": "6.4.0"
3233
},

src/Instrumentation/PDO/tests/Integration/PDOInstrumentationTest.php

Lines changed: 63 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,14 @@
1515
use OpenTelemetry\SDK\Trace\SpanProcessor\SimpleSpanProcessor;
1616
use OpenTelemetry\SDK\Trace\TracerProvider;
1717
use OpenTelemetry\SemConv\TraceAttributes;
18+
use OpenTelemetry\TestUtils\TraceStructureAssertionTrait;
1819
use PDO;
1920
use PHPUnit\Framework\TestCase;
2021

2122
class 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

Comments
 (0)