laravel.tsx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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. : []),
  107. {
  108. description: tct(
  109. 'Enable capturing unhandled exception to report to Sentry by making the following change to your [code:bootstrap/app.php]:',
  110. {
  111. code: <code />,
  112. }
  113. ),
  114. language: 'php',
  115. code: getExceptionHandlerSnippet(),
  116. },
  117. ],
  118. },
  119. ],
  120. configure: (params: Params) => [
  121. {
  122. type: StepType.CONFIGURE,
  123. configurations: [
  124. {
  125. description: t('Configure the Sentry DSN with this command:'),
  126. language: 'shell',
  127. code: `php artisan sentry:publish --dsn=${params.dsn}`,
  128. },
  129. {
  130. description: tct(
  131. '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:',
  132. {sentryPHPCode: <code />, dsnCode: <code />, envCode: <code />}
  133. ),
  134. language: 'shell',
  135. code: getConfigureSnippet(params),
  136. },
  137. ],
  138. },
  139. ],
  140. verify: () => [
  141. {
  142. type: StepType.VERIFY,
  143. configurations: [
  144. {
  145. description: tct(
  146. 'You can test your configuration using the provided [code:sentry:test] artisan command:',
  147. {
  148. code: <code />,
  149. }
  150. ),
  151. language: 'shell',
  152. code: 'php artisan sentry:test',
  153. },
  154. ],
  155. },
  156. ],
  157. nextSteps: () => [],
  158. };
  159. const customMetricsOnboarding: OnboardingConfig = {
  160. install: () => [
  161. {
  162. type: StepType.INSTALL,
  163. description: tct(
  164. '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',
  165. {
  166. codeVersionLaravel: <code />,
  167. codeVersion: <code />,
  168. }
  169. ),
  170. configurations: [
  171. {
  172. language: 'bash',
  173. code: getMetricsInstallSnippet(),
  174. },
  175. ],
  176. },
  177. ],
  178. configure: () => [
  179. {
  180. type: StepType.CONFIGURE,
  181. description: tct(
  182. 'Once the SDK is installed or updated, you can enable code locations being emitted with your metrics in your [code:config/sentry.php] file:',
  183. {
  184. code: <code />,
  185. }
  186. ),
  187. configurations: [
  188. {
  189. code: [
  190. {
  191. label: 'PHP',
  192. value: 'php',
  193. language: 'php',
  194. code: `'attach_metric_code_locations' => true,`,
  195. },
  196. ],
  197. },
  198. ],
  199. },
  200. ],
  201. verify: () => [
  202. {
  203. type: StepType.VERIFY,
  204. description: tct(
  205. "Then you'll be able to add metrics as [codeCounters:counters], [codeSets:sets], [codeDistribution:distributions], and [codeGauge:gauges]. Try out this example:",
  206. {
  207. codeCounters: <code />,
  208. codeSets: <code />,
  209. codeDistribution: <code />,
  210. codeGauge: <code />,
  211. codeNamespace: <code />,
  212. }
  213. ),
  214. configurations: [
  215. {
  216. code: [
  217. {
  218. label: 'PHP',
  219. value: 'php',
  220. language: 'php',
  221. code: getMetricsVerifySnippet(),
  222. },
  223. ],
  224. },
  225. {
  226. description: t(
  227. 'With a bit of delay you can see the data appear in the Sentry UI.'
  228. ),
  229. },
  230. {
  231. description: tct(
  232. 'Learn more about metrics and how to configure them, by reading the [docsLink:docs].',
  233. {
  234. docsLink: (
  235. <ExternalLink href="https://docs.sentry.io/platforms/php/guides/laravel/metrics/" />
  236. ),
  237. }
  238. ),
  239. },
  240. ],
  241. },
  242. ],
  243. };
  244. const crashReportOnboarding: OnboardingConfig = {
  245. introduction: () => getCrashReportModalIntroduction(),
  246. install: (params: Params) => [
  247. {
  248. type: StepType.INSTALL,
  249. configurations: [
  250. getCrashReportSDKInstallFirstStep(params),
  251. {
  252. description: tct(
  253. 'Next, create [code:resources/views/errors/500.blade.php], and embed the feedback code:',
  254. {code: <code />}
  255. ),
  256. code: [
  257. {
  258. label: 'HTML',
  259. value: 'html',
  260. language: 'html',
  261. code: `<div class="content">
  262. <div class="title">Something went wrong.</div>
  263. @if(app()->bound('sentry') && app('sentry')->getLastEventId())
  264. <div class="subtitle">Error ID: {{ app('sentry')->getLastEventId() }}</div>
  265. <script>
  266. Sentry.init({ dsn: '${params.dsn}' });
  267. Sentry.showReportDialog({
  268. eventId: '{{ app('sentry')->getLastEventId() }}'
  269. });
  270. </script>
  271. @endif
  272. </div>`,
  273. },
  274. ],
  275. },
  276. {
  277. description: tct(
  278. '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.',
  279. {code: <code />}
  280. ),
  281. code: [
  282. {
  283. label: 'PHP',
  284. value: 'php',
  285. language: 'php',
  286. code: `<?php
  287. use Symfony\Component\HttpKernel\Exception\HttpException;
  288. class Handler extends ExceptionHandler
  289. {
  290. public function report(Exception $exception)
  291. {
  292. if (app()->bound('sentry') && $this->shouldReport($exception)) {
  293. app('sentry')->captureException($exception);
  294. }
  295. parent::report($exception);
  296. }
  297. // This method is ONLY needed for Laravel 5 up to 5.4.
  298. // You can skip this method if you are using Laravel 5.5+.
  299. public function render($request, Exception $exception)
  300. {
  301. // Convert all non-http exceptions to a proper 500 http exception
  302. // if we don't do this exceptions are shown as a default template
  303. // instead of our own view in resources/views/errors/500.blade.php
  304. if ($this->shouldReport($exception) && !$this->isHttpException($exception) && !config('app.debug')) {
  305. $exception = new HttpException(500, 'Whoops!');
  306. }
  307. return parent::render($request, $exception);
  308. }
  309. }`,
  310. },
  311. ],
  312. },
  313. ],
  314. },
  315. ],
  316. configure: () => [
  317. {
  318. type: StepType.CONFIGURE,
  319. description: getCrashReportModalConfigDescription({
  320. link: 'https://docs.sentry.io/platforms/php/guides/laravel/user-feedback/configuration/#crash-report-modal',
  321. }),
  322. },
  323. ],
  324. verify: () => [],
  325. nextSteps: () => [],
  326. };
  327. const docs: Docs = {
  328. onboarding,
  329. replayOnboardingJsLoader,
  330. customMetricsOnboarding,
  331. crashReportOnboarding,
  332. };
  333. export default docs;