Skip to content

Commit 14760f7

Browse files
summerKKstayallive
andauthored
Adding bindings to SQL spans in tracing (#804)
Co-authored-by: Alex Bouma <[email protected]>
1 parent 38ccc3a commit 14760f7

File tree

4 files changed

+50
-4
lines changed

4 files changed

+50
-4
lines changed

config/sentry.php

+3
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@
7373
// Capture SQL queries as spans
7474
'sql_queries' => env('SENTRY_TRACE_SQL_QUERIES_ENABLED', true),
7575

76+
// Capture SQL query bindings (parameters) in SQL query spans
77+
'sql_bindings' => env('SENTRY_TRACE_SQL_BINDINGS_ENABLED', false),
78+
7679
// Capture where the SQL query originated from on the SQL query spans
7780
'sql_origin' => env('SENTRY_TRACE_SQL_ORIGIN_ENABLED', true),
7881

src/Sentry/Laravel/Tracing/EventHandler.php

+14
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,13 @@ class EventHandler
4141
*/
4242
private $traceSqlQueries;
4343

44+
/**
45+
* Indicates if we should add query bindings to query spans.
46+
*
47+
* @var bool
48+
*/
49+
private $traceSqlBindings;
50+
4451
/**
4552
* Indicates if we should we add SQL query origin data to query spans.
4653
*
@@ -82,6 +89,7 @@ class EventHandler
8289
public function __construct(array $config)
8390
{
8491
$this->traceSqlQueries = ($config['sql_queries'] ?? true) === true;
92+
$this->traceSqlBindings = ($config['sql_bindings'] ?? true) === true;
8593
$this->traceSqlQueryOrigins = ($config['sql_origin'] ?? true) === true;
8694

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

177+
if ($this->traceSqlBindings) {
178+
$context->setData(array_merge($context->getData(), [
179+
'db.sql.bindings' => $query->bindings
180+
]));
181+
}
182+
169183
if ($this->traceSqlQueryOrigins) {
170184
$queryOrigin = $this->resolveQueryOriginFromBacktrace();
171185

test/Sentry/Features/DatabaseIntegrationTest.php

+32-2
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,41 @@ public function testSpanIsCreatedForSqliteConnectionQuery(): void
8585
$this->assertNull($span->getData()['server.port']);
8686
}
8787

88-
private function executeQueryAndRetrieveSpan(string $query): Span
88+
public function testSqlBindingsAreRecordedWhenEnabled(): void
89+
{
90+
$this->resetApplicationWithConfig([
91+
'sentry.tracing.sql_bindings' => true,
92+
]);
93+
94+
$span = $this->executeQueryAndRetrieveSpan(
95+
$query = 'SELECT %',
96+
$bindings = ['1']
97+
);
98+
99+
$this->assertEquals($query, $span->getDescription());
100+
$this->assertEquals($bindings, $span->getData()['db.sql.bindings']);
101+
}
102+
103+
public function testSqlBindingsAreRecordedWhenDisabled(): void
104+
{
105+
$this->resetApplicationWithConfig([
106+
'sentry.tracing.sql_bindings' => false,
107+
]);
108+
109+
$span = $this->executeQueryAndRetrieveSpan(
110+
$query = 'SELECT %',
111+
['1']
112+
);
113+
114+
$this->assertEquals($query, $span->getDescription());
115+
$this->assertFalse(isset($span->getData()['db.sql.bindings']));
116+
}
117+
118+
private function executeQueryAndRetrieveSpan(string $query, array $bindings = []): Span
89119
{
90120
$transaction = $this->startTransaction();
91121

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

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

test/Sentry/Tracing/EventHandlerTest.php

+1-2
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@
33
namespace Sentry\Laravel\Tests\Tracing;
44

55
use ReflectionClass;
6-
use Sentry\Laravel\Tests\TestCase;
7-
use Sentry\Laravel\Tracing\BacktraceHelper;
86
use RuntimeException;
7+
use Sentry\Laravel\Tests\TestCase;
98
use Sentry\Laravel\Tracing\EventHandler;
109

1110
class EventHandlerTest extends TestCase

0 commit comments

Comments
 (0)