gcpfunctions.tsx 3.6 KB

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