@@ -3,10 +3,11 @@ import {
3
3
SEMANTIC_ATTRIBUTE_SENTRY_OP ,
4
4
SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN ,
5
5
defineIntegration ,
6
+ getClient ,
6
7
spanToJSON ,
7
8
} from '@sentry/core' ;
8
9
import type { IntegrationFn } from '@sentry/core' ;
9
- import { generateInstrumentOnce } from '../../otel/instrument' ;
10
+ import { callWhenWrapped , generateInstrumentOnce } from '../../otel/instrument' ;
10
11
11
12
const INTEGRATION_NAME = 'Dataloader' ;
12
13
@@ -19,31 +20,38 @@ export const instrumentDataloader = generateInstrumentOnce(
19
20
) ;
20
21
21
22
const _dataloaderIntegration = ( ( ) => {
23
+ let hookCallback : undefined | ( ( ) => void ) ;
24
+
22
25
return {
23
26
name : INTEGRATION_NAME ,
24
27
setupOnce ( ) {
25
- instrumentDataloader ( ) ;
26
- } ,
28
+ const instrumentation = instrumentDataloader ( ) ;
27
29
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 ;
33
34
}
34
35
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
+ } ) ;
47
55
} ) ;
48
56
} ,
49
57
} ;
0 commit comments