@@ -3,10 +3,11 @@ import {
33 SEMANTIC_ATTRIBUTE_SENTRY_OP ,
44 SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN ,
55 defineIntegration ,
6+ getClient ,
67 spanToJSON ,
78} from '@sentry/core' ;
89import type { IntegrationFn } from '@sentry/core' ;
9- import { generateInstrumentOnce } from '../../otel/instrument' ;
10+ import { callWhenWrapped , generateInstrumentOnce } from '../../otel/instrument' ;
1011
1112const INTEGRATION_NAME = 'Dataloader' ;
1213
@@ -19,31 +20,38 @@ export const instrumentDataloader = generateInstrumentOnce(
1920) ;
2021
2122const _dataloaderIntegration = ( ( ) => {
23+ let hookCallback : undefined | ( ( ) => void ) ;
24+
2225 return {
2326 name : INTEGRATION_NAME ,
2427 setupOnce ( ) {
25- instrumentDataloader ( ) ;
26- } ,
28+ const instrumentation = instrumentDataloader ( ) ;
2729
28- setup ( client ) {
29- client . on ( 'spanStart' , span => {
30- const spanJSON = spanToJSON ( span ) ;
31- if ( spanJSON . description ?. startsWith ( 'dataloader' ) ) {
32- span . setAttribute ( SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN , 'auto.db.otel.dataloader' ) ;
30+ callWhenWrapped ( instrumentation , ( ) => {
31+ const client = getClient ( ) ;
32+ if ( hookCallback || ! client ) {
33+ return ;
3334 }
3435
35- // These are all possible dataloader span descriptions
36- // Still checking for the future versions
37- // in case they add support for `clear` and `prime`
38- if (
39- spanJSON . description === 'dataloader.load' ||
40- spanJSON . description === 'dataloader.loadMany' ||
41- spanJSON . description === 'dataloader.batch'
42- ) {
43- span . setAttribute ( SEMANTIC_ATTRIBUTE_SENTRY_OP , 'cache.get' ) ;
44- // TODO: We can try adding `key` to the `data` attribute upstream.
45- // Or alternatively, we can add `requestHook` to the dataloader instrumentation.
46- }
36+ hookCallback = client . on ( 'spanStart' , span => {
37+ const spanJSON = spanToJSON ( span ) ;
38+ if ( spanJSON . description ?. startsWith ( 'dataloader' ) ) {
39+ span . setAttribute ( SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN , 'auto.db.otel.dataloader' ) ;
40+ }
41+
42+ // These are all possible dataloader span descriptions
43+ // Still checking for the future versions
44+ // in case they add support for `clear` and `prime`
45+ if (
46+ spanJSON . description === 'dataloader.load' ||
47+ spanJSON . description === 'dataloader.loadMany' ||
48+ spanJSON . description === 'dataloader.batch'
49+ ) {
50+ span . setAttribute ( SEMANTIC_ATTRIBUTE_SENTRY_OP , 'cache.get' ) ;
51+ // TODO: We can try adding `key` to the `data` attribute upstream.
52+ // Or alternatively, we can add `requestHook` to the dataloader instrumentation.
53+ }
54+ } ) ;
4755 } ) ;
4856 } ,
4957 } ;
0 commit comments