serverlesscloud.tsx 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. import {Layout, LayoutProps} from 'sentry/components/onboarding/gettingStartedDoc/layout';
  2. import {ModuleProps} from 'sentry/components/onboarding/gettingStartedDoc/sdkDocumentation';
  3. import {StepType} from 'sentry/components/onboarding/gettingStartedDoc/step';
  4. import {getUploadSourceMapsStep} from 'sentry/components/onboarding/gettingStartedDoc/utils';
  5. import {PlatformKey} from 'sentry/data/platformCategories';
  6. import {t, tct} from 'sentry/locale';
  7. import type {Organization} from 'sentry/types';
  8. type StepProps = {
  9. newOrg: boolean;
  10. organization: Organization;
  11. platformKey: PlatformKey;
  12. projectId: string;
  13. sentryInitContent: string;
  14. };
  15. const performanceIntegrations: string[] = [
  16. `// enable HTTP calls tracing
  17. new Sentry.Integrations.Http({ tracing: true }),`,
  18. `// enable Express.js middleware tracing
  19. new Sentry.Integrations.Express({ app }),`,
  20. `// Automatically instrument Node.js libraries and frameworks
  21. ...Sentry.autoDiscoverNodePerformanceMonitoringIntegrations(),`,
  22. ];
  23. const performanceOtherConfig = `environment: params.INSTANCE_NAME,
  24. // Performance Monitoring
  25. // Capture 100% of the transactions, reduce in production!
  26. tracesSampleRate: 1.0, `;
  27. export const steps = ({
  28. sentryInitContent,
  29. ...props
  30. }: Partial<StepProps> = {}): LayoutProps['steps'] => [
  31. {
  32. type: StepType.INSTALL,
  33. description: (
  34. <p>{tct('Add [code:@sentry/node] as a dependency:', {code: <code />})}</p>
  35. ),
  36. configurations: [
  37. {
  38. language: 'bash',
  39. code: `cloud install @sentry/node:`,
  40. },
  41. ],
  42. },
  43. {
  44. type: StepType.CONFIGURE,
  45. description: t('Sentry should be initialized as early in your app as possible.'),
  46. configurations: [
  47. {
  48. language: 'javascript',
  49. code: `
  50. import api from "@serverless/cloud";
  51. import * as Sentry from "@sentry/node";
  52. // or using CommonJS
  53. // const api = require("@serverless/cloud");
  54. // const Sentry = require('@sentry/node');
  55. Sentry.init({
  56. ${sentryInitContent},
  57. });
  58. // RequestHandler creates a separate execution context, so that all
  59. // transactions/spans/breadcrumbs are isolated across requests
  60. api.use(Sentry.Handlers.requestHandler());
  61. // TracingHandler creates a trace for every incoming request
  62. api.use(Sentry.Handlers.tracingHandler());
  63. // All controllers should live here
  64. api.get("/", function rootHandler(req, res) {
  65. res.end("Hello world!");
  66. });
  67. // The error handler must be before any other error middleware and after all controllers
  68. api.use(Sentry.Handlers.errorHandler());
  69. // Optional fallthrough error handler
  70. api.use(function onError(err, req, res, next) {
  71. // The error id is attached to \`res.sentry\` to be returned
  72. // and optionally displayed to the user for support.
  73. res.statusCode = 500;
  74. res.end(res.sentry + "\\n");
  75. });
  76. `,
  77. },
  78. ],
  79. },
  80. getUploadSourceMapsStep({
  81. guideLink:
  82. 'https://docs.sentry.io/platforms/node/guides/serverless-cloud/sourcemaps/',
  83. ...props,
  84. }),
  85. {
  86. type: StepType.VERIFY,
  87. description: t(
  88. "This snippet contains an intentional error and can be used as a test to make sure that everything's working as expected."
  89. ),
  90. configurations: [
  91. {
  92. language: 'javascript',
  93. code: `
  94. api.get("/debug-sentry", function mainHandler(req, res) {
  95. throw new Error("My first Sentry error!");
  96. });
  97. `,
  98. },
  99. ],
  100. },
  101. ];
  102. export function GettingStartedWithServerlesscloud({
  103. dsn,
  104. organization,
  105. newOrg,
  106. platformKey,
  107. projectId,
  108. }: ModuleProps) {
  109. let sentryInitContent: string[] = [`dsn: "${dsn}",`];
  110. const integrations = [...performanceIntegrations];
  111. const otherConfigs = [performanceOtherConfig];
  112. if (integrations.length > 0) {
  113. sentryInitContent = sentryInitContent.concat('integrations: [', integrations, '],');
  114. }
  115. if (otherConfigs.length > 0) {
  116. sentryInitContent = sentryInitContent.concat(otherConfigs);
  117. }
  118. return (
  119. <Layout
  120. steps={steps({
  121. sentryInitContent: sentryInitContent.join('\n'),
  122. organization,
  123. newOrg,
  124. platformKey,
  125. projectId,
  126. })}
  127. newOrg={newOrg}
  128. platformKey={platformKey}
  129. />
  130. );
  131. }
  132. export default GettingStartedWithServerlesscloud;