Skip to content

Commit 8724759

Browse files
authored
fix doctrine statement execution error (open-telemetry#399)
the post callback function signature was incorrect for the case where an exception occurs, ResultInterface should be nullable
1 parent cddcfcc commit 8724759

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

src/Instrumentation/Doctrine/src/DoctrineInstrumentation.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ public static function register(): void
180180

181181
Context::storage()->attach($span->storeInContext($parent));
182182
},
183-
post: static function (\Doctrine\DBAL\Driver\Statement $statement, array $params, ResultInterface $result, ?Throwable $exception) {
183+
post: static function (\Doctrine\DBAL\Driver\Statement $statement, array $params, ?ResultInterface $result, ?Throwable $exception) {
184184
self::end($exception);
185185
}
186186
);

src/Instrumentation/Doctrine/tests/Integration/DoctrineInstrumentationTest.php

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ public function test_connection_exception(): void
9595
]);
9696
}
9797

98-
public function test_statement_execution(): void
98+
public function test_connection_execute_statement(): void
9999
{
100100
$connection = self::createConnection();
101101
$statement = self::fillDB();
@@ -202,4 +202,39 @@ public function test_transaction(): void
202202
$sth = $connection->prepare('SELECT * FROM `technology`');
203203
$this->assertSame(2, count($sth->executeQuery()->fetchAllAssociative()));
204204
}
205+
206+
public function test_statement_execute(): void
207+
{
208+
$connection = self::createConnection();
209+
$statement = self::fillDB();
210+
$connection->executeStatement($statement);
211+
$stmt = $connection->prepare('SELECT * FROM `technology`');
212+
$this->storage->exchangeArray([]);
213+
$stmt->executeQuery();
214+
$this->assertCount(1, $this->storage);
215+
$span = $this->storage->offsetGet(0);
216+
$this->assertSame('Doctrine::execute', $span->getName());
217+
$this->assertSame('execute', $span->getAttributes()->get(TraceAttributes::DB_OPERATION_NAME));
218+
}
219+
220+
public function test_statement_execute_error(): void
221+
{
222+
$connection = self::createConnection();
223+
$statement = self::fillDB();
224+
$connection->executeStatement($statement);
225+
$stmt = $connection->prepare('insert into technology(name, date) values (?, ?);');
226+
$this->storage->exchangeArray([]);
227+
$e = null;
228+
229+
try {
230+
$stmt->executeQuery();
231+
} catch (\Throwable $e) {
232+
// do nothing
233+
}
234+
$this->assertNotNull($e);
235+
$this->assertCount(1, $this->storage);
236+
$span = $this->storage->offsetGet(0);
237+
$this->assertSame('Error', $span->getStatus()->getCode());
238+
$this->assertStringContainsString('Unable to execute', $span->getStatus()->getDescription());
239+
}
205240
}

0 commit comments

Comments
 (0)