gcpfunctions.tsx 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. import {Layout, LayoutProps} from 'sentry/components/onboarding/gettingStartedDoc/layout';
  2. import {ModuleProps} from 'sentry/components/onboarding/gettingStartedDoc/sdkDocumentation';
  3. import {StepProps, StepType} from 'sentry/components/onboarding/gettingStartedDoc/step';
  4. import {getUploadSourceMapsStep} from 'sentry/components/onboarding/gettingStartedDoc/utils';
  5. import {t, tct} from 'sentry/locale';
  6. import {
  7. getDefaulServerlessImports,
  8. getDefaultInitParams,
  9. getProductInitParams,
  10. getProductIntegrations,
  11. getProductSelectionMap,
  12. joinWithIndentation,
  13. } from 'sentry/utils/gettingStartedDocs/node';
  14. interface StepsParams {
  15. importContent: string;
  16. initContent: string;
  17. installSnippet: string;
  18. sourceMapStep: StepProps;
  19. }
  20. export const steps = ({
  21. installSnippet,
  22. importContent,
  23. initContent,
  24. sourceMapStep,
  25. }: StepsParams): LayoutProps['steps'] => [
  26. {
  27. type: StepType.INSTALL,
  28. description: (
  29. <p>
  30. {tct(
  31. 'Add the Sentry Serverless SDK as a dependency to your [code:package.json]:',
  32. {code: <code />}
  33. )}
  34. </p>
  35. ),
  36. configurations: [
  37. {
  38. language: 'json',
  39. code: installSnippet,
  40. },
  41. ],
  42. },
  43. {
  44. type: StepType.CONFIGURE,
  45. description: (
  46. <p>
  47. {tct('Use the Sentry SDK to wrap your functions:', {
  48. code: <code />,
  49. })}
  50. </p>
  51. ),
  52. configurations: [
  53. {
  54. language: 'javascript',
  55. code: `
  56. ${importContent}
  57. Sentry.GCPFunction.init({
  58. ${initContent}
  59. });
  60. // Use wrapHttpFunction to instrument your http functions
  61. exports.helloHttp = Sentry.GCPFunction.wrapHttpFunction((req, res) => {
  62. /* Your function code */
  63. });
  64. // Use wrapEventFunction to instrument your background functions
  65. exports.helloEvents = Sentry.GCPFunction.wrapEventFunction(
  66. (data, context, callback) => {
  67. /* Your function code */
  68. }
  69. );
  70. // Use wrapCloudEventFunction to instrument your CloudEvent functions
  71. exports.helloEvents = Sentry.GCPFunction.wrapCloudEventFunction(
  72. (context, callback) => {
  73. /* Your function code */
  74. }
  75. );
  76. `,
  77. },
  78. ],
  79. },
  80. sourceMapStep,
  81. {
  82. type: StepType.VERIFY,
  83. description: t(
  84. "This snippet contains an intentional error and can be used as a test to make sure that everything's working as expected."
  85. ),
  86. configurations: [
  87. {
  88. language: 'javascript',
  89. code: `
  90. exports.helloHttp = Sentry.GCPFunction.wrapHttpFunction((req, res) => {
  91. throw new Error("oh, hello there!");
  92. });
  93. `,
  94. },
  95. ],
  96. },
  97. ];
  98. export function GettingStartedWithGCPFunctions({
  99. dsn,
  100. newOrg,
  101. platformKey,
  102. activeProductSelection = [],
  103. organization,
  104. projectId,
  105. ...props
  106. }: ModuleProps) {
  107. const productSelection = getProductSelectionMap(activeProductSelection);
  108. const installSnippet: string[] = [
  109. 'dependencies: {',
  110. ' //...',
  111. ` "@sentry/serverless": "^7",`,
  112. ];
  113. if (productSelection.profiling) {
  114. installSnippet.push(` "@sentry/profiling-node": "^1",`);
  115. }
  116. installSnippet.push(' //...', '}');
  117. const imports = getDefaulServerlessImports({productSelection});
  118. const integrations = getProductIntegrations({productSelection});
  119. const integrationParam =
  120. integrations.length > 0
  121. ? `integrations: [\n${joinWithIndentation(integrations)}\n],`
  122. : null;
  123. const initContent = joinWithIndentation([
  124. ...getDefaultInitParams({dsn}),
  125. ...(integrationParam ? [integrationParam] : []),
  126. ...getProductInitParams({productSelection}),
  127. ]);
  128. return (
  129. <Layout
  130. steps={steps({
  131. installSnippet: installSnippet.join('\n'),
  132. importContent: imports.join('\n'),
  133. initContent,
  134. sourceMapStep: getUploadSourceMapsStep({
  135. guideLink:
  136. 'https://docs.sentry.io/platforms/node/guides/gcp-functions/sourcemaps/',
  137. organization,
  138. platformKey,
  139. projectId,
  140. newOrg,
  141. }),
  142. })}
  143. newOrg={newOrg}
  144. platformKey={platformKey}
  145. {...props}
  146. />
  147. );
  148. }
  149. export default GettingStartedWithGCPFunctions;