@@ -25,6 +25,7 @@ const collectionName = command => command.ns.split('.')[1];
2525const generateConnectionId = pool =>
2626 pool . options ? `${ pool . options . host } :${ pool . options . port } ` : pool . address ;
2727const maybeRedact = ( commandName , result ) => ( SENSITIVE_COMMANDS . has ( commandName ) ? { } : result ) ;
28+ const isLegacyPool = pool => pool . s && pool . queue ;
2829
2930const LEGACY_FIND_QUERY_MAP = {
3031 $query : 'filter' ,
@@ -151,6 +152,22 @@ const extractReply = (command, reply) => {
151152 return reply && reply . result ? reply . result : reply ;
152153} ;
153154
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+
154171/** An event indicating the start of a given command */
155172class CommandStartedEvent {
156173 /**
@@ -162,15 +179,15 @@ class CommandStartedEvent {
162179 constructor ( pool , command ) {
163180 const cmd = extractCommand ( command ) ;
164181 const commandName = extractCommandName ( cmd ) ;
182+ const connectionDetails = extractConnectionDetails ( pool ) ;
165183
166184 // NOTE: remove in major revision, this is not spec behavior
167185 if ( SENSITIVE_COMMANDS . has ( commandName ) ) {
168186 this . commandObj = { } ;
169187 this . commandObj [ commandName ] = true ;
170188 }
171189
172- Object . assign ( this , {
173- connectionId : generateConnectionId ( pool ) ,
190+ Object . assign ( this , connectionDetails , {
174191 requestId : command . requestId ,
175192 databaseName : databaseName ( command ) ,
176193 commandName,
@@ -192,9 +209,9 @@ class CommandSucceededEvent {
192209 constructor ( pool , command , reply , started ) {
193210 const cmd = extractCommand ( command ) ;
194211 const commandName = extractCommandName ( cmd ) ;
212+ const connectionDetails = extractConnectionDetails ( pool ) ;
195213
196- Object . assign ( this , {
197- connectionId : generateConnectionId ( pool ) ,
214+ Object . assign ( this , connectionDetails , {
198215 requestId : command . requestId ,
199216 commandName,
200217 duration : calculateDurationInMs ( started ) ,
@@ -216,9 +233,9 @@ class CommandFailedEvent {
216233 constructor ( pool , command , error , started ) {
217234 const cmd = extractCommand ( command ) ;
218235 const commandName = extractCommandName ( cmd ) ;
236+ const connectionDetails = extractConnectionDetails ( pool ) ;
219237
220- Object . assign ( this , {
221- connectionId : generateConnectionId ( pool ) ,
238+ Object . assign ( this , connectionDetails , {
222239 requestId : command . requestId ,
223240 commandName,
224241 duration : calculateDurationInMs ( started ) ,
0 commit comments