node.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. import {ProductSolution} from 'sentry/components/onboarding/productSelection';
  2. type ProductSelectionMap = Record<ProductSolution, boolean>;
  3. /**
  4. * Transforms the product selection array into a map of booleans for each product for easier access.
  5. */
  6. export const getProductSelectionMap = (
  7. activeProductSelection: ProductSolution[]
  8. ): ProductSelectionMap => {
  9. const productSelectionMap: ProductSelectionMap = {
  10. [ProductSolution.ERROR_MONITORING]: false,
  11. [ProductSolution.PROFILING]: false,
  12. [ProductSolution.PERFORMANCE_MONITORING]: false,
  13. [ProductSolution.SESSION_REPLAY]: false,
  14. };
  15. activeProductSelection.forEach(product => {
  16. productSelectionMap[product] = true;
  17. });
  18. return productSelectionMap;
  19. };
  20. /**
  21. * Joins the given lines with the given indentation using \n as delimiter.
  22. */
  23. export function joinWithIndentation(lines: string[], indent = 2) {
  24. const indentation = ' '.repeat(indent);
  25. return lines.map(line => `${indentation}${line}`).join('\n');
  26. }
  27. export function getInstallSnippet({
  28. productSelection,
  29. additionalPackages = [],
  30. basePackage = '@sentry/node',
  31. }: {
  32. productSelection: ProductSelectionMap;
  33. additionalPackages?: string[];
  34. basePackage?: string;
  35. }) {
  36. let packages = [basePackage];
  37. if (productSelection.profiling) {
  38. packages.push('@sentry/profiling-node');
  39. }
  40. packages = packages.concat(additionalPackages);
  41. return `# Using yarn
  42. yarn add ${packages.join(' ')}
  43. # Using npm
  44. npm install --save ${packages.join(' ')}`;
  45. }
  46. export function getDefaultNodeImports({
  47. productSelection,
  48. }: {
  49. productSelection: ProductSelectionMap;
  50. }) {
  51. const imports: string[] = [
  52. `// You can also use CommonJS \`require('@sentry/node')\` instead of \`import\``,
  53. `import * as Sentry from "@sentry/node";`,
  54. ];
  55. if (productSelection.profiling) {
  56. imports.push(`import { ProfilingIntegration } from "@sentry/profiling-node";`);
  57. }
  58. return imports;
  59. }
  60. export function getDefaulServerlessImports({
  61. productSelection,
  62. }: {
  63. productSelection: ProductSelectionMap;
  64. }) {
  65. const imports: string[] = [
  66. `// You can also use ESM \`import * as Sentry from "@sentry/serverless"\` instead of \`require\``,
  67. `const Sentry = require("@sentry/serverless");`,
  68. ];
  69. if (productSelection.profiling) {
  70. imports.push(`const { ProfilingIntegration } = require("@sentry/profiling");`);
  71. }
  72. return imports;
  73. }
  74. export function getProductIntegrations({
  75. productSelection,
  76. }: {
  77. productSelection: ProductSelectionMap;
  78. }) {
  79. const integrations: string[] = [];
  80. if (productSelection.profiling) {
  81. integrations.push(`new ProfilingIntegration(),`);
  82. }
  83. return integrations;
  84. }
  85. export function getDefaultInitParams({dsn}: {dsn: string}) {
  86. return [`dsn: '${dsn}',`];
  87. }
  88. export function getProductInitParams({
  89. productSelection,
  90. }: {
  91. productSelection: ProductSelectionMap;
  92. }) {
  93. const params: string[] = [];
  94. if (productSelection['performance-monitoring']) {
  95. params.push(`// Performance Monitoring`);
  96. params.push(
  97. `tracesSampleRate: 1.0, // Capture 100% of the transactions, reduce in production!`
  98. );
  99. }
  100. if (productSelection.profiling) {
  101. params.push(
  102. `// Set sampling rate for profiling - this is relative to tracesSampleRate`
  103. );
  104. params.push(
  105. `profilesSampleRate: 1.0, // Capture 100% of the transactions, reduce in production!`
  106. );
  107. }
  108. return params;
  109. }