extensions.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import { addTracingExtensions, getMainCarrier } from '@sentry/core';
  2. import { isNodeEnv, loadModule, dynamicRequire } from '@sentry/utils';
  3. /**
  4. * @private
  5. */
  6. function _autoloadDatabaseIntegrations() {
  7. const carrier = getMainCarrier();
  8. if (!carrier.__SENTRY__) {
  9. return;
  10. }
  11. const packageToIntegrationMapping = {
  12. mongodb() {
  13. const integration = dynamicRequire(module, './node/integrations/mongo')
  14. ;
  15. return new integration.Mongo();
  16. },
  17. mongoose() {
  18. const integration = dynamicRequire(module, './node/integrations/mongo')
  19. ;
  20. return new integration.Mongo();
  21. },
  22. mysql() {
  23. const integration = dynamicRequire(module, './node/integrations/mysql')
  24. ;
  25. return new integration.Mysql();
  26. },
  27. pg() {
  28. const integration = dynamicRequire(module, './node/integrations/postgres')
  29. ;
  30. return new integration.Postgres();
  31. },
  32. };
  33. const mappedPackages = Object.keys(packageToIntegrationMapping)
  34. .filter(moduleName => !!loadModule(moduleName))
  35. .map(pkg => {
  36. try {
  37. return packageToIntegrationMapping[pkg]();
  38. } catch (e) {
  39. return undefined;
  40. }
  41. })
  42. .filter(p => p) ;
  43. if (mappedPackages.length > 0) {
  44. carrier.__SENTRY__.integrations = [...(carrier.__SENTRY__.integrations || []), ...mappedPackages];
  45. }
  46. }
  47. /**
  48. * This patches the global object and injects the Tracing extensions methods
  49. */
  50. function addExtensionMethods() {
  51. addTracingExtensions();
  52. // Detect and automatically load specified integrations.
  53. if (isNodeEnv()) {
  54. _autoloadDatabaseIntegrations();
  55. }
  56. }
  57. export { addExtensionMethods };
  58. //# sourceMappingURL=extensions.js.map