serverlesscloud.tsx 3.8 KB

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