prisma.js 2.8 KB

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