serverlesscloud.tsx 3.8 KB

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