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
@@ -22,15 +21,11 @@ function wrapAddCommand (addCommand) {
22
21
const name = command [ 0 ]
23
22
const args = command . slice ( 1 )
24
23
25
- const asyncResource = new AsyncResource ( 'bound-anonymous-fn' )
26
- return asyncResource . runInAsyncScope ( ( ) => {
27
- start ( this , name , args , this . _url )
28
-
24
+ const ctx = getStartCtx ( this , name , args , this . _url )
25
+ return startCh . runStores ( ctx , ( ) => {
29
26
const res = addCommand . apply ( this , arguments )
30
- const onResolve = asyncResource . bind ( ( ) => finish ( finishCh , errorCh ) )
31
- const onReject = asyncResource . bind ( err => finish ( finishCh , errorCh , err ) )
32
27
33
- res . then ( onResolve , onReject )
28
+ res . then ( ( ) => finish ( finishCh , errorCh , ctx ) , err => finish ( finishCh , errorCh , ctx , err ) )
34
29
35
30
return res
36
31
} )
@@ -94,14 +89,9 @@ addHook({ name: 'redis', versions: ['>=2.6 <4'] }, redis => {
94
89
95
90
if ( ! options . callback ) return internalSendCommand . apply ( this , arguments )
96
91
97
- const callbackResource = new AsyncResource ( 'bound-anonymous-fn' )
98
- const asyncResource = new AsyncResource ( 'bound-anonymous-fn' )
99
- const cb = callbackResource . bind ( options . callback )
100
-
101
- return asyncResource . runInAsyncScope ( ( ) => {
102
- start ( this , options . command , options . args )
103
-
104
- options . callback = asyncResource . bind ( wrapCallback ( finishCh , errorCh , cb ) )
92
+ const ctx = getStartCtx ( this , options . command , options . args )
93
+ return startCh . runStores ( ctx , ( ) => {
94
+ options . callback = wrapCallback ( finishCh , errorCh , ctx , options . callback )
105
95
106
96
try {
107
97
return internalSendCommand . apply ( this , arguments )
@@ -121,26 +111,21 @@ addHook({ name: 'redis', versions: ['>=0.12 <2.6'] }, redis => {
121
111
return sendCommand . apply ( this , arguments )
122
112
}
123
113
124
- const callbackResource = new AsyncResource ( 'bound-anonymous-fn' )
125
- const asyncResource = new AsyncResource ( 'bound-anonymous-fn' )
126
-
127
- return asyncResource . runInAsyncScope ( ( ) => {
128
- start ( this , command , args )
129
-
114
+ const ctx = getStartCtx ( this , command , args )
115
+ return startCh . runStores ( ctx , ( ) => {
130
116
if ( typeof callback === 'function' ) {
131
- const cb = callbackResource . bind ( callback )
132
- arguments [ 2 ] = asyncResource . bind ( wrapCallback ( finishCh , errorCh , cb ) )
117
+ arguments [ 2 ] = wrapCallback ( finishCh , errorCh , ctx , callback )
133
118
} else if ( Array . isArray ( args ) && typeof args . at ( - 1 ) === 'function' ) {
134
- const cb = callbackResource . bind ( args . at ( - 1 ) )
135
- args [ args . length - 1 ] = asyncResource . bind ( wrapCallback ( finishCh , errorCh , cb ) )
119
+ args [ args . length - 1 ] = wrapCallback ( finishCh , errorCh , ctx , args . at ( - 1 ) )
136
120
} else {
137
- arguments [ 2 ] = asyncResource . bind ( wrapCallback ( finishCh , errorCh ) )
121
+ arguments [ 2 ] = wrapCallback ( finishCh , errorCh , ctx )
138
122
}
139
123
140
124
try {
141
125
return sendCommand . apply ( this , arguments )
142
126
} catch ( err ) {
143
- errorCh . publish ( err )
127
+ ctx . error = err
128
+ errorCh . publish ( ctx )
144
129
145
130
throw err
146
131
}
@@ -149,24 +134,28 @@ addHook({ name: 'redis', versions: ['>=0.12 <2.6'] }, redis => {
149
134
return redis
150
135
} )
151
136
152
- function start ( client , command , args , url = { } ) {
153
- const db = client . selected_db
154
- const connectionOptions = client . connection_options || client . connection_option || client . connectionOption || url
155
- startCh . publish ( { db, command, args, connectionOptions } )
137
+ function getStartCtx ( client , command , args , url = { } ) {
138
+ return {
139
+ db : client . selected_db ,
140
+ command,
141
+ args,
142
+ connectionOptions : client . connection_options || client . connection_option || client . connectionOption || url
143
+ }
156
144
}
157
145
158
- function wrapCallback ( finishCh , errorCh , callback ) {
146
+ function wrapCallback ( finishCh , errorCh , ctx , callback ) {
159
147
return shimmer . wrapFunction ( callback , callback => function ( err ) {
160
- finish ( finishCh , errorCh , err )
161
- if ( callback ) {
162
- return callback . apply ( this , arguments )
163
- }
148
+ return finish ( finishCh , errorCh , ctx , err , callback , this , arguments )
164
149
} )
165
150
}
166
151
167
- function finish ( finishCh , errorCh , error ) {
152
+ function finish ( finishCh , errorCh , ctx , error , callback , thisArg , args ) {
168
153
if ( error ) {
169
- errorCh . publish ( error )
154
+ ctx . error = error
155
+ errorCh . publish ( ctx )
156
+ }
157
+ if ( callback ) {
158
+ return finishCh . runStores ( ctx , callback , thisArg , ...args )
170
159
}
171
- finishCh . publish ( )
160
+ finishCh . publish ( ctx )
172
161
}
0 commit comments