laravel.tsx 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. import ExternalLink from 'sentry/components/links/externalLink';
  2. import {StepType} from 'sentry/components/onboarding/gettingStartedDoc/step';
  3. import {
  4. Docs,
  5. DocsParams,
  6. OnboardingConfig,
  7. } from 'sentry/components/onboarding/gettingStartedDoc/types';
  8. import replayOnboardingJsLoader from 'sentry/gettingStartedDocs/javascript/jsLoader/jsLoader';
  9. import {t, tct} from 'sentry/locale';
  10. type Params = DocsParams;
  11. const getExceptionHandlerSnippet = () => `
  12. public function register() {
  13. $this->reportable(function (Throwable $e) {
  14. if (app()->bound('sentry')) {
  15. app('sentry')->captureException($e);
  16. }
  17. });
  18. }`;
  19. const getConfigureSnippet = (params: Params) =>
  20. `SENTRY_LARAVEL_DSN=${params.dsn}${
  21. params.isPerformanceSelected
  22. ? `
  23. # Specify a fixed sample rate
  24. SENTRY_TRACES_SAMPLE_RATE=1.0`
  25. : ''
  26. }${
  27. params.isProfilingSelected
  28. ? `
  29. # Set a sampling rate for profiling - this is relative to traces_sample_rate
  30. SENTRY_PROFILES_SAMPLE_RATE=1.0`
  31. : ''
  32. }`;
  33. const getMetricsInstallSnippet = () => `
  34. composer install sentry/sentry-laravel
  35. composer update sentry/sentry-laravel -W`;
  36. const getMetricsVerifySnippet = () => `
  37. use function \\Sentry\\metrics;
  38. // Add 4 to a counter named 'hits'
  39. metrics()->increment('hits', 4);
  40. metrics()->flush();
  41. // We recommend registering the flush call in a shutdown function
  42. register_shutdown_function(static fn () => metrics()->flush());
  43. // Or call flush in a Terminable Middleware
  44. use Closure;
  45. use Illuminate\\Http\\Request;
  46. use Symfony\\Component\\HttpFoundation\\Response;
  47. use function \\Sentry\\metrics;
  48. class SentryMetricsMiddleware
  49. {
  50. public function handle(Request $request, Closure $next): Response
  51. {
  52. return $next($request);
  53. }
  54. public function terminate(Request $request, Response $response): void
  55. {
  56. metrics()->flush();
  57. }
  58. }`;
  59. const onboarding: OnboardingConfig = {
  60. introduction: () =>
  61. tct(
  62. 'This guide is for Laravel 8+. We also provide instructions for [otherVersionsLink:other versions] as well as [lumenSpecificLink:Lumen-specific instructions].',
  63. {
  64. otherVersionsLink: (
  65. <ExternalLink href="https://docs.sentry.io/platforms/php/guides/laravel/other-versions/" />
  66. ),
  67. lumenSpecificLink: (
  68. <ExternalLink href="https://docs.sentry.io/platforms/php/guides/laravel/other-versions/lumen/" />
  69. ),
  70. }
  71. ),
  72. install: (params: Params) => [
  73. {
  74. type: StepType.INSTALL,
  75. configurations: [
  76. {
  77. description: tct('Install the [code:sentry/sentry-laravel] package:', {
  78. code: <code />,
  79. }),
  80. language: 'bash',
  81. code: `composer require sentry/sentry-laravel`,
  82. },
  83. ...(params.isProfilingSelected
  84. ? [
  85. {
  86. description: t('Install the Excimer extension via PECL:'),
  87. language: 'bash',
  88. code: 'pecl install excimer',
  89. },
  90. ]
  91. : []),
  92. {
  93. description: tct(
  94. 'Enable capturing unhandled exception to report to Sentry by making the following change to your [code:App/Exceptions/Handler.php]:',
  95. {
  96. code: <code />,
  97. }
  98. ),
  99. language: 'php',
  100. code: getExceptionHandlerSnippet(),
  101. },
  102. ],
  103. },
  104. ],
  105. configure: (params: Params) => [
  106. {
  107. type: StepType.CONFIGURE,
  108. configurations: [
  109. {
  110. description: t('Configure the Sentry DSN with this command:'),
  111. language: 'shell',
  112. code: `php artisan sentry:publish --dsn=${params.dsn}`,
  113. },
  114. {
  115. description: tct(
  116. '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:',
  117. {sentryPHPCode: <code />, dsnCode: <code />, envCode: <code />}
  118. ),
  119. language: 'shell',
  120. code: getConfigureSnippet(params),
  121. },
  122. ],
  123. },
  124. ],
  125. verify: () => [
  126. {
  127. type: StepType.VERIFY,
  128. configurations: [
  129. {
  130. description: tct(
  131. 'You can test your configuration using the provided [code:sentry:test] artisan command:',
  132. {
  133. code: <code />,
  134. }
  135. ),
  136. language: 'shell',
  137. code: 'php artisan sentry:test',
  138. },
  139. ],
  140. },
  141. ],
  142. nextSteps: () => [],
  143. };
  144. const customMetricsOnboarding: OnboardingConfig = {
  145. install: () => [
  146. {
  147. type: StepType.INSTALL,
  148. description: tct(
  149. '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',
  150. {
  151. codeVersionLaravel: <code />,
  152. codeVersion: <code />,
  153. }
  154. ),
  155. configurations: [
  156. {
  157. language: 'bash',
  158. code: getMetricsInstallSnippet(),
  159. },
  160. ],
  161. },
  162. ],
  163. configure: () => [
  164. {
  165. type: StepType.CONFIGURE,
  166. description: tct(
  167. 'Once the SDK is installed or updated, you can enable code locations being emitted with your metricsin your [code:config/sentry.php] file:',
  168. {
  169. code: <code />,
  170. }
  171. ),
  172. configurations: [
  173. {
  174. code: [
  175. {
  176. label: 'PHP',
  177. value: 'php',
  178. language: 'php',
  179. code: `'metric_code_locations' => true,`,
  180. },
  181. ],
  182. },
  183. ],
  184. },
  185. ],
  186. verify: () => [
  187. {
  188. type: StepType.VERIFY,
  189. description: tct(
  190. "Then you'll be able to add metrics as [codeCounters:counters], [codeSets:sets], [codeDistribution:distributions], and [codeGauge:gauges]. Try out this example:",
  191. {
  192. codeCounters: <code />,
  193. codeSets: <code />,
  194. codeDistribution: <code />,
  195. codeGauge: <code />,
  196. codeNamespace: <code />,
  197. }
  198. ),
  199. configurations: [
  200. {
  201. code: [
  202. {
  203. label: 'PHP',
  204. value: 'php',
  205. language: 'php',
  206. code: getMetricsVerifySnippet(),
  207. },
  208. ],
  209. },
  210. {
  211. description: t(
  212. 'With a bit of delay you can see the data appear in the Sentry UI.'
  213. ),
  214. },
  215. {
  216. description: tct(
  217. 'Learn more about metrics and how to configure them, by reading the [docsLink:docs].',
  218. {
  219. docsLink: (
  220. <ExternalLink href="https://github.com/getsentry/sentry-laravel/discussions/823" />
  221. ),
  222. }
  223. ),
  224. },
  225. ],
  226. },
  227. ],
  228. };
  229. const docs: Docs = {
  230. onboarding,
  231. replayOnboardingJsLoader,
  232. customMetricsOnboarding,
  233. };
  234. export default docs;