2
2
3
3
const {
4
4
channel,
5
- addHook,
6
- AsyncResource
5
+ addHook
7
6
} = require ( './helpers/instrument' )
8
7
const shimmer = require ( '../../datadog-shimmer' )
9
8
@@ -14,11 +13,11 @@ const startChannel = channel('apm:oracledb:query:start')
14
13
const errorChannel = channel ( 'apm:oracledb:query:error' )
15
14
const finishChannel = channel ( 'apm:oracledb:query:finish' )
16
15
17
- function finish ( err ) {
18
- if ( err ) {
19
- errorChannel . publish ( err )
16
+ function finish ( ctx ) {
17
+ if ( ctx . error ) {
18
+ errorChannel . publish ( ctx )
20
19
}
21
- finishChannel . publish ( )
20
+ finishChannel . publish ( ctx )
22
21
}
23
22
24
23
addHook ( { name : 'oracledb' , versions : [ '>=5' ] } , oracledb => {
@@ -30,57 +29,64 @@ addHook({ name: 'oracledb', versions: ['>=5'] }, oracledb => {
30
29
31
30
if ( arguments . length && typeof arguments [ arguments . length - 1 ] === 'function' ) {
32
31
const cb = arguments [ arguments . length - 1 ]
33
- const outerAr = new AsyncResource ( 'apm:oracledb:outer-scope' )
34
32
arguments [ arguments . length - 1 ] = shimmer . wrapFunction ( cb , cb => function wrappedCb ( err , result ) {
35
- finish ( err )
36
- return outerAr . runInAsyncScope ( ( ) => cb . apply ( this , arguments ) )
33
+ if ( err ) {
34
+ ctx . error = err
35
+ errorChannel . publish ( ctx )
36
+ }
37
+ return finishChannel . runStores ( ctx , ( ) => {
38
+ return cb . apply ( this , arguments )
39
+ } )
37
40
} )
38
41
}
39
42
40
- return new AsyncResource ( 'apm:oracledb:inner-scope' ) . runInAsyncScope ( ( ) => {
41
- // The connAttrs are used to pass through the argument to the potential
42
- // serviceName method a user might have passed through as well as parsing
43
- // the connection string in v5.
44
- const connAttrs = connectionAttributes . get ( this )
43
+ // The connAttrs are used to pass through the argument to the potential
44
+ // serviceName method a user might have passed through as well as parsing
45
+ // the connection string in v5.
46
+ const connAttrs = connectionAttributes . get ( this )
45
47
46
- const details = typeof this . hostName === 'string' ? this : this . _impl
48
+ const details = typeof this . hostName === 'string' ? this : this . _impl
47
49
48
- let hostname
49
- let port
50
- let dbInstance
50
+ let hostname
51
+ let port
52
+ let dbInstance
51
53
52
- if ( details ) {
53
- dbInstance = details . serviceName
54
- hostname = details . hostName ?? details . nscon ?. ntAdapter ?. hostName
55
- port = String ( details . port ?? details . nscon ?. ntAdapter ?. port ?? '' )
56
- }
54
+ if ( details ) {
55
+ dbInstance = details . serviceName
56
+ hostname = details . hostName ?? details . nscon ?. ntAdapter ?. hostName
57
+ port = String ( details . port ?? details . nscon ?. ntAdapter ?. port ?? '' )
58
+ }
57
59
58
- startChannel . publish ( {
59
- query : dbQuery ,
60
- connAttrs,
61
- dbInstance,
62
- port,
63
- hostname,
64
- } )
60
+ const ctx = {
61
+ dbInstance,
62
+ port,
63
+ hostname,
64
+ query : dbQuery ,
65
+ connAttrs
66
+ }
67
+
68
+ return startChannel . runStores ( ctx , ( ) => {
65
69
try {
66
70
let result = execute . apply ( this , arguments )
67
71
68
72
if ( typeof result ?. then === 'function' ) {
69
73
result = result . then (
70
74
x => {
71
- finish ( )
75
+ finish ( ctx )
72
76
return x
73
77
} ,
74
78
e => {
75
- finish ( e )
79
+ ctx . error = e
80
+ finish ( ctx )
76
81
throw e
77
82
}
78
83
)
79
84
}
80
85
81
86
return result
82
87
} catch ( err ) {
83
- errorChannel . publish ( err )
88
+ ctx . error = err
89
+ finish ( ctx )
84
90
throw err
85
91
}
86
92
} )
0 commit comments