laravel.tsx 11 KB

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