|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Sentry\Laravel\Console; |
| 4 | + |
| 5 | +use Exception; |
| 6 | +use Illuminate\Console\Command; |
| 7 | +use Illuminate\Support\Str; |
| 8 | +use Psr\Log\AbstractLogger; |
| 9 | +use Sentry\ClientBuilder; |
| 10 | +use Sentry\State\Hub; |
| 11 | +use Sentry\State\HubInterface; |
| 12 | +use Sentry\Tracing\SpanContext; |
| 13 | +use Sentry\Tracing\TransactionContext; |
| 14 | + |
| 15 | +class TestCommand extends Command |
| 16 | +{ |
| 17 | + /** |
| 18 | + * Laravel 5.0.x: The name and signature of the console command. |
| 19 | + * |
| 20 | + * @var string |
| 21 | + */ |
| 22 | + protected $name = 'sentry:test'; |
| 23 | + |
| 24 | + /** |
| 25 | + * The name and signature of the console command. |
| 26 | + * |
| 27 | + * @var string |
| 28 | + */ |
| 29 | + protected $signature = 'sentry:test {--transaction} {--dsn=}'; |
| 30 | + |
| 31 | + /** |
| 32 | + * The console command description. |
| 33 | + * |
| 34 | + * @var string |
| 35 | + */ |
| 36 | + protected $description = 'Generate a test event and send it to Sentry'; |
| 37 | + |
| 38 | + /** |
| 39 | + * Buffer of log messages generated by the Sentry SDK. |
| 40 | + * |
| 41 | + * @var array |
| 42 | + */ |
| 43 | + private $logMessages = []; |
| 44 | + |
| 45 | + /** |
| 46 | + * Execute the console command. |
| 47 | + * |
| 48 | + * @return int |
| 49 | + */ |
| 50 | + public function handle(): int |
| 51 | + { |
| 52 | + // Maximize error reporting |
| 53 | + $old_error_reporting = error_reporting(E_ALL | E_STRICT); |
| 54 | + |
| 55 | + $dsn = $this->option('dsn'); |
| 56 | + |
| 57 | + // If the DSN was not passed as option to the command we use the registered client to get the DSN from the Laravel config |
| 58 | + if ($dsn === null) { |
| 59 | + $dsnObject = app(HubInterface::class)->getClient()->getOptions()->getDsn(); |
| 60 | + |
| 61 | + if ($dsnObject !== null) { |
| 62 | + $dsn = (string)$dsnObject; |
| 63 | + |
| 64 | + $this->info('DSN discovered from Laravel config or `.env` file!'); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + // No DSN found from the command line or config |
| 69 | + if (!$dsn) { |
| 70 | + $this->error('Could not discover DSN!'); |
| 71 | + |
| 72 | + $this->printDebugTips(); |
| 73 | + |
| 74 | + return self::FAILURE; |
| 75 | + } |
| 76 | + |
| 77 | + try { |
| 78 | + $clientBuilder = ClientBuilder::create([ |
| 79 | + 'dsn' => $dsn, |
| 80 | + 'traces_sample_rate' => 1.0, |
| 81 | + ]); |
| 82 | + } catch (Exception $e) { |
| 83 | + $this->error($e->getMessage()); |
| 84 | + |
| 85 | + return self::FAILURE; |
| 86 | + } |
| 87 | + |
| 88 | + // We set a logger so we can surface errors thrown internally by the SDK |
| 89 | + $clientBuilder->setLogger(new class($this) extends AbstractLogger { |
| 90 | + private $command; |
| 91 | + |
| 92 | + public function __construct(TestCommand $command) |
| 93 | + { |
| 94 | + $this->command = $command; |
| 95 | + } |
| 96 | + |
| 97 | + public function log($level, $message, array $context = []): void |
| 98 | + { |
| 99 | + if ($level === 'error') { |
| 100 | + $this->command->logMessageFromSDK($message); |
| 101 | + } |
| 102 | + } |
| 103 | + }); |
| 104 | + |
| 105 | + $hub = new Hub($clientBuilder->getClient()); |
| 106 | + |
| 107 | + $this->info('Sending test event...'); |
| 108 | + |
| 109 | + $exception = $this->generateTestException($this->name, ['foo' => 'bar']); |
| 110 | + |
| 111 | + $eventId = $hub->captureException($exception); |
| 112 | + |
| 113 | + if (!$eventId) { |
| 114 | + $this->error('There was an error sending the event.'); |
| 115 | + |
| 116 | + $this->printDebugTips(); |
| 117 | + |
| 118 | + return self::FAILURE; |
| 119 | + } |
| 120 | + |
| 121 | + $this->info("Test event sent with ID: {$eventId}"); |
| 122 | + |
| 123 | + if ($this->option('transaction')) { |
| 124 | + $this->clearLogMessagesFromSDK(); |
| 125 | + |
| 126 | + $transactionContext = new TransactionContext(); |
| 127 | + $transactionContext->setSampled(true); |
| 128 | + $transactionContext->setName('Sentry Test Transaction'); |
| 129 | + $transactionContext->setOp('sentry.test'); |
| 130 | + |
| 131 | + $transaction = $hub->startTransaction($transactionContext); |
| 132 | + |
| 133 | + $spanContext = new SpanContext(); |
| 134 | + $spanContext->setOp('sentry.sent'); |
| 135 | + |
| 136 | + $span = $transaction->startChild($spanContext); |
| 137 | + |
| 138 | + $this->info('Sending transaction...'); |
| 139 | + |
| 140 | + $span->finish(); |
| 141 | + $transactionId = $transaction->finish(); |
| 142 | + |
| 143 | + if (!$transactionId) { |
| 144 | + $this->error('There was an error sending the transaction.'); |
| 145 | + |
| 146 | + $this->printDebugTips(); |
| 147 | + |
| 148 | + return self::FAILURE; |
| 149 | + } |
| 150 | + |
| 151 | + $this->info("Transaction sent with ID: {$transactionId}"); |
| 152 | + } |
| 153 | + |
| 154 | + error_reporting($old_error_reporting); |
| 155 | + |
| 156 | + return self::SUCCESS; |
| 157 | + } |
| 158 | + |
| 159 | + /** |
| 160 | + * Generate a test exception to send to Sentry. |
| 161 | + * |
| 162 | + * @param $command |
| 163 | + * @param $arg |
| 164 | + * |
| 165 | + * @return \Exception |
| 166 | + */ |
| 167 | + protected function generateTestException($command, $arg): Exception |
| 168 | + { |
| 169 | + // Do something silly |
| 170 | + try { |
| 171 | + throw new Exception('This is a test exception sent from the Sentry Laravel SDK.'); |
| 172 | + } catch (Exception $exception) { |
| 173 | + return $exception; |
| 174 | + } |
| 175 | + } |
| 176 | + |
| 177 | + public function logMessageFromSDK(string $message): void |
| 178 | + { |
| 179 | + $this->logMessages[] = $message; |
| 180 | + } |
| 181 | + |
| 182 | + private function clearLogMessagesFromSDK(): void |
| 183 | + { |
| 184 | + $this->logMessages = []; |
| 185 | + } |
| 186 | + |
| 187 | + private function printDebugTips(): void |
| 188 | + { |
| 189 | + $emittedSDKErrors = false; |
| 190 | + $probablySSLError = false; |
| 191 | + |
| 192 | + foreach ($this->logMessages as $logMessage) { |
| 193 | + $emittedSDKErrors = true; |
| 194 | + |
| 195 | + $this->error("SDK: {$logMessage}"); |
| 196 | + |
| 197 | + if (Str::contains($logMessage, ['SSL certificate problem', 'certificate has expired'])) { |
| 198 | + $probablySSLError = true; |
| 199 | + } |
| 200 | + } |
| 201 | + |
| 202 | + if ($probablySSLError) { |
| 203 | + $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.'); |
| 204 | + $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'); |
| 205 | + } elseif ($emittedSDKErrors) { |
| 206 | + $this->error('Please check the error message from the SDK above for further hints about what went wrong.'); |
| 207 | + } else { |
| 208 | + $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`.'); |
| 209 | + } |
| 210 | + } |
| 211 | +} |
0 commit comments