@@ -126,12 +126,25 @@ export class ConnectionCreatedEvent extends ConnectionPoolMonitoringEvent {
126
126
export class ConnectionReadyEvent extends ConnectionPoolMonitoringEvent {
127
127
/** The id of the connection */
128
128
connectionId : number | '<monitor>' ;
129
+ /**
130
+ * The time it took to establish the connection.
131
+ * In accordance with the definition of establishment of a connection
132
+ * specified by `ConnectionPoolOptions.maxConnecting`,
133
+ * it is the time elapsed between emitting a `ConnectionCreatedEvent`
134
+ * and emitting this event as part of the same checking out.
135
+ *
136
+ * Naturally, when establishing a connection is part of checking out,
137
+ * this duration is not greater than
138
+ * `ConnectionCheckedOutEvent.duration`.
139
+ */
140
+ durationMS : number ;
129
141
/** @internal */
130
142
name = CONNECTION_READY ;
131
143
132
144
/** @internal */
133
- constructor ( pool : ConnectionPool , connection : Connection ) {
145
+ constructor ( pool : ConnectionPool , connection : Connection , connectionCreatedEventTime : number ) {
134
146
super ( pool ) ;
147
+ this . durationMS = Date . now ( ) - connectionCreatedEventTime ;
135
148
this . connectionId = connection . id ;
136
149
}
137
150
}
@@ -194,6 +207,20 @@ export class ConnectionCheckOutFailedEvent extends ConnectionPoolMonitoringEvent
194
207
error ?: MongoError ;
195
208
/** @internal */
196
209
name = CONNECTION_CHECK_OUT_FAILED ;
210
+ /**
211
+ * The time it took to check out the connection.
212
+ * More specifically, the time elapsed between
213
+ * emitting a `ConnectionCheckOutStartedEvent`
214
+ * and emitting this event as part of the same checking out.
215
+ *
216
+ * Naturally, if a new connection was not created (`ConnectionCreatedEvent`)
217
+ * and established (`ConnectionReadyEvent`) as part of checking out,
218
+ * this duration is usually
219
+ * not greater than `ConnectionPoolOptions.waitQueueTimeoutMS`,
220
+ * but MAY occasionally be greater than that,
221
+ * because a driver does not provide hard real-time guarantees.
222
+ */
223
+ durationMS : number ;
197
224
198
225
/** @internal */
199
226
constructor (
@@ -202,6 +229,7 @@ export class ConnectionCheckOutFailedEvent extends ConnectionPoolMonitoringEvent
202
229
error ?: MongoError
203
230
) {
204
231
super ( pool ) ;
232
+ this . durationMS = this . durationMS = Date . now ( ) - ( connection . connectionCreatedEventTime ?? Date . now ( ) ) ;
205
233
this . reason = reason ;
206
234
this . error = error ;
207
235
}
@@ -217,10 +245,25 @@ export class ConnectionCheckedOutEvent extends ConnectionPoolMonitoringEvent {
217
245
connectionId : number | '<monitor>' ;
218
246
/** @internal */
219
247
name = CONNECTION_CHECKED_OUT ;
248
+ /**
249
+ * The time it took to check out the connection.
250
+ * More specifically, the time elapsed between
251
+ * emitting a `ConnectionCheckOutStartedEvent`
252
+ * and emitting this event as part of the same checking out.
253
+ *
254
+ * Naturally, if a new connection was not created (`ConnectionCreatedEvent`)
255
+ * and established (`ConnectionReadyEvent`) as part of checking out,
256
+ * this duration is usually
257
+ * not greater than `ConnectionPoolOptions.waitQueueTimeoutMS`,
258
+ * but MAY occasionally be greater than that,
259
+ * because a driver does not provide hard real-time guarantees.
260
+ */
261
+ durationMS : number ;
220
262
221
263
/** @internal */
222
264
constructor ( pool : ConnectionPool , connection : Connection ) {
223
265
super ( pool ) ;
266
+ this . durationMS = 0 ;
224
267
this . connectionId = connection . id ;
225
268
}
226
269
}
0 commit comments