laravel.tsx 11 KB

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