laravel.tsx 11 KB

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