Skip to content

Commit

Permalink
Merge branch 'master' into eloquent-violation-reporter-memoization
Browse files Browse the repository at this point in the history
# Conflicts:
#	src/Sentry/Laravel/Integration.php
  • Loading branch information
stayallive committed Apr 17, 2024
2 parents 6d12095 + 300bb6f commit f1ea1df
Show file tree
Hide file tree
Showing 15 changed files with 557 additions and 484 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"require": {
"php": "^7.2 | ^8.0",
"illuminate/support": "^6.0 | ^7.0 | ^8.0 | ^9.0 | ^10.0 | ^11.0",
"sentry/sentry": "^4.5",
"sentry/sentry": "^4.7",
"symfony/psr-http-message-bridge": "^1.0 | ^2.0 | ^6.0 | ^7.0",
"nyholm/psr7": "^1.0"
},
Expand Down
3 changes: 3 additions & 0 deletions config/sentry.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,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
10 changes: 10 additions & 0 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,11 @@ parameters:
count: 1
path: src/Sentry/Laravel/Tracing/Integrations/LighthouseIntegration.php

-
message: "#^Method Sentry\\\\Laravel\\\\Tracing\\\\Integrations\\\\LighthouseIntegration\\:\\:extractOperationDefinitionNode\\(\\) has invalid return type GraphQL\\\\Language\\\\AST\\\\OperationDefinitionNode\\.$#"
count: 1
path: src/Sentry/Laravel/Tracing/Integrations/LighthouseIntegration.php

-
message: "#^Parameter \\$endExecution of method Sentry\\\\Laravel\\\\Tracing\\\\Integrations\\\\LighthouseIntegration\\:\\:handleEndExecution\\(\\) has invalid type Nuwave\\\\Lighthouse\\\\Events\\\\EndExecution\\.$#"
count: 1
Expand All @@ -150,6 +155,11 @@ parameters:
count: 1
path: src/Sentry/Laravel/Tracing/Integrations/LighthouseIntegration.php

-
message: "#^Parameter \\$query of method Sentry\\\\Laravel\\\\Tracing\\\\Integrations\\\\LighthouseIntegration\\:\\:extractOperationDefinitionNode\\(\\) has invalid type GraphQL\\\\Language\\\\AST\\\\DocumentNode\\.$#"
count: 1
path: src/Sentry/Laravel/Tracing/Integrations/LighthouseIntegration.php

-
message: "#^Parameter \\$startExecution of method Sentry\\\\Laravel\\\\Tracing\\\\Integrations\\\\LighthouseIntegration\\:\\:handleStartExecution\\(\\) has invalid type Nuwave\\\\Lighthouse\\\\Events\\\\StartExecution\\.$#"
count: 1
Expand Down
75 changes: 0 additions & 75 deletions src/Sentry/Laravel/EventHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Exception;
use Illuminate\Auth\Events as AuthEvents;
use Illuminate\Console\Events as ConsoleEvents;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Contracts\Container\BindingResolutionException;
use Illuminate\Contracts\Container\Container;
Expand All @@ -21,8 +20,6 @@
use Sentry\Laravel\Tracing\Middleware;
use Sentry\SentrySdk;
use Sentry\State\Scope;
use Symfony\Component\Console\Input\ArgvInput;
use Symfony\Component\Console\Input\InputInterface;

class EventHandler
{
Expand All @@ -35,8 +32,6 @@ class EventHandler
LogEvents\MessageLogged::class => 'messageLogged',
RoutingEvents\RouteMatched::class => 'routeMatched',
DatabaseEvents\QueryExecuted::class => 'queryExecuted',
ConsoleEvents\CommandStarting::class => 'commandStarting',
ConsoleEvents\CommandFinished::class => 'commandFinished',
];

/**
Expand Down Expand Up @@ -96,13 +91,6 @@ class EventHandler
*/
private $recordLaravelLogs;

/**
* Indicates if we should add command info to the breadcrumbs.
*
* @var bool
*/
private $recordCommandInfo;

/**
* Indicates if we should add tick info to the breadcrumbs.
*
Expand Down Expand Up @@ -137,7 +125,6 @@ public function __construct(Container $container, array $config)
$this->recordSqlQueries = ($config['breadcrumbs.sql_queries'] ?? $config['breadcrumbs']['sql_queries'] ?? true) === true;
$this->recordSqlBindings = ($config['breadcrumbs.sql_bindings'] ?? $config['breadcrumbs']['sql_bindings'] ?? false) === true;
$this->recordLaravelLogs = ($config['breadcrumbs.logs'] ?? $config['breadcrumbs']['logs'] ?? true) === true;
$this->recordCommandInfo = ($config['breadcrumbs.command_info'] ?? $config['breadcrumbs']['command_info'] ?? true) === true;
$this->recordOctaneTickInfo = ($config['breadcrumbs.octane_tick_info'] ?? $config['breadcrumbs']['octane_tick_info'] ?? true) === true;
$this->recordOctaneTaskInfo = ($config['breadcrumbs.octane_task_info'] ?? $config['breadcrumbs']['octane_task_info'] ?? true) === true;
}
Expand Down Expand Up @@ -316,68 +303,6 @@ private function configureUserScopeFromModel($authUser): void
});
}

protected function commandStartingHandler(ConsoleEvents\CommandStarting $event): void
{
if ($event->command) {
Integration::configureScope(static function (Scope $scope) use ($event): void {
$scope->setTag('command', $event->command);
});

if (!$this->recordCommandInfo) {
return;
}

Integration::addBreadcrumb(new Breadcrumb(
Breadcrumb::LEVEL_INFO,
Breadcrumb::TYPE_DEFAULT,
'artisan.command',
'Starting Artisan command: ' . $event->command,
[
'input' => $this->extractConsoleCommandInput($event->input),
]
));
}
}

protected function commandFinishedHandler(ConsoleEvents\CommandFinished $event): void
{
if ($this->recordCommandInfo) {
Integration::addBreadcrumb(new Breadcrumb(
Breadcrumb::LEVEL_INFO,
Breadcrumb::TYPE_DEFAULT,
'artisan.command',
'Finished Artisan command: ' . $event->command,
[
'exit' => $event->exitCode,
'input' => $this->extractConsoleCommandInput($event->input),
]
));
}

// Flush any and all events that were possibly generated by the command
Integration::flushEvents();

Integration::configureScope(static function (Scope $scope): void {
$scope->removeTag('command');
});
}

/**
* Extract the command input arguments if possible.
*
* @param \Symfony\Component\Console\Input\InputInterface|null $input
*
* @return string|null
*/
private function extractConsoleCommandInput(?InputInterface $input): ?string
{
if ($input instanceof ArgvInput) {
return (string)$input;
}

return null;
}

protected function octaneRequestReceivedHandler(Octane\RequestReceived $event): void
{
$this->prepareScopeForOctane();
Expand Down
2 changes: 1 addition & 1 deletion src/Sentry/Laravel/Features/CacheIntegration.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public function handleRedisCommand(RedisEvents\CommandExecuted $event): void
$commandOrigin = $this->resolveEventOrigin();

if ($commandOrigin !== null) {
$data['db.redis.origin'] = $commandOrigin;
$data = array_merge($data, $commandOrigin);
}
}

Expand Down
22 changes: 19 additions & 3 deletions src/Sentry/Laravel/Features/Concerns/ResolvesEventOrigin.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,35 @@ protected function container(): Container
return app();
}

protected function resolveEventOrigin(): ?string
protected function resolveEventOrigin(): ?array
{
$backtraceHelper = $this->makeBacktraceHelper();

$firstAppFrame = $backtraceHelper->findFirstInAppFrameForBacktrace(debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS));
// We limit the backtrace to 20 frames to prevent too much overhead and we'd reasonable expect the origin to be within the first 20 frames
$firstAppFrame = $backtraceHelper->findFirstInAppFrameForBacktrace(debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 20));

if ($firstAppFrame === null) {
return null;
}

$filePath = $backtraceHelper->getOriginalViewPathForFrameOfCompiledViewPath($firstAppFrame) ?? $firstAppFrame->getFile();

return "{$filePath}:{$firstAppFrame->getLine()}";
return [
'code.filepath' => $filePath,
'code.function' => $firstAppFrame->getFunctionName(),
'code.lineno' => $firstAppFrame->getLine(),
];
}

protected function resolveEventOriginAsString(): ?string
{
$origin = $this->resolveEventOrigin();

if ($origin === null) {
return null;
}

return "{$origin['code.filepath']}:{$origin['code.lineno']}";
}

private function makeBacktraceHelper(): BacktraceHelper
Expand Down
Loading

0 comments on commit f1ea1df

Please sign in to comment.