gcpfunctions.tsx 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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 {getJSServerMetricsOnboarding} from 'sentry/components/onboarding/gettingStartedDoc/utils/metricsOnboarding';
  9. import {ProductSolution} from 'sentry/components/onboarding/productSelection';
  10. import {t, tct} from 'sentry/locale';
  11. import type {ProductSelectionMap} from 'sentry/utils/gettingStartedDocs/node';
  12. import {getDefaulServerlessImports} from 'sentry/utils/gettingStartedDocs/node';
  13. type Params = DocsParams;
  14. const productSelection = (params: Params): ProductSelectionMap => {
  15. return {
  16. [ProductSolution.ERROR_MONITORING]: true,
  17. [ProductSolution.PROFILING]: params.isProfilingSelected,
  18. [ProductSolution.PERFORMANCE_MONITORING]: params.isPerformanceSelected,
  19. [ProductSolution.SESSION_REPLAY]: params.isReplaySelected,
  20. };
  21. };
  22. const getInstallSnippet = (params: Params) => `
  23. dependencies: {
  24. //...
  25. "@sentry/serverless": "^7",${
  26. params.isProfilingSelected
  27. ? `
  28. "@sentry/profiling-node": "^1",`
  29. : ''
  30. }
  31. //...
  32. }`;
  33. const getSdkSetupSnippet = (params: Params) => `
  34. ${getDefaulServerlessImports({productSelection: productSelection(params)}).join('\n')}
  35. Sentry.GCPFunction.init({
  36. dsn: "${params.dsn}",
  37. integrations: [${
  38. params.isProfilingSelected
  39. ? `
  40. new ProfilingIntegration(),`
  41. : ''
  42. }
  43. ],${
  44. params.isPerformanceSelected
  45. ? `
  46. // Performance Monitoring
  47. tracesSampleRate: 1.0, // Capture 100% of the transactions`
  48. : ''
  49. }${
  50. params.isProfilingSelected
  51. ? `
  52. // Set sampling rate for profiling - this is relative to tracesSampleRate
  53. profilesSampleRate: 1.0,`
  54. : ''
  55. }
  56. });
  57. // Use wrapHttpFunction to instrument your http functions
  58. exports.helloHttp = Sentry.GCPFunction.wrapHttpFunction((req, res) => {
  59. /* Your function code */
  60. });
  61. // Use wrapEventFunction to instrument your background functions
  62. exports.helloEvents = Sentry.GCPFunction.wrapEventFunction(
  63. (data, context, callback) => {
  64. /* Your function code */
  65. }
  66. );
  67. // Use wrapCloudEventFunction to instrument your CloudEvent functions
  68. exports.helloEvents = Sentry.GCPFunction.wrapCloudEventFunction(
  69. (context, callback) => {
  70. /* Your function code */
  71. }
  72. );`;
  73. const getVerifySnippet = () => `
  74. exports.helloHttp = Sentry.GCPFunction.wrapHttpFunction((req, res) => {
  75. throw new Error("oh, hello there!");
  76. });`;
  77. const getMetricsConfigureSnippet = (params: DocsParams) => `
  78. Sentry.GCPFunction.init({
  79. dsn: "${params.dsn}",
  80. _experiments: {
  81. metricsAggregator: true,
  82. },
  83. });`;
  84. const onboarding: OnboardingConfig = {
  85. install: params => [
  86. {
  87. type: StepType.INSTALL,
  88. description: tct(
  89. 'Add the Sentry Serverless SDK as a dependency to your [code:package.json]:',
  90. {code: <code />}
  91. ),
  92. configurations: [
  93. {
  94. language: 'json',
  95. code: getInstallSnippet(params),
  96. },
  97. ],
  98. },
  99. ],
  100. configure: params => [
  101. {
  102. type: StepType.CONFIGURE,
  103. description: tct('Use the Sentry SDK to wrap your functions:', {
  104. code: <code />,
  105. }),
  106. configurations: [
  107. {
  108. language: 'javascript',
  109. code: getSdkSetupSnippet(params),
  110. },
  111. ],
  112. },
  113. getUploadSourceMapsStep({
  114. guideLink: 'https://docs.sentry.io/platforms/node/guides/gcp-functions/sourcemaps/',
  115. ...params,
  116. }),
  117. ],
  118. verify: () => [
  119. {
  120. type: StepType.VERIFY,
  121. description: t(
  122. "This snippet contains an intentional error and can be used as a test to make sure that everything's working as expected."
  123. ),
  124. configurations: [
  125. {
  126. language: 'javascript',
  127. code: getVerifySnippet(),
  128. },
  129. ],
  130. },
  131. ],
  132. };
  133. const customMetricsOnboarding: OnboardingConfig = {
  134. install: params => [
  135. {
  136. type: StepType.INSTALL,
  137. description: tct(
  138. 'You need a minimum version [codeVersion:7.91.0] of [codePackage:@sentry/serverless]:',
  139. {
  140. codeVersion: <code />,
  141. codePackage: <code />,
  142. }
  143. ),
  144. configurations: [{language: 'json', code: getInstallSnippet(params)}],
  145. },
  146. ],
  147. configure: params => [
  148. {
  149. type: StepType.CONFIGURE,
  150. description: tct(
  151. 'To enable capturing metrics, you first need to add the [codeIntegration:metricsAggregator] experiment to your [codeNamespace:Sentry.init] call in your main process.',
  152. {
  153. codeIntegration: <code />,
  154. codeNamespace: <code />,
  155. }
  156. ),
  157. configurations: [
  158. {
  159. code: [
  160. {
  161. label: 'JavaScript',
  162. value: 'javascript',
  163. language: 'javascript',
  164. code: getMetricsConfigureSnippet(params),
  165. },
  166. ],
  167. },
  168. ],
  169. },
  170. ],
  171. verify: getJSServerMetricsOnboarding().verify,
  172. };
  173. const docs: Docs = {
  174. onboarding,
  175. customMetricsOnboarding,
  176. };
  177. export default docs;