laravel.tsx 10 KB

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