diff --git a/config/xray.php b/config/xray.php index bf5eb92..a3b7cf7 100755 --- a/config/xray.php +++ b/config/xray.php @@ -56,6 +56,13 @@ */ 'db_bindings' => env('AWS_XRAY_ENABLE_DB_QUERY_BINDINGS', false), + /* + |-------------------------------------------------------------------------- + | Erase Database Query Value + |-------------------------------------------------------------------------- + */ + 'db_erase_query' => env('AWS_XRAY_ERASE_DB_QUERY_VALUE', false), + /* |-------------------------------------------------------------------------- | Trace Queue Jobs diff --git a/src/Collectors/DatabaseQueryCollector.php b/src/Collectors/DatabaseQueryCollector.php index b9beb5e..6b6c36a 100644 --- a/src/Collectors/DatabaseQueryCollector.php +++ b/src/Collectors/DatabaseQueryCollector.php @@ -13,6 +13,7 @@ class DatabaseQueryCollector extends EventsCollector { protected $bindingsEnabled = false; + protected $eraseQuery = false; public function registerEventListeners(): void { @@ -26,11 +27,14 @@ public function registerEventListeners(): void }); $this->bindingsEnabled = config('xray.db_bindings'); + $this->eraseQuery = config('xray.db_erase_query'); } public function handleQueryReport(string $sql, array $bindings, float $time, Connection $connection): void { - if ($this->bindingsEnabled) { + if ($this->eraseQuery) { + $sql = ''; + } else if ($this->bindingsEnabled) { $sql = $this->parseBindings($sql, $bindings, $connection); } diff --git a/tests/Collectors/DatabaseQueryCollectorTest.php b/tests/Collectors/DatabaseQueryCollectorTest.php index b8db637..1af2f0a 100644 --- a/tests/Collectors/DatabaseQueryCollectorTest.php +++ b/tests/Collectors/DatabaseQueryCollectorTest.php @@ -13,6 +13,8 @@ class DatabaseQueryCollectorTest extends TestCase { public function test_return_query_if_count_not_match() { + $this->app['config']->set('xray.db_erase_query', false); + $this->app['config']->set('xray.db_bindings', true); $connection = $this->createMock(Connection::class); $connection->expects($this->once()) @@ -37,6 +39,8 @@ public function test_return_query_if_count_not_match() public function test_binding_correctly() { + $this->app['config']->set('xray.db_erase_query', false); + $this->app['config']->set('xray.db_bindings', true); $connection = $this->createMock(Connection::class); $connection->expects($this->once()) @@ -59,15 +63,28 @@ public function test_binding_correctly() $this->assertEquals("abc 123 def 'ghi'", $querySerialized['sql']['sanitized_query']); } - /** - * Define environment setup. - * - * @param \Illuminate\Foundation\Application $app - * @return void - */ - protected function getEnvironmentSetUp($app) + public function test_should_ignore_binding_when_erase_query() { - $app['config']->set('xray.db_bindings', true); + $this->app['config']->set('xray.db_erase_query', true); + $this->app['config']->set('xray.db_bindings', true); + $connection = $this->createMock(Connection::class); + + $connection->expects($this->once()) + ->method('getName') + ->willReturn('name'); + $connection->expects($this->once()) + ->method('getDriverName') + ->willReturn('driver-name'); + $connection->expects($this->never())->method('prepareBindings'); + + $collector = new DatabaseQueryCollector($this->app); + $collector->current()->begin(); + $collector->handleQueryReport('abc ? def ?', [1, 2], 0, $connection); + + $serialized = $collector->current()->jsonSerialize(); + $querySerialized = $serialized['subsegments'][0]->jsonSerialize(); + + $this->assertFalse(isset($querySerialized['sql']['sanitized_query'])); } protected function setUp(): void