quickStartEntries.tsx 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. import {Fragment} from 'react';
  2. import merge from 'lodash/merge';
  3. import {CodeSnippet} from 'sentry/components/codeSnippet';
  4. import ExternalLink from 'sentry/components/links/externalLink';
  5. import {t, tct} from 'sentry/locale';
  6. export interface QuickStartProps {
  7. dsnKey?: string;
  8. orgSlug?: string;
  9. slug?: string;
  10. }
  11. const VALUE_DEFAULTS = {
  12. dsnKey: '<my-dsn-key>',
  13. orgSlug: '<my-organization-slug>',
  14. slug: '<my-monitor-slug>',
  15. };
  16. function withDefaultProps(props: QuickStartProps): Required<QuickStartProps> {
  17. return merge(VALUE_DEFAULTS, props);
  18. }
  19. export function PythonCronQuickStart(props: QuickStartProps) {
  20. const {slug} = withDefaultProps(props);
  21. const code = `import sentry_sdk
  22. from sentry_sdk.crons import monitor
  23. # Add this decorator to instrument your python function
  24. @monitor(monitor_slug='${slug}')
  25. def tell_the_world(msg):
  26. print(msg)`;
  27. return (
  28. <Fragment>
  29. <div>
  30. {tct(
  31. '[installLink:Install and configure] the Sentry Python SDK (min v1.17.0), then instrument your monitor:',
  32. {
  33. installLink: <ExternalLink href="https://docs.sentry.io/platforms/python/" />,
  34. }
  35. )}
  36. </div>
  37. <CodeSnippet language="python">{code}</CodeSnippet>
  38. </Fragment>
  39. );
  40. }
  41. export function PythonCeleryCronQuickStart(props: QuickStartProps) {
  42. const {slug, dsnKey} = withDefaultProps(props);
  43. const setupCode = `import sentry_sdk
  44. from sentry_sdk.crons import monitor
  45. from sentry_sdk.integrations.celery import CeleryIntegration
  46. # @signals.celeryd_init.connect
  47. @signals.beat_init.connect
  48. def init_sentry(**kwargs):
  49. sentry_sdk.init(
  50. dsn='${dsnKey}',
  51. integrations=[CeleryIntegration()],
  52. )
  53. `;
  54. const linkTaskCode = `@app.task
  55. @monitor(monitor_slug='${slug}')
  56. def tell_the_world(msg):
  57. print(msg)
  58. `;
  59. return (
  60. <Fragment>
  61. <div>
  62. {tct(
  63. '[installLink:Install and configure] the Sentry Python SDK (min v1.17.0), then initialize Sentry either in [celerydInit:celeryd_init] or [beatInit:beat_init] signal:',
  64. {
  65. celerydInit: <code />,
  66. beatInit: <code />,
  67. installLink: (
  68. <ExternalLink href="https://docs.sentry.io/platforms/python/guides/celery/" />
  69. ),
  70. }
  71. )}
  72. </div>
  73. <CodeSnippet language="python">{setupCode}</CodeSnippet>
  74. <div>{t('Link your Celery task to your Monitor:')}</div>
  75. <CodeSnippet language="python">{linkTaskCode}</CodeSnippet>
  76. </Fragment>
  77. );
  78. }
  79. export function CLICronQuickStart(props: QuickStartProps) {
  80. const {slug, dsnKey} = withDefaultProps(props);
  81. const script = `# Example for a Python script:
  82. export SENTRY_DSN=${dsnKey}
  83. sentry-cli monitors run ${slug} -- python path/to/file`;
  84. return (
  85. <Fragment>
  86. <div>
  87. {tct(
  88. 'Make sure to [installLink:install the Sentry CLI] (min v2.16.1), then instrument your monitor:',
  89. {
  90. installLink: (
  91. <ExternalLink href="https://docs.sentry.io/product/cli/installation/" />
  92. ),
  93. }
  94. )}
  95. </div>
  96. <CodeSnippet language="bash">{script}</CodeSnippet>
  97. </Fragment>
  98. );
  99. }
  100. export function CurlCronQuickStart(props: QuickStartProps) {
  101. const {slug, orgSlug, dsnKey} = withDefaultProps(props);
  102. const checkInSuccessCode = `# Notify Sentry your job is running:
  103. curl -X POST \\
  104. 'https://sentry.io/api/0/organizations/${orgSlug}/monitors/${slug}/checkins/' \\
  105. --header 'Authorization: DSN ${dsnKey}' \\
  106. --header 'Content-Type: application/json' \\
  107. --data-raw '{"status": "in_progress"}'
  108. # Execute your scheduled task here...
  109. # Notify Sentry your job has completed successfully:
  110. curl -X PUT \\
  111. 'https://sentry.io/api/0/organizations/${orgSlug}/monitors/${slug}/checkins/latest/' \\
  112. --header 'Authorization: DSN ${dsnKey}' \\
  113. --header 'Content-Type: application/json' \\
  114. --data-raw '{"status": "ok"}'`;
  115. const checkInFailCode = `# Notify Sentry your job has failed:
  116. curl -X PUT \\
  117. 'https://sentry.io/api/0/organizations/${orgSlug}/monitors/${slug}/checkins/latest/' \\
  118. --header 'Authorization: DSN ${dsnKey}' \\
  119. --header 'Content-Type: application/json' \\
  120. --data-raw '{"status": "error"}'`;
  121. return (
  122. <Fragment>
  123. <CodeSnippet language="bash">{checkInSuccessCode}</CodeSnippet>
  124. <div>{t('To notify Sentry if your job execution fails')}</div>
  125. <CodeSnippet language="bash">{checkInFailCode}</CodeSnippet>
  126. </Fragment>
  127. );
  128. }
  129. export function PHPCronQuickStart(props: QuickStartProps) {
  130. const {slug} = withDefaultProps(props);
  131. const checkInSuccessCode = `// ๐ŸŸก Notify Sentry your job is running:
  132. $event = Event::createCheckIn();
  133. $checkIn = new CheckIn(
  134. monitorSlug: '${slug}',
  135. status: CheckInStatus::inProgress(),
  136. );
  137. $event->setCheckIn($checkIn);
  138. SentrySdk::getCurrentHub()->captureEvent($event);
  139. // Execute your scheduled task here...
  140. // ๐ŸŸข Notify Sentry your job has completed successfully:
  141. $event = Event::createCheckIn();
  142. $event->setCheckIn(new CheckIn(
  143. id: $checkIn->getId(),
  144. monitorSlug: '${slug}',
  145. status: CheckInStatus::ok(),
  146. ));
  147. SentrySdk::getCurrentHub()->captureEvent($event);`;
  148. const checkInFailCode = `// ๐Ÿ”ด Notify Sentry your job has failed:
  149. $event = Event::createCheckIn();
  150. $event->setCheckIn(new CheckIn(
  151. id: $checkIn->getId(),
  152. monitorSlug: '${slug}',
  153. status: CheckInStatus::error(),
  154. ));
  155. SentrySdk::getCurrentHub()->captureEvent($event);`;
  156. return (
  157. <Fragment>
  158. <div>
  159. {tct(
  160. '[installLink:Install and configure] the Sentry PHP SDK (min v3.16.0), then instrument your monitor:',
  161. {
  162. installLink: <ExternalLink href="https://docs.sentry.io/platforms/php/" />,
  163. }
  164. )}
  165. </div>
  166. <CodeSnippet language="php">{checkInSuccessCode}</CodeSnippet>
  167. <div>{t('To notify Sentry if your job execution fails')}</div>
  168. <CodeSnippet language="php">{checkInFailCode}</CodeSnippet>
  169. </Fragment>
  170. );
  171. }
  172. export function PHPLaravelCronQuickStart(props: QuickStartProps) {
  173. const {slug} = withDefaultProps(props);
  174. const code = `protected function schedule(Schedule $schedule)
  175. {
  176. $schedule->command('emails:send')
  177. ->everyHour()
  178. ->sentryMonitor('${slug}'); // add this line
  179. }`;
  180. return (
  181. <Fragment>
  182. <div>
  183. {tct(
  184. '[installLink:Install and configure] the Sentry PHP Laravel SDK (min v3.3.1), then add the [sentryMonitor:sentryMonitor()] call to your scheduled tasks defined in your [kernel:app/Console/Kernel.php] file:',
  185. {
  186. sentryMonitor: <code />,
  187. kernel: <code />,
  188. installLink: (
  189. <ExternalLink href="https://docs.sentry.io/platforms/php/guides/laravel/" />
  190. ),
  191. }
  192. )}
  193. </div>
  194. <CodeSnippet language="php">{code}</CodeSnippet>
  195. </Fragment>
  196. );
  197. }
  198. export function NodeJSCronQuickStart(props: QuickStartProps) {
  199. const {slug} = withDefaultProps(props);
  200. const checkInSuccessCode = `// ๐ŸŸก Notify Sentry your job is running:
  201. const checkInId = Sentry.captureCheckIn({
  202. monitorSlug: "${slug}",
  203. status: "in_progress",
  204. });
  205. // Execute your scheduled task here...
  206. // ๐ŸŸข Notify Sentry your job has completed successfully:
  207. Sentry.captureCheckIn({
  208. checkInId,
  209. monitorSlug: "${slug}",
  210. status: "ok",
  211. });`;
  212. const checkInFailCode = `// ๐Ÿ”ด Notify Sentry your job has failed:
  213. Sentry.captureCheckIn({
  214. checkInId,
  215. monitorSlug: "${slug}",
  216. status: "error",
  217. });`;
  218. return (
  219. <Fragment>
  220. <div>
  221. {tct(
  222. '[installLink:Install and configure] the Sentry Node SDK (min v7.52), then instrument your monitor:',
  223. {
  224. installLink: <ExternalLink href="https://docs.sentry.io/platforms/node/" />,
  225. }
  226. )}
  227. </div>
  228. <CodeSnippet language="javascript">{checkInSuccessCode}</CodeSnippet>
  229. <div>{t('To notify Sentry if your job execution fails')}</div>
  230. <CodeSnippet language="javascript">{checkInFailCode}</CodeSnippet>
  231. </Fragment>
  232. );
  233. }