prisma.js 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. Object.defineProperty(exports, '__esModule', { value: true });
  2. const core = require('@sentry/core');
  3. const utils = require('@sentry/utils');
  4. const debugBuild = require('../../common/debug-build.js');
  5. const nodeUtils = require('./utils/node-utils.js');
  6. function isValidPrismaClient(possibleClient) {
  7. return !!possibleClient && !!(possibleClient )['$use'];
  8. }
  9. /** Tracing integration for @prisma/client package */
  10. class Prisma {
  11. /**
  12. * @inheritDoc
  13. */
  14. static __initStatic() {this.id = 'Prisma';}
  15. /**
  16. * @inheritDoc
  17. */
  18. /**
  19. * @inheritDoc
  20. */
  21. constructor(options = {}) {
  22. this.name = Prisma.id;
  23. // We instrument the PrismaClient inside the constructor and not inside `setupOnce` because in some cases of server-side
  24. // bundling (Next.js) multiple Prisma clients can be instantiated, even though users don't intend to. When instrumenting
  25. // in setupOnce we can only ever instrument one client.
  26. // https://github.com/getsentry/sentry-javascript/issues/7216#issuecomment-1602375012
  27. // In the future we might explore providing a dedicated PrismaClient middleware instead of this hack.
  28. if (isValidPrismaClient(options.client) && !options.client._sentryInstrumented) {
  29. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  30. utils.addNonEnumerableProperty(options.client , '_sentryInstrumented', true);
  31. const clientData = {};
  32. try {
  33. const engineConfig = (options.client )._engineConfig;
  34. if (engineConfig) {
  35. const { activeProvider, clientVersion } = engineConfig;
  36. if (activeProvider) {
  37. clientData['db.system'] = activeProvider;
  38. }
  39. if (clientVersion) {
  40. clientData['db.prisma.version'] = clientVersion;
  41. }
  42. }
  43. } catch (e) {
  44. // ignore
  45. }
  46. options.client.$use((params, next) => {
  47. // eslint-disable-next-line deprecation/deprecation
  48. if (nodeUtils.shouldDisableAutoInstrumentation(core.getCurrentHub)) {
  49. return next(params);
  50. }
  51. const action = params.action;
  52. const model = params.model;
  53. return core.startSpan(
  54. {
  55. name: model ? `${model} ${action}` : action,
  56. onlyIfParent: true,
  57. op: 'db.prisma',
  58. attributes: {
  59. [core.SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.db.prisma',
  60. },
  61. data: { ...clientData, 'db.operation': action },
  62. },
  63. () => next(params),
  64. );
  65. });
  66. } else {
  67. debugBuild.DEBUG_BUILD &&
  68. utils.logger.warn('Unsupported Prisma client provided to PrismaIntegration. Provided client:', options.client);
  69. }
  70. }
  71. /**
  72. * @inheritDoc
  73. */
  74. setupOnce() {
  75. // Noop - here for backwards compatibility
  76. }
  77. } Prisma.__initStatic();
  78. exports.Prisma = Prisma;
  79. //# sourceMappingURL=prisma.js.map