laravel.tsx 11 KB

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