Skip to content

Limit cases where SQL origin is captured #881

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 6 commits into from
Apr 17, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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 @@ -85,6 +85,9 @@
// Capture where the SQL query originated from on the SQL query spans
'sql_origin' => env('SENTRY_TRACE_SQL_ORIGIN_ENABLED', true),

// Define a threshold in milliseconds for SQL queries to resolve their origin
'sql_origin_threshold_ms' => env('SENTRY_TRACE_SQL_ORIGIN_THRESHOLD_MS', 100),

// Capture views rendered as spans
'views' => env('SENTRY_TRACE_VIEWS_ENABLED', true),

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ protected function resolveEventOrigin(): ?string
{
$backtraceHelper = $this->makeBacktraceHelper();

$firstAppFrame = $backtraceHelper->findFirstInAppFrameForBacktrace(debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS));
$firstAppFrame = $backtraceHelper->findFirstInAppFrameForBacktrace(debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 20));

if ($firstAppFrame === null) {
return null;
Expand Down
18 changes: 13 additions & 5 deletions src/Sentry/Laravel/Tracing/EventHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,14 @@ class EventHandler
*
* @var bool
*/
private $traceSqlQueryOrigins;
private $traceSqlQueryOrigin;

/**
* The threshold in milliseconds to consider a SQL query origin.
*
* @var int
*/
private $traceSqlQueryOriginTreshHoldMs;

/**
* Indicates if we should trace queue job spans.
Expand Down Expand Up @@ -90,7 +97,8 @@ 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->traceSqlQueryOrigin = ($config['sql_origin'] ?? true) === true;
$this->traceSqlQueryOriginTreshHoldMs = $config['sql_origin_threshold_ms'] ?? 100;

$this->traceQueueJobs = ($config['queue_jobs'] ?? false) === true;
$this->traceQueueJobsAsTransactions = ($config['queue_job_transactions'] ?? false) === true;
Expand Down Expand Up @@ -180,7 +188,7 @@ protected function queryExecutedHandler(DatabaseEvents\QueryExecuted $query): vo
]));
}

if ($this->traceSqlQueryOrigins) {
if ($this->traceSqlQueryOrigin && $query->time >= $this->traceSqlQueryOriginTreshHoldMs) {
$queryOrigin = $this->resolveQueryOriginFromBacktrace();

if ($queryOrigin !== null) {
Expand All @@ -194,13 +202,13 @@ protected function queryExecutedHandler(DatabaseEvents\QueryExecuted $query): vo
/**
* Try to find the origin of the SQL query that was just executed.
*
* @return string|null
* @return array|null
*/
private function resolveQueryOriginFromBacktrace(): ?array
{
$backtraceHelper = $this->makeBacktraceHelper();

$firstAppFrame = $backtraceHelper->findFirstInAppFrameForBacktrace(debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS));
$firstAppFrame = $backtraceHelper->findFirstInAppFrameForBacktrace(debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 20));

if ($firstAppFrame === null) {
return null;
Expand Down
39 changes: 37 additions & 2 deletions test/Sentry/Features/DatabaseIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,46 @@ public function testSqlBindingsAreRecordedWhenDisabled(): void
$this->assertFalse(isset($span->getData()['db.sql.bindings']));
}

private function executeQueryAndRetrieveSpan(string $query, array $bindings = []): Span
public function testSqlOriginIsResolvedWhenEnabledAndOverTreshold(): void
{
$this->resetApplicationWithConfig([
'sentry.tracing.sql_origin' => true,
'sentry.tracing.sql_origin_threshold_ms' => 10,
]);

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

$this->assertArrayHasKey('code.filepath', $span->getData());
}

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

$span = $this->executeQueryAndRetrieveSpan('SELECT 1');

$this->assertArrayNotHasKey('code.filepath', $span->getData());
}

public function testSqlOriginIsNotResolvedWhenUnderThreshold(): void
{
$this->resetApplicationWithConfig([
'sentry.tracing.sql_origin' => true,
'sentry.tracing.sql_origin_threshold_ms' => 10,
]);

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

$this->assertArrayNotHasKey('code.filepath', $span->getData());
}

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

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

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

Expand Down