gcpfunctions.tsx 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. import {StepType} from 'sentry/components/onboarding/gettingStartedDoc/step';
  2. import type {
  3. Docs,
  4. DocsParams,
  5. OnboardingConfig,
  6. } from 'sentry/components/onboarding/gettingStartedDoc/types';
  7. import {getUploadSourceMapsStep} from 'sentry/components/onboarding/gettingStartedDoc/utils';
  8. import {ProductSolution} from 'sentry/components/onboarding/productSelection';
  9. import {t, tct} from 'sentry/locale';
  10. import type {ProductSelectionMap} from 'sentry/utils/gettingStartedDocs/node';
  11. import {getDefaulServerlessImports} from 'sentry/utils/gettingStartedDocs/node';
  12. type Params = DocsParams;
  13. const productSelection = (params: Params): ProductSelectionMap => {
  14. return {
  15. [ProductSolution.ERROR_MONITORING]: true,
  16. [ProductSolution.PROFILING]: params.isProfilingSelected,
  17. [ProductSolution.PERFORMANCE_MONITORING]: params.isPerformanceSelected,
  18. [ProductSolution.SESSION_REPLAY]: params.isReplaySelected,
  19. };
  20. };
  21. const getInstallSnippet = (params: Params) => `
  22. dependencies: {
  23. //...
  24. "@sentry/serverless": "^7",${
  25. params.isProfilingSelected
  26. ? `
  27. "@sentry/profiling-node": "^1",`
  28. : ''
  29. }
  30. //...
  31. }`;
  32. const getSdkSetupSnippet = (params: Params) => `
  33. ${getDefaulServerlessImports({productSelection: productSelection(params)}).join('\n')}
  34. Sentry.GCPFunction.init({
  35. dsn: "${params.dsn}",
  36. integrations: [${
  37. params.isProfilingSelected
  38. ? `
  39. new ProfilingIntegration(),`
  40. : ''
  41. }
  42. ],${
  43. params.isPerformanceSelected
  44. ? `
  45. // Performance Monitoring
  46. tracesSampleRate: 1.0, // Capture 100% of the transactions`
  47. : ''
  48. }${
  49. params.isProfilingSelected
  50. ? `
  51. // Set sampling rate for profiling - this is relative to tracesSampleRate
  52. profilesSampleRate: 1.0,`
  53. : ''
  54. }
  55. });
  56. // Use wrapHttpFunction to instrument your http functions
  57. exports.helloHttp = Sentry.GCPFunction.wrapHttpFunction((req, res) => {
  58. /* Your function code */
  59. });
  60. // Use wrapEventFunction to instrument your background functions
  61. exports.helloEvents = Sentry.GCPFunction.wrapEventFunction(
  62. (data, context, callback) => {
  63. /* Your function code */
  64. }
  65. );
  66. // Use wrapCloudEventFunction to instrument your CloudEvent functions
  67. exports.helloEvents = Sentry.GCPFunction.wrapCloudEventFunction(
  68. (context, callback) => {
  69. /* Your function code */
  70. }
  71. );`;
  72. const getVerifySnippet = () => `
  73. exports.helloHttp = Sentry.GCPFunction.wrapHttpFunction((req, res) => {
  74. throw new Error("oh, hello there!");
  75. });`;
  76. const onboarding: OnboardingConfig = {
  77. install: params => [
  78. {
  79. type: StepType.INSTALL,
  80. description: tct(
  81. 'Add the Sentry Serverless SDK as a dependency to your [code:package.json]:',
  82. {code: <code />}
  83. ),
  84. configurations: [
  85. {
  86. language: 'json',
  87. code: getInstallSnippet(params),
  88. },
  89. ],
  90. },
  91. ],
  92. configure: params => [
  93. {
  94. type: StepType.CONFIGURE,
  95. description: tct('Use the Sentry SDK to wrap your functions:', {
  96. code: <code />,
  97. }),
  98. configurations: [
  99. {
  100. language: 'javascript',
  101. code: getSdkSetupSnippet(params),
  102. },
  103. ],
  104. },
  105. getUploadSourceMapsStep({
  106. guideLink: 'https://docs.sentry.io/platforms/node/guides/gcp-functions/sourcemaps/',
  107. ...params,
  108. }),
  109. ],
  110. verify: () => [
  111. {
  112. type: StepType.VERIFY,
  113. description: t(
  114. "This snippet contains an intentional error and can be used as a test to make sure that everything's working as expected."
  115. ),
  116. configurations: [
  117. {
  118. language: 'javascript',
  119. code: getVerifySnippet(),
  120. },
  121. ],
  122. },
  123. ],
  124. };
  125. const docs: Docs = {
  126. onboarding,
  127. };
  128. export default docs;