Skip to content

Adding bindings to SQL spans in tracing #804

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Nov 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions config/sentry.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,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