azurefunctions.tsx 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. getDefaultInitParams,
  8. getDefaultNodeImports,
  9. getInstallSnippet,
  10. getProductInitParams,
  11. getProductIntegrations,
  12. getProductSelectionMap,
  13. joinWithIndentation,
  14. } from 'sentry/utils/gettingStartedDocs/node';
  15. interface StepsParams {
  16. importContent: string;
  17. initContent: string;
  18. installSnippetNpm: string;
  19. installSnippetYarn: string;
  20. sourceMapStep: StepProps;
  21. }
  22. export const steps = ({
  23. installSnippetYarn,
  24. installSnippetNpm,
  25. importContent,
  26. initContent,
  27. sourceMapStep,
  28. }: StepsParams): LayoutProps['steps'] => [
  29. {
  30. type: StepType.INSTALL,
  31. description: t('Add the Sentry Node SDK as a dependency:'),
  32. configurations: [
  33. {
  34. code: [
  35. {
  36. label: 'npm',
  37. value: 'npm',
  38. language: 'bash',
  39. code: installSnippetNpm,
  40. },
  41. {
  42. label: 'yarn',
  43. value: 'yarn',
  44. language: 'bash',
  45. code: installSnippetYarn,
  46. },
  47. ],
  48. },
  49. ],
  50. },
  51. {
  52. type: StepType.CONFIGURE,
  53. description: t('To set up Sentry error logging for an Azure Function:'),
  54. configurations: [
  55. {
  56. language: 'javascript',
  57. code: `
  58. "use strict";
  59. ${importContent}
  60. Sentry.init({
  61. ${initContent}
  62. });
  63. module.exports = async function (context, req) {
  64. try {
  65. await notExistFunction();
  66. } catch (e) {
  67. Sentry.captureException(e);
  68. await Sentry.flush(2000);
  69. }
  70. context.res = {
  71. status: 200,
  72. body: "Hello from Azure Cloud Function!",
  73. };
  74. };
  75. `,
  76. },
  77. {
  78. language: 'javascript',
  79. description: (
  80. <p>
  81. {tct(
  82. 'Note: You need to call both [captureExceptionCode:captureException] and [flushCode:flush] for captured events to be successfully delivered to Sentry.',
  83. {captureExceptionCode: <code />, flushCode: <code />}
  84. )}
  85. </p>
  86. ),
  87. },
  88. ],
  89. },
  90. sourceMapStep,
  91. ];
  92. export function GettingStartedWithAzurefunctions({
  93. dsn,
  94. newOrg,
  95. platformKey,
  96. activeProductSelection = [],
  97. organization,
  98. projectId,
  99. ...props
  100. }: ModuleProps) {
  101. const productSelection = getProductSelectionMap(activeProductSelection);
  102. const imports = getDefaultNodeImports({productSelection});
  103. const integrations = getProductIntegrations({productSelection});
  104. const integrationParam =
  105. integrations.length > 0
  106. ? `integrations: [\n${joinWithIndentation(integrations)}\n],`
  107. : null;
  108. const initContent = joinWithIndentation([
  109. ...getDefaultInitParams({dsn}),
  110. ...(integrationParam ? [integrationParam] : []),
  111. ...getProductInitParams({productSelection}),
  112. ]);
  113. return (
  114. <Layout
  115. steps={steps({
  116. installSnippetNpm: getInstallSnippet({productSelection, packageManager: 'npm'}),
  117. installSnippetYarn: getInstallSnippet({productSelection, packageManager: 'yarn'}),
  118. importContent: imports.join('\n'),
  119. initContent,
  120. sourceMapStep: getUploadSourceMapsStep({
  121. guideLink:
  122. 'https://docs.sentry.io/platforms/node/guides/azure-functions/sourcemaps/',
  123. organization,
  124. platformKey,
  125. projectId,
  126. newOrg,
  127. }),
  128. })}
  129. newOrg={newOrg}
  130. platformKey={platformKey}
  131. {...props}
  132. />
  133. );
  134. }
  135. export default GettingStartedWithAzurefunctions;