diff --git a/src/Instrumentation/PostgreSql/src/PostgreSqlInstrumentation.php b/src/Instrumentation/PostgreSql/src/PostgreSqlInstrumentation.php index 46fa72c66..317faed83 100644 --- a/src/Instrumentation/PostgreSql/src/PostgreSqlInstrumentation.php +++ b/src/Instrumentation/PostgreSql/src/PostgreSqlInstrumentation.php @@ -137,6 +137,17 @@ public static function register(): void } ); + hook( + null, + 'pg_query_params', + pre: static function (...$args) use ($instrumentation, $tracker) { + return self::basicPreHookWithContextPropagator('pg_query_params', $instrumentation, $tracker, ...$args); + }, + post: static function (...$args) use ($instrumentation, $tracker) { + self::queryPostHook($instrumentation, $tracker, ...$args); + } + ); + hook( null, 'pg_select', diff --git a/src/Instrumentation/PostgreSql/tests/Integration/PostgreSqlInstrumentationTest.php b/src/Instrumentation/PostgreSql/tests/Integration/PostgreSqlInstrumentationTest.php index ce19347d2..1bb534de1 100644 --- a/src/Instrumentation/PostgreSql/tests/Integration/PostgreSqlInstrumentationTest.php +++ b/src/Instrumentation/PostgreSql/tests/Integration/PostgreSqlInstrumentationTest.php @@ -124,6 +124,34 @@ public function test_pg_query(): void $this->assertDatabaseAttributesForAllSpans($offset); } + public function test_pg_query_params(): void + { + $conn = pg_connect('host=' . $this->pgsqlHost . ' dbname=' . $this->database . ' user=' . $this->user . ' password=' . $this->passwd); + $this->assertTrue($conn instanceof Connection); + + $offset = 0; + $this->assertSame('pg_connect', $this->storage->offsetGet($offset)->getName()); + $offset++; + + $res = pg_query_params($conn, 'SELECT * FROM users WHERE id = $1', [1]); + $this->assertTrue($res instanceof Result); + + $this->assertSame('pg_query_params', $this->storage->offsetGet($offset)->getName()); + $this->assertAttributes($offset, [ + TraceAttributes::DB_QUERY_TEXT => 'SELECT * FROM users WHERE id = $1', + TraceAttributes::DB_OPERATION_NAME => 'SELECT', + ]); + + while ($row = pg_fetch_assoc($res)) { + } + $offset++; + + pg_close($conn); + + $this->assertCount($offset, $this->storage); + $this->assertDatabaseAttributesForAllSpans($offset); + } + public function test_pg_convert(): void { $conn = pg_connect('host=' . $this->pgsqlHost . ' dbname=' . $this->database . ' user=' . $this->user . ' password=' . $this->passwd);