@@ -25,6 +25,7 @@ const collectionName = command => command.ns.split('.')[1];
25
25
const generateConnectionId = pool =>
26
26
pool . options ? `${ pool . options . host } :${ pool . options . port } ` : pool . address ;
27
27
const maybeRedact = ( commandName , result ) => ( SENSITIVE_COMMANDS . has ( commandName ) ? { } : result ) ;
28
+ const isLegacyPool = pool => pool . s && pool . queue ;
28
29
29
30
const LEGACY_FIND_QUERY_MAP = {
30
31
$query : 'filter' ,
@@ -151,6 +152,22 @@ const extractReply = (command, reply) => {
151
152
return reply && reply . result ? reply . result : reply ;
152
153
} ;
153
154
155
+ const extractConnectionDetails = pool => {
156
+ if ( isLegacyPool ( pool ) ) {
157
+ return {
158
+ connectionId : generateConnectionId ( pool )
159
+ } ;
160
+ }
161
+
162
+ // APM in the modern pool is done at the `Connection` level, so we rename it here for
163
+ // readability.
164
+ const connection = pool ;
165
+ return {
166
+ address : connection . address ,
167
+ connectionId : connection . id
168
+ } ;
169
+ } ;
170
+
154
171
/** An event indicating the start of a given command */
155
172
class CommandStartedEvent {
156
173
/**
@@ -162,15 +179,15 @@ class CommandStartedEvent {
162
179
constructor ( pool , command ) {
163
180
const cmd = extractCommand ( command ) ;
164
181
const commandName = extractCommandName ( cmd ) ;
182
+ const connectionDetails = extractConnectionDetails ( pool ) ;
165
183
166
184
// NOTE: remove in major revision, this is not spec behavior
167
185
if ( SENSITIVE_COMMANDS . has ( commandName ) ) {
168
186
this . commandObj = { } ;
169
187
this . commandObj [ commandName ] = true ;
170
188
}
171
189
172
- Object . assign ( this , {
173
- connectionId : generateConnectionId ( pool ) ,
190
+ Object . assign ( this , connectionDetails , {
174
191
requestId : command . requestId ,
175
192
databaseName : databaseName ( command ) ,
176
193
commandName,
@@ -192,9 +209,9 @@ class CommandSucceededEvent {
192
209
constructor ( pool , command , reply , started ) {
193
210
const cmd = extractCommand ( command ) ;
194
211
const commandName = extractCommandName ( cmd ) ;
212
+ const connectionDetails = extractConnectionDetails ( pool ) ;
195
213
196
- Object . assign ( this , {
197
- connectionId : generateConnectionId ( pool ) ,
214
+ Object . assign ( this , connectionDetails , {
198
215
requestId : command . requestId ,
199
216
commandName,
200
217
duration : calculateDurationInMs ( started ) ,
@@ -216,9 +233,9 @@ class CommandFailedEvent {
216
233
constructor ( pool , command , error , started ) {
217
234
const cmd = extractCommand ( command ) ;
218
235
const commandName = extractCommandName ( cmd ) ;
236
+ const connectionDetails = extractConnectionDetails ( pool ) ;
219
237
220
- Object . assign ( this , {
221
- connectionId : generateConnectionId ( pool ) ,
238
+ Object . assign ( this , connectionDetails , {
222
239
requestId : command . requestId ,
223
240
commandName,
224
241
duration : calculateDurationInMs ( started ) ,
0 commit comments