laravel.tsx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. import ExternalLink from 'sentry/components/links/externalLink';
  2. import {StepType} from 'sentry/components/onboarding/gettingStartedDoc/step';
  3. import type {
  4. Docs,
  5. DocsParams,
  6. OnboardingConfig,
  7. } from 'sentry/components/onboarding/gettingStartedDoc/types';
  8. import {
  9. getCrashReportModalConfigDescription,
  10. getCrashReportModalIntroduction,
  11. getCrashReportSDKInstallFirstStep,
  12. } from 'sentry/components/onboarding/gettingStartedDoc/utils/feedbackOnboarding';
  13. import replayOnboardingJsLoader from 'sentry/gettingStartedDocs/javascript/jsLoader/jsLoader';
  14. import {t, tct} from 'sentry/locale';
  15. type Params = DocsParams;
  16. const getExceptionHandlerSnippet = () => `
  17. <?php
  18. use Illuminate\\Foundation\\Application;
  19. use Illuminate\\Foundation\\Configuration\\Exceptions;
  20. use Illuminate\\Foundation\\Configuration\\Middleware;
  21. use Sentry\\Laravel\\Integration;
  22. return Application::configure(basePath: dirname(__DIR__))
  23. ->withRouting(
  24. web: __DIR__.'/../routes/web.php',
  25. commands: __DIR__.'/../routes/console.php',
  26. health: '/up',
  27. )
  28. ->withMiddleware(function (Middleware $middleware) {
  29. //
  30. })
  31. ->withExceptions(function (Exceptions $exceptions) {
  32. Integration::handles($exceptions);
  33. })->create();`;
  34. const getConfigureSnippet = (params: Params) =>
  35. `SENTRY_LARAVEL_DSN=${params.dsn}${
  36. params.isPerformanceSelected
  37. ? `
  38. # Specify a fixed sample rate
  39. SENTRY_TRACES_SAMPLE_RATE=1.0`
  40. : ''
  41. }${
  42. params.isProfilingSelected
  43. ? `
  44. # Set a sampling rate for profiling - this is relative to traces_sample_rate
  45. SENTRY_PROFILES_SAMPLE_RATE=1.0`
  46. : ''
  47. }`;
  48. const getMetricsInstallSnippet = () => `
  49. composer install sentry/sentry-laravel
  50. composer update sentry/sentry-laravel -W`;
  51. const getMetricsVerifySnippet = () => `
  52. use function \\Sentry\\metrics;
  53. // Add 4 to a counter named 'hits'
  54. metrics()->increment('hits', 4);
  55. metrics()->flush();
  56. // We recommend registering the flush call in a shutdown function
  57. register_shutdown_function(static fn () => metrics()->flush());
  58. // Or call flush in a Terminable Middleware
  59. use Closure;
  60. use Illuminate\\Http\\Request;
  61. use Symfony\\Component\\HttpFoundation\\Response;
  62. use function \\Sentry\\metrics;
  63. class SentryMetricsMiddleware
  64. {
  65. public function handle(Request $request, Closure $next): Response
  66. {
  67. return $next($request);
  68. }
  69. public function terminate(Request $request, Response $response): void
  70. {
  71. metrics()->flush();
  72. }
  73. }`;
  74. const onboarding: OnboardingConfig = {
  75. introduction: () =>
  76. tct(
  77. 'This guide is for Laravel 11.0 an up. We also provide instructions for [otherVersionsLink:other versions] as well as [lumenSpecificLink:Lumen-specific instructions].',
  78. {
  79. otherVersionsLink: (
  80. <ExternalLink href="https://docs.sentry.io/platforms/php/guides/laravel/other-versions/" />
  81. ),
  82. lumenSpecificLink: (
  83. <ExternalLink href="https://docs.sentry.io/platforms/php/guides/laravel/other-versions/lumen/" />
  84. ),
  85. }
  86. ),
  87. install: (params: Params) => [
  88. {
  89. type: StepType.INSTALL,
  90. configurations: [
  91. {
  92. description: tct('Install the [code:sentry/sentry-laravel] package:', {
  93. code: <code />,
  94. }),
  95. language: 'bash',
  96. code: `composer require sentry/sentry-laravel`,
  97. },
  98. ...(params.isProfilingSelected
  99. ? [
  100. {
  101. description: t('Install the Excimer extension via PECL:'),
  102. language: 'bash',
  103. code: 'pecl install excimer',
  104. },
  105. {
  106. description: tct(
  107. "The Excimer PHP extension supports PHP 7.2 and up. Excimer requires Linux or macOS and doesn't support Windows. For additional ways to install Excimer, see [sentryPhpDocumentationLink: Sentry documentation].",
  108. {
  109. sentryPhpDocumentationLink: (
  110. <ExternalLink href="https://docs.sentry.io/platforms/php/profiling/#installation" />
  111. ),
  112. }
  113. ),
  114. },
  115. ]
  116. : []),
  117. {
  118. description: tct(
  119. 'Enable capturing unhandled exception to report to Sentry by making the following change to your [code:bootstrap/app.php]:',
  120. {
  121. code: <code />,
  122. }
  123. ),
  124. language: 'php',
  125. code: getExceptionHandlerSnippet(),
  126. },
  127. ],
  128. },
  129. ],
  130. configure: (params: Params) => [
  131. {
  132. type: StepType.CONFIGURE,
  133. configurations: [
  134. {
  135. description: t('Configure the Sentry DSN with this command:'),
  136. language: 'shell',
  137. code: `php artisan sentry:publish --dsn=${params.dsn}`,
  138. },
  139. {
  140. description: tct(
  141. 'It creates the config file ([sentryPHPCode:config/sentry.php]) and adds the [dsnCode:DSN] to your [envCode:.env] file where you can add further configuration options:',
  142. {sentryPHPCode: <code />, dsnCode: <code />, envCode: <code />}
  143. ),
  144. language: 'shell',
  145. code: getConfigureSnippet(params),
  146. },
  147. ],
  148. },
  149. ],
  150. verify: () => [
  151. {
  152. type: StepType.VERIFY,
  153. configurations: [
  154. {
  155. description: tct(
  156. 'You can test your configuration using the provided [code:sentry:test] artisan command:',
  157. {
  158. code: <code />,
  159. }
  160. ),
  161. language: 'shell',
  162. code: 'php artisan sentry:test',
  163. },
  164. ],
  165. },
  166. ],
  167. nextSteps: () => [],
  168. };
  169. const customMetricsOnboarding: OnboardingConfig = {
  170. install: () => [
  171. {
  172. type: StepType.INSTALL,
  173. description: tct(
  174. 'You need a minimum version [codeVersionLaravel:4.0.0] of the Laravel SDK and a minimum version [codeVersion:4.3.0] of the PHP SDK installed',
  175. {
  176. codeVersionLaravel: <code />,
  177. codeVersion: <code />,
  178. }
  179. ),
  180. configurations: [
  181. {
  182. language: 'bash',
  183. code: getMetricsInstallSnippet(),
  184. },
  185. ],
  186. },
  187. ],
  188. configure: () => [
  189. {
  190. type: StepType.CONFIGURE,
  191. description: tct(
  192. 'Once the SDK is installed or updated, you can enable code locations being emitted with your metrics in your [code:config/sentry.php] file:',
  193. {
  194. code: <code />,
  195. }
  196. ),
  197. configurations: [
  198. {
  199. code: [
  200. {
  201. label: 'PHP',
  202. value: 'php',
  203. language: 'php',
  204. code: `'attach_metric_code_locations' => true,`,
  205. },
  206. ],
  207. },
  208. ],
  209. },
  210. ],
  211. verify: () => [
  212. {
  213. type: StepType.VERIFY,
  214. description: tct(
  215. "Then you'll be able to add metrics as [codeCounters:counters], [codeSets:sets], [codeDistribution:distributions], and [codeGauge:gauges]. Try out this example:",
  216. {
  217. codeCounters: <code />,
  218. codeSets: <code />,
  219. codeDistribution: <code />,
  220. codeGauge: <code />,
  221. codeNamespace: <code />,
  222. }
  223. ),
  224. configurations: [
  225. {
  226. code: [
  227. {
  228. label: 'PHP',
  229. value: 'php',
  230. language: 'php',
  231. code: getMetricsVerifySnippet(),
  232. },
  233. ],
  234. },
  235. {
  236. description: t(
  237. 'With a bit of delay you can see the data appear in the Sentry UI.'
  238. ),
  239. },
  240. {
  241. description: tct(
  242. 'Learn more about metrics and how to configure them, by reading the [docsLink:docs].',
  243. {
  244. docsLink: (
  245. <ExternalLink href="https://docs.sentry.io/platforms/php/guides/laravel/metrics/" />
  246. ),
  247. }
  248. ),
  249. },
  250. ],
  251. },
  252. ],
  253. };
  254. const crashReportOnboarding: OnboardingConfig = {
  255. introduction: () => getCrashReportModalIntroduction(),
  256. install: (params: Params) => [
  257. {
  258. type: StepType.INSTALL,
  259. configurations: [
  260. getCrashReportSDKInstallFirstStep(params),
  261. {
  262. description: tct(
  263. 'Next, create [code:resources/views/errors/500.blade.php], and embed the feedback code:',
  264. {code: <code />}
  265. ),
  266. code: [
  267. {
  268. label: 'HTML',
  269. value: 'html',
  270. language: 'html',
  271. code: `<div class="content">
  272. <div class="title">Something went wrong.</div>
  273. @if(app()->bound('sentry') && app('sentry')->getLastEventId())
  274. <div class="subtitle">Error ID: {{ app('sentry')->getLastEventId() }}</div>
  275. <script>
  276. Sentry.init({ dsn: '${params.dsn}' });
  277. Sentry.showReportDialog({
  278. eventId: '{{ app('sentry')->getLastEventId() }}'
  279. });
  280. </script>
  281. @endif
  282. </div>`,
  283. },
  284. ],
  285. },
  286. {
  287. description: tct(
  288. 'For Laravel 5 up to 5.4 there is some extra work needed. You need to open up [codeApp:App/Exceptions/Handler.php] and extend the [codeRender:render] method to make sure the 500 error is rendered as a view correctly, in 5.5+ this step is not required anymore.',
  289. {code: <code />}
  290. ),
  291. code: [
  292. {
  293. label: 'PHP',
  294. value: 'php',
  295. language: 'php',
  296. code: `<?php
  297. use Symfony\Component\HttpKernel\Exception\HttpException;
  298. class Handler extends ExceptionHandler
  299. {
  300. public function report(Exception $exception)
  301. {
  302. if (app()->bound('sentry') && $this->shouldReport($exception)) {
  303. app('sentry')->captureException($exception);
  304. }
  305. parent::report($exception);
  306. }
  307. // This method is ONLY needed for Laravel 5 up to 5.4.
  308. // You can skip this method if you are using Laravel 5.5+.
  309. public function render($request, Exception $exception)
  310. {
  311. // Convert all non-http exceptions to a proper 500 http exception
  312. // if we don't do this exceptions are shown as a default template
  313. // instead of our own view in resources/views/errors/500.blade.php
  314. if ($this->shouldReport($exception) && !$this->isHttpException($exception) && !config('app.debug')) {
  315. $exception = new HttpException(500, 'Whoops!');
  316. }
  317. return parent::render($request, $exception);
  318. }
  319. }`,
  320. },
  321. ],
  322. },
  323. ],
  324. },
  325. ],
  326. configure: () => [
  327. {
  328. type: StepType.CONFIGURE,
  329. description: getCrashReportModalConfigDescription({
  330. link: 'https://docs.sentry.io/platforms/php/guides/laravel/user-feedback/configuration/#crash-report-modal',
  331. }),
  332. },
  333. ],
  334. verify: () => [],
  335. nextSteps: () => [],
  336. };
  337. const docs: Docs = {
  338. onboarding,
  339. replayOnboardingJsLoader,
  340. customMetricsOnboarding,
  341. crashReportOnboarding,
  342. };
  343. export default docs;