laravel.tsx 9.7 KB

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