@@ -2,7 +2,7 @@ import type { Instrumentation } from '@opentelemetry/instrumentation';
22// When importing CJS modules into an ESM module, we cannot import the named exports directly.
33import * as prismaInstrumentation from '@prisma/instrumentation' ;
44import { SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN , consoleSandbox , defineIntegration , spanToJSON } from '@sentry/core' ;
5- import { generateInstrumentOnce } from '../../otel/instrument' ;
5+ import { generateInstrumentOnce , instrumentWhenWrapped } from '../../otel/instrument' ;
66import type { PrismaV5TracingHelper } from './prisma/vendor/v5-tracing-helper' ;
77import type { PrismaV6TracingHelper } from './prisma/vendor/v6-tracing-helper' ;
88
@@ -113,29 +113,34 @@ export const prismaIntegration = defineIntegration(
113113 */
114114 prismaInstrumentation ?: Instrumentation ;
115115 } = { } ) => {
116+ let instrumentationWrappedCallback : undefined | ( ( callback : ( ) => void ) => void ) ;
117+
116118 return {
117119 name : INTEGRATION_NAME ,
118120 setupOnce ( ) {
119- instrumentPrisma ( { prismaInstrumentation } ) ;
121+ const instrumentation = instrumentPrisma ( { prismaInstrumentation } ) ;
122+ instrumentationWrappedCallback = instrumentWhenWrapped ( instrumentation ) ;
120123 } ,
121124 setup ( client ) {
122- client . on ( 'spanStart' , span => {
123- const spanJSON = spanToJSON ( span ) ;
124- if ( spanJSON . description ?. startsWith ( 'prisma:' ) ) {
125- span . setAttribute ( SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN , 'auto.db.otel.prisma' ) ;
126- }
125+ instrumentationWrappedCallback ?.( ( ) =>
126+ client . on ( 'spanStart' , span => {
127+ const spanJSON = spanToJSON ( span ) ;
128+ if ( spanJSON . description ?. startsWith ( 'prisma:' ) ) {
129+ span . setAttribute ( SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN , 'auto.db.otel.prisma' ) ;
130+ }
127131
128- // Make sure we use the query text as the span name, for ex. SELECT * FROM "User" WHERE "id" = $1
129- if ( spanJSON . description === 'prisma:engine:db_query' && spanJSON . data [ 'db.query.text' ] ) {
130- span . updateName ( spanJSON . data [ 'db.query.text' ] as string ) ;
131- }
132+ // Make sure we use the query text as the span name, for ex. SELECT * FROM "User" WHERE "id" = $1
133+ if ( spanJSON . description === 'prisma:engine:db_query' && spanJSON . data [ 'db.query.text' ] ) {
134+ span . updateName ( spanJSON . data [ 'db.query.text' ] as string ) ;
135+ }
132136
133- // In Prisma v5.22+, the `db.system` attribute is automatically set
134- // On older versions, this is missing, so we add it here
135- if ( spanJSON . description === 'prisma:engine:db_query' && ! spanJSON . data [ 'db.system' ] ) {
136- span . setAttribute ( 'db.system' , 'prisma' ) ;
137- }
138- } ) ;
137+ // In Prisma v5.22+, the `db.system` attribute is automatically set
138+ // On older versions, this is missing, so we add it here
139+ if ( spanJSON . description === 'prisma:engine:db_query' && ! spanJSON . data [ 'db.system' ] ) {
140+ span . setAttribute ( 'db.system' , 'prisma' ) ;
141+ }
142+ } ) ,
143+ ) ;
139144 } ,
140145 } ;
141146 } ,
0 commit comments