Skip to content

Commit

Permalink
feat: add toggler for erasing query (#13)
Browse files Browse the repository at this point in the history
* Add toggler for erasing query

* fix comment
  • Loading branch information
evan361425 authored Aug 2, 2021
1 parent c730c12 commit 228f2dd
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 9 deletions.
7 changes: 7 additions & 0 deletions config/xray.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 5 additions & 1 deletion src/Collectors/DatabaseQueryCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
class DatabaseQueryCollector extends EventsCollector
{
protected $bindingsEnabled = false;
protected $eraseQuery = false;

public function registerEventListeners(): void
{
Expand All @@ -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);
}

Expand Down
33 changes: 25 additions & 8 deletions tests/Collectors/DatabaseQueryCollectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand All @@ -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())
Expand All @@ -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
Expand Down

0 comments on commit 228f2dd

Please sign in to comment.