gcpfunctions.tsx 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 {t, tct} from 'sentry/locale';
  5. import {
  6. getDefaulServerlessImports,
  7. getDefaultInitParams,
  8. getProductInitParams,
  9. getProductIntegrations,
  10. getProductSelectionMap,
  11. joinWithIndentation,
  12. } from 'sentry/utils/gettingStartedDocs/node';
  13. interface StepProps {
  14. importContent: string;
  15. initContent: string;
  16. installSnippet: string;
  17. }
  18. export const steps = ({
  19. installSnippet,
  20. importContent,
  21. initContent,
  22. }: StepProps): LayoutProps['steps'] => [
  23. {
  24. type: StepType.INSTALL,
  25. description: (
  26. <p>
  27. {tct(
  28. 'Add the Sentry Serverless SDK as a dependency to your [code:package.json]:',
  29. {code: <code />}
  30. )}
  31. </p>
  32. ),
  33. configurations: [
  34. {
  35. language: 'json',
  36. code: installSnippet,
  37. },
  38. ],
  39. },
  40. {
  41. type: StepType.CONFIGURE,
  42. description: (
  43. <p>
  44. {tct('Use the Sentry SDK to wrap your functions:', {
  45. code: <code />,
  46. })}
  47. </p>
  48. ),
  49. configurations: [
  50. {
  51. language: 'javascript',
  52. code: `
  53. ${importContent}
  54. Sentry.GCPFunction.init({
  55. ${initContent}
  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. `,
  74. },
  75. ],
  76. },
  77. {
  78. type: StepType.VERIFY,
  79. description: t(
  80. "This snippet contains an intentional error and can be used as a test to make sure that everything's working as expected."
  81. ),
  82. configurations: [
  83. {
  84. language: 'javascript',
  85. code: `
  86. exports.helloHttp = Sentry.GCPFunction.wrapHttpFunction((req, res) => {
  87. throw new Error("oh, hello there!");
  88. });
  89. `,
  90. },
  91. ],
  92. },
  93. ];
  94. export function GettingStartedWithGCPFunctions({
  95. dsn,
  96. newOrg,
  97. platformKey,
  98. activeProductSelection = [],
  99. }: ModuleProps) {
  100. const productSelection = getProductSelectionMap(activeProductSelection);
  101. const installSnippet: string[] = [
  102. 'dependencies: {',
  103. ' //...',
  104. ` "@sentry/serverless": "^7",`,
  105. ];
  106. if (productSelection.profiling) {
  107. installSnippet.push(` "@sentry/profiling-node": "^1",`);
  108. }
  109. installSnippet.push(' //...', '}');
  110. const imports = getDefaulServerlessImports({productSelection});
  111. const integrations = getProductIntegrations({productSelection});
  112. const integrationParam =
  113. integrations.length > 0
  114. ? `integrations: [\n${joinWithIndentation(integrations)}\n],`
  115. : null;
  116. const initContent = joinWithIndentation([
  117. ...getDefaultInitParams({dsn}),
  118. ...(integrationParam ? [integrationParam] : []),
  119. ...getProductInitParams({productSelection}),
  120. ]);
  121. return (
  122. <Layout
  123. steps={steps({
  124. installSnippet: installSnippet.join('\n'),
  125. importContent: imports.join('\n'),
  126. initContent,
  127. })}
  128. newOrg={newOrg}
  129. platformKey={platformKey}
  130. />
  131. );
  132. }
  133. export default GettingStartedWithGCPFunctions;