1
1
import { GetMore , KillCursor , Msg , WriteProtocolMessageType } from './commands' ;
2
2
import { calculateDurationInMs , deepCopy } from '../utils' ;
3
- import type { ConnectionPool } from './connection_pool' ;
4
3
import type { Connection } from './connection' ;
5
- import type { Document } from '../bson' ;
4
+ import type { Document , ObjectId } from '../bson' ;
6
5
7
6
/**
8
7
* An event indicating the start of a given
@@ -17,6 +16,7 @@ export class CommandStartedEvent {
17
16
command : Document ;
18
17
address : string ;
19
18
connectionId ?: string | number ;
19
+ serviceId ?: ObjectId ;
20
20
21
21
/**
22
22
* Create a started event
@@ -25,10 +25,10 @@ export class CommandStartedEvent {
25
25
* @param pool - the pool that originated the command
26
26
* @param command - the command
27
27
*/
28
- constructor ( pool : Connection | ConnectionPool , command : WriteProtocolMessageType ) {
28
+ constructor ( connection : Connection , command : WriteProtocolMessageType ) {
29
29
const cmd = extractCommand ( command ) ;
30
30
const commandName = extractCommandName ( cmd ) ;
31
- const { address, connectionId } = extractConnectionDetails ( pool ) ;
31
+ const { address, connectionId, serviceId } = extractConnectionDetails ( connection ) ;
32
32
33
33
// TODO: remove in major revision, this is not spec behavior
34
34
if ( SENSITIVE_COMMANDS . has ( commandName ) ) {
@@ -38,11 +38,17 @@ export class CommandStartedEvent {
38
38
39
39
this . address = address ;
40
40
this . connectionId = connectionId ;
41
+ this . serviceId = serviceId ;
41
42
this . requestId = command . requestId ;
42
43
this . databaseName = databaseName ( command ) ;
43
44
this . commandName = commandName ;
44
45
this . command = maybeRedact ( commandName , cmd , cmd ) ;
45
46
}
47
+
48
+ /* @internal */
49
+ get hasServiceId ( ) : boolean {
50
+ return ! ! this . serviceId ;
51
+ }
46
52
}
47
53
48
54
/**
@@ -57,6 +63,7 @@ export class CommandSucceededEvent {
57
63
duration : number ;
58
64
commandName : string ;
59
65
reply : unknown ;
66
+ serviceId ?: ObjectId ;
60
67
61
68
/**
62
69
* Create a succeeded event
@@ -68,22 +75,28 @@ export class CommandSucceededEvent {
68
75
* @param started - a high resolution tuple timestamp of when the command was first sent, to calculate duration
69
76
*/
70
77
constructor (
71
- pool : Connection | ConnectionPool ,
78
+ connection : Connection ,
72
79
command : WriteProtocolMessageType ,
73
80
reply : Document | undefined ,
74
81
started : number
75
82
) {
76
83
const cmd = extractCommand ( command ) ;
77
84
const commandName = extractCommandName ( cmd ) ;
78
- const { address, connectionId } = extractConnectionDetails ( pool ) ;
85
+ const { address, connectionId, serviceId } = extractConnectionDetails ( connection ) ;
79
86
80
87
this . address = address ;
81
88
this . connectionId = connectionId ;
89
+ this . serviceId = serviceId ;
82
90
this . requestId = command . requestId ;
83
91
this . commandName = commandName ;
84
92
this . duration = calculateDurationInMs ( started ) ;
85
93
this . reply = maybeRedact ( commandName , cmd , extractReply ( command , reply ) ) ;
86
94
}
95
+
96
+ /* @internal */
97
+ get hasServiceId ( ) : boolean {
98
+ return ! ! this . serviceId ;
99
+ }
87
100
}
88
101
89
102
/**
@@ -98,6 +111,8 @@ export class CommandFailedEvent {
98
111
duration : number ;
99
112
commandName : string ;
100
113
failure : Error ;
114
+ serviceId ?: ObjectId ;
115
+
101
116
/**
102
117
* Create a failure event
103
118
*
@@ -108,23 +123,29 @@ export class CommandFailedEvent {
108
123
* @param started - a high resolution tuple timestamp of when the command was first sent, to calculate duration
109
124
*/
110
125
constructor (
111
- pool : Connection | ConnectionPool ,
126
+ connection : Connection ,
112
127
command : WriteProtocolMessageType ,
113
128
error : Error | Document ,
114
129
started : number
115
130
) {
116
131
const cmd = extractCommand ( command ) ;
117
132
const commandName = extractCommandName ( cmd ) ;
118
- const { address, connectionId } = extractConnectionDetails ( pool ) ;
133
+ const { address, connectionId, serviceId } = extractConnectionDetails ( connection ) ;
119
134
120
135
this . address = address ;
121
136
this . connectionId = connectionId ;
137
+ this . serviceId = serviceId ;
122
138
123
139
this . requestId = command . requestId ;
124
140
this . commandName = commandName ;
125
141
this . duration = calculateDurationInMs ( started ) ;
126
142
this . failure = maybeRedact ( commandName , cmd , error ) as Error ;
127
143
}
144
+
145
+ /* @internal */
146
+ get hasServiceId ( ) : boolean {
147
+ return ! ! this . serviceId ;
148
+ }
128
149
}
129
150
130
151
/** Commands that we want to redact because of the sensitive nature of their contents */
@@ -300,13 +321,14 @@ function extractReply(command: WriteProtocolMessageType, reply?: Document) {
300
321
return deepCopy ( reply . result ? reply . result : reply ) ;
301
322
}
302
323
303
- function extractConnectionDetails ( connection : Connection | ConnectionPool ) {
324
+ function extractConnectionDetails ( connection : Connection ) {
304
325
let connectionId ;
305
326
if ( 'id' in connection ) {
306
327
connectionId = connection . id ;
307
328
}
308
329
return {
309
330
address : connection . address ,
331
+ serviceId : connection . serviceId ,
310
332
connectionId
311
333
} ;
312
334
}
0 commit comments