Skip to content
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

Improve sentry:test command #842

Merged
merged 3 commits into from
Jan 29, 2024
Merged
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
40 changes: 23 additions & 17 deletions src/Sentry/Laravel/Console/TestCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ class TestCommand extends Command
protected $description = 'Generate a test event and send it to Sentry';

/**
* Buffer of log messages generated by the Sentry SDK.
* Buffer of error messages generated by the Sentry SDK.
*
* @var array
*/
private $logMessages = [];
private $errorMessages = [];

/**
* Execute the console command.
Expand All @@ -46,6 +46,12 @@ class TestCommand extends Command
*/
public function handle(): int
{
if (!\extension_loaded('curl')) {
$this->error('You need to enable the PHP cURL extension (ext-curl).');

return 1;
}

// Maximize error reporting
$old_error_reporting = error_reporting(E_ALL | E_STRICT);

Expand Down Expand Up @@ -110,10 +116,15 @@ public function __construct(TestCommand $command)

public function log($level, $message, array $context = []): void
{
$this->command->info("SDK({$level}): {$message}", OutputInterface::VERBOSITY_VERBOSE);
// Only show debug, info and notice messages in verbose mode
$verbosity = in_array($level, ['debug', 'info', 'notice'], true)
? OutputInterface::VERBOSITY_VERBOSE
: OutputInterface::VERBOSITY_NORMAL;

if ($level === 'error') {
$this->command->logMessageFromSDK($message);
$this->command->info("SDK({$level}): {$message}", $verbosity);

if (in_array($level, ['error', 'critical'], true)) {
$this->command->logErrorMessageFromSDK($message);
}
}
});
Expand All @@ -137,7 +148,7 @@ public function log($level, $message, array $context = []): void
$this->info("Test event sent with ID: {$eventId}");

if ($this->option('transaction')) {
$this->clearLogMessagesFromSDK();
$this->clearErrorMessagesFromSDK();

$transactionContext = new TransactionContext();
$transactionContext->setSampled(true);
Expand Down Expand Up @@ -191,26 +202,21 @@ protected function generateTestException($command, $arg): Exception
}
}

public function logMessageFromSDK(string $message): void
public function logErrorMessageFromSDK(string $message): void
{
$this->logMessages[] = $message;
$this->errorMessages[] = $message;
}

private function clearLogMessagesFromSDK(): void
private function clearErrorMessagesFromSDK(): void
{
$this->logMessages = [];
$this->errorMessages = [];
}

private function printDebugTips(): void
{
$emittedSDKErrors = false;
$probablySSLError = false;

foreach ($this->logMessages as $logMessage) {
$emittedSDKErrors = true;

$this->error("SDK: {$logMessage}");

foreach ($this->errorMessages as $logMessage) {
if (Str::contains($logMessage, ['SSL certificate problem', 'certificate has expired'])) {
$probablySSLError = true;
}
Expand All @@ -219,7 +225,7 @@ private function printDebugTips(): void
if ($probablySSLError) {
$this->warn('The problem might be related to the Let\'s Encrypt root certificate that expired and your machine not having an up-to-date enough OpenSSL version or still having the expired root in your certificate authority store.');
$this->warn('For more information you can check out this forum post from Let\'s Encrypt that contains helpful links on how to resolve this for your environment: https://community.letsencrypt.org/t/production-chain-changes/150739/4');
} elseif ($emittedSDKErrors) {
} elseif (count($this->errorMessages) > 0) {
$this->error('Please check the error message from the SDK above for further hints about what went wrong.');
} else {
$this->error('Please check if your DSN is set properly in your `.env` as `SENTRY_LARAVEL_DSN` or in your config file `config/sentry.php`.');
Expand Down
Loading