Skip to content

Commit

Permalink
Adding bindings to SQL spans in tracing (#804)
Browse files Browse the repository at this point in the history
Co-authored-by: Alex Bouma <[email protected]>
  • Loading branch information
summerKK and stayallive authored Nov 22, 2023
1 parent 38ccc3a commit 14760f7
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 4 deletions.
3 changes: 3 additions & 0 deletions config/sentry.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@
// Capture SQL queries as spans
'sql_queries' => env('SENTRY_TRACE_SQL_QUERIES_ENABLED', true),

// Capture SQL query bindings (parameters) in SQL query spans
'sql_bindings' => env('SENTRY_TRACE_SQL_BINDINGS_ENABLED', false),

// Capture where the SQL query originated from on the SQL query spans
'sql_origin' => env('SENTRY_TRACE_SQL_ORIGIN_ENABLED', true),

Expand Down
14 changes: 14 additions & 0 deletions src/Sentry/Laravel/Tracing/EventHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@ class EventHandler
*/
private $traceSqlQueries;

/**
* Indicates if we should add query bindings to query spans.
*
* @var bool
*/
private $traceSqlBindings;

/**
* Indicates if we should we add SQL query origin data to query spans.
*
Expand Down Expand Up @@ -82,6 +89,7 @@ class EventHandler
public function __construct(array $config)
{
$this->traceSqlQueries = ($config['sql_queries'] ?? true) === true;
$this->traceSqlBindings = ($config['sql_bindings'] ?? true) === true;
$this->traceSqlQueryOrigins = ($config['sql_origin'] ?? true) === true;

$this->traceQueueJobs = ($config['queue_jobs'] ?? false) === true;
Expand Down Expand Up @@ -166,6 +174,12 @@ protected function queryExecutedHandler(DatabaseEvents\QueryExecuted $query): vo
$context->setStartTimestamp(microtime(true) - $query->time / 1000);
$context->setEndTimestamp($context->getStartTimestamp() + $query->time / 1000);

if ($this->traceSqlBindings) {
$context->setData(array_merge($context->getData(), [
'db.sql.bindings' => $query->bindings
]));
}

if ($this->traceSqlQueryOrigins) {
$queryOrigin = $this->resolveQueryOriginFromBacktrace();

Expand Down
34 changes: 32 additions & 2 deletions test/Sentry/Features/DatabaseIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,41 @@ public function testSpanIsCreatedForSqliteConnectionQuery(): void
$this->assertNull($span->getData()['server.port']);
}

private function executeQueryAndRetrieveSpan(string $query): Span
public function testSqlBindingsAreRecordedWhenEnabled(): void
{
$this->resetApplicationWithConfig([
'sentry.tracing.sql_bindings' => true,
]);

$span = $this->executeQueryAndRetrieveSpan(
$query = 'SELECT %',
$bindings = ['1']
);

$this->assertEquals($query, $span->getDescription());
$this->assertEquals($bindings, $span->getData()['db.sql.bindings']);
}

public function testSqlBindingsAreRecordedWhenDisabled(): void
{
$this->resetApplicationWithConfig([
'sentry.tracing.sql_bindings' => false,
]);

$span = $this->executeQueryAndRetrieveSpan(
$query = 'SELECT %',
['1']
);

$this->assertEquals($query, $span->getDescription());
$this->assertFalse(isset($span->getData()['db.sql.bindings']));
}

private function executeQueryAndRetrieveSpan(string $query, array $bindings = []): Span
{
$transaction = $this->startTransaction();

$this->dispatchLaravelEvent(new QueryExecuted($query, [], 123, DB::connection()));
$this->dispatchLaravelEvent(new QueryExecuted($query, $bindings, 123, DB::connection()));

$spans = $transaction->getSpanRecorder()->getSpans();

Expand Down
3 changes: 1 addition & 2 deletions test/Sentry/Tracing/EventHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@
namespace Sentry\Laravel\Tests\Tracing;

use ReflectionClass;
use Sentry\Laravel\Tests\TestCase;
use Sentry\Laravel\Tracing\BacktraceHelper;
use RuntimeException;
use Sentry\Laravel\Tests\TestCase;
use Sentry\Laravel\Tracing\EventHandler;

class EventHandlerTest extends TestCase
Expand Down

0 comments on commit 14760f7

Please sign in to comment.