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
@@ -12,36 +11,33 @@ const finishCh = channel('apm:cassandra-driver:query:finish')
12
11
const errorCh = channel ( 'apm:cassandra-driver:query:error' )
13
12
const connectCh = channel ( 'apm:cassandra-driver:query:connect' )
14
13
14
+ let startCtx = { }
15
+
15
16
addHook ( { name : 'cassandra-driver' , versions : [ '>=3.0.0' ] } , cassandra => {
16
17
shimmer . wrap ( cassandra . Client . prototype , 'batch' , batch => function ( queries , options , callback ) {
17
18
if ( ! startCh . hasSubscribers ) {
18
19
return batch . apply ( this , arguments )
19
20
}
20
- const callbackResource = new AsyncResource ( 'bound-anonymous-fn' )
21
- const asyncResource = new AsyncResource ( 'bound-anonymous-fn' )
22
21
const lastIndex = arguments . length - 1
23
- let cb = arguments [ lastIndex ]
22
+ const cb = arguments [ lastIndex ]
24
23
25
- if ( typeof cb === 'function' ) {
26
- cb = callbackResource . bind ( cb )
27
- arguments [ lastIndex ] = wrapCallback ( finishCh , errorCh , asyncResource , cb )
28
- }
24
+ startCtx = { keyspace : this . keyspace , query : queries , contactPoints : this . options && this . options . contactPoints }
25
+ return startCh . runStores ( startCtx , ( ) => {
26
+ if ( typeof cb === 'function' ) {
27
+ arguments [ lastIndex ] = wrapCallback ( finishCh , errorCh , startCtx , cb )
28
+ }
29
29
30
- return asyncResource . runInAsyncScope ( ( ) => {
31
- const contactPoints = this . options && this . options . contactPoints
32
- startCh . publish ( { keyspace : this . keyspace , query : queries , contactPoints } )
33
30
try {
34
31
const res = batch . apply ( this , arguments )
35
32
if ( typeof res === 'function' || ! res ) {
36
- return wrapCallback ( finishCh , errorCh , asyncResource , res )
33
+ return wrapCallback ( finishCh , errorCh , startCtx , res )
37
34
}
38
- const promiseAsyncResource = new AsyncResource ( 'bound-anonymous-fn' )
39
35
return res . then (
40
- promiseAsyncResource . bind ( ( ) => finish ( finishCh , errorCh ) ) ,
41
- promiseAsyncResource . bind ( err => finish ( finishCh , errorCh , err ) )
36
+ ( ) => finish ( finishCh , errorCh , startCtx ) ,
37
+ err => finish ( finishCh , errorCh , startCtx , err )
42
38
)
43
39
} catch ( e ) {
44
- finish ( finishCh , errorCh , e )
40
+ finish ( finishCh , errorCh , startCtx , e )
45
41
throw e
46
42
}
47
43
} )
@@ -54,17 +50,13 @@ addHook({ name: 'cassandra-driver', versions: ['>=4.4'] }, cassandra => {
54
50
if ( ! startCh . hasSubscribers ) {
55
51
return _execute . apply ( this , arguments )
56
52
}
57
- const asyncResource = new AsyncResource ( 'bound-anonymous-fn' )
58
- return asyncResource . runInAsyncScope ( ( ) => {
59
- const contactPoints = this . options && this . options . contactPoints
60
- startCh . publish ( { keyspace : this . keyspace , query, contactPoints } )
53
+ startCtx = { keyspace : this . keyspace , query, contactPoints : this . options && this . options . contactPoints }
54
+ return startCh . runStores ( startCtx , ( ) => {
61
55
const promise = _execute . apply ( this , arguments )
62
56
63
- const promiseAsyncResource = new AsyncResource ( 'bound-anonymous-fn' )
64
-
65
57
promise . then (
66
- promiseAsyncResource . bind ( ( ) => finish ( finishCh , errorCh ) ) ,
67
- promiseAsyncResource . bind ( err => finish ( finishCh , errorCh , err ) )
58
+ ( ) => finish ( finishCh , errorCh , startCtx ) ,
59
+ err => finish ( finishCh , errorCh , startCtx , err )
68
60
)
69
61
return promise
70
62
} )
@@ -82,29 +74,23 @@ addHook({ name: 'cassandra-driver', versions: ['3 - 4.3'] }, cassandra => {
82
74
if ( ! startCh . hasSubscribers ) {
83
75
return _innerExecute . apply ( this , arguments )
84
76
}
85
- const callbackResource = new AsyncResource ( 'bound-anonymous-fn' )
86
- const asyncResource = new AsyncResource ( 'bound-anonymous-fn' )
87
-
88
77
if ( ! isValid ( arguments ) ) {
89
78
return _innerExecute . apply ( this , arguments )
90
79
}
91
80
92
- return asyncResource . runInAsyncScope ( ( ) => {
93
- const contactPoints = this . options && this . options . contactPoints
94
- startCh . publish ( { keyspace : this . keyspace , query, contactPoints } )
95
-
81
+ startCtx = { keyspace : this . keyspace , query, contactPoints : this . options && this . options . contactPoints }
82
+ return startCh . runStores ( startCtx , ( ) => {
96
83
const lastIndex = arguments . length - 1
97
- let cb = arguments [ lastIndex ]
84
+ const cb = arguments [ lastIndex ]
98
85
99
86
if ( typeof cb === 'function' ) {
100
- cb = callbackResource . bind ( cb )
101
- arguments [ lastIndex ] = wrapCallback ( finishCh , errorCh , asyncResource , cb )
87
+ arguments [ lastIndex ] = wrapCallback ( finishCh , errorCh , startCtx , cb )
102
88
}
103
89
104
90
try {
105
91
return _innerExecute . apply ( this , arguments )
106
92
} catch ( e ) {
107
- finish ( finishCh , errorCh , e )
93
+ finish ( finishCh , errorCh , startCtx , e )
108
94
throw e
109
95
}
110
96
} )
@@ -118,7 +104,8 @@ addHook({ name: 'cassandra-driver', versions: ['>=3.3'], file: 'lib/request-exec
118
104
if ( ! startCh . hasSubscribers ) {
119
105
return _sendOnConnection . apply ( this , arguments )
120
106
}
121
- connectCh . publish ( { hostname : this . _connection . address , port : this . _connection . port } )
107
+ startCtx = { hostname : this . _connection . address , port : this . _connection . port , ...startCtx }
108
+ connectCh . publish ( startCtx )
122
109
return _sendOnConnection . apply ( this , arguments )
123
110
} )
124
111
return RequestExecution
@@ -129,19 +116,16 @@ addHook({ name: 'cassandra-driver', versions: ['3.3 - 4.3'], file: 'lib/request-
129
116
if ( ! startCh . hasSubscribers ) {
130
117
return getHostCallback . apply ( this , arguments )
131
118
}
132
- const asyncResource = new AsyncResource ( 'bound-anonymous-fn' )
133
119
const execution = this
134
120
135
121
if ( ! isRequestValid ( this , arguments , 1 ) ) {
136
122
return start . apply ( this , arguments )
137
123
}
138
124
139
- getHostCallback = asyncResource . bind ( getHostCallback )
140
-
141
- arguments [ 0 ] = AsyncResource . bind ( function ( ) {
142
- connectCh . publish ( { hostname : execution . _connection . address , port : execution . _connection . port } )
143
- return getHostCallback . apply ( this , arguments )
144
- } )
125
+ arguments [ 0 ] = function ( ) {
126
+ startCtx = { hostname : execution . _connection . address , port : execution . _connection . port , ...startCtx }
127
+ return connectCh . runStores ( startCtx , getHostCallback , this , ...arguments )
128
+ }
145
129
146
130
return start . apply ( this , arguments )
147
131
} )
@@ -158,34 +142,33 @@ addHook({ name: 'cassandra-driver', versions: ['3 - 3.2'], file: 'lib/request-ha
158
142
if ( ! isRequestValid ( this , arguments , 3 ) ) {
159
143
return send . apply ( this , arguments )
160
144
}
161
- const asyncResource = new AsyncResource ( 'bound-anonymous-fn' )
162
-
163
- callback = asyncResource . bind ( callback )
164
145
165
- arguments [ 2 ] = AsyncResource . bind ( function ( ) {
166
- connectCh . publish ( { hostname : handler . connection . address , port : handler . connection . port } )
167
- return callback . apply ( this , arguments )
168
- } )
146
+ arguments [ 2 ] = function ( ) {
147
+ startCtx = { hostname : handler . connection . address , port : handler . connection . port , ... startCtx }
148
+ return connectCh . runStores ( startCtx , callback , this , ... arguments )
149
+ }
169
150
170
151
return send . apply ( this , arguments )
171
152
} )
172
153
return RequestHandler
173
154
} )
174
155
175
- function finish ( finishCh , errorCh , error ) {
156
+ function finish ( finishCh , errorCh , ctx , error ) {
176
157
if ( error ) {
177
- errorCh . publish ( error )
158
+ ctx . error = error
159
+ errorCh . publish ( ctx )
178
160
}
179
- finishCh . publish ( )
161
+ finishCh . runStores ( ctx , ( ) => { } )
180
162
}
181
163
182
- function wrapCallback ( finishCh , errorCh , asyncResource , callback ) {
183
- return shimmer . wrapFunction ( callback , callback => asyncResource . bind ( function ( err ) {
184
- finish ( finishCh , errorCh , err )
185
- if ( callback ) {
186
- return callback . apply ( this , arguments )
164
+ function wrapCallback ( finishCh , errorCh , ctx , callback ) {
165
+ return shimmer . wrapFunction ( callback , callback => function ( err ) {
166
+ if ( err ) {
167
+ ctx . error = err
168
+ errorCh . publish ( ctx )
187
169
}
188
- } ) )
170
+ return finishCh . runStores ( ctx , callback , this , ...arguments )
171
+ } )
189
172
}
190
173
191
174
function isRequestValid ( exec , args , length ) {
0 commit comments