Skip to content

Commit 63e9973

Browse files
authored
RUST-910 Expose server connectionId in command monitoring events (#682)
1 parent 41745bb commit 63e9973

File tree

13 files changed

+370
-26
lines changed

13 files changed

+370
-26
lines changed

src/cmap/conn/mod.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,16 @@ pub(crate) use wire::next_request_id;
3939

4040
/// User-facing information about a connection to the database.
4141
#[derive(Clone, Debug, Serialize)]
42+
#[serde(rename_all = "camelCase")]
43+
#[non_exhaustive]
4244
pub struct ConnectionInfo {
4345
/// A driver-generated identifier that uniquely identifies the connection.
4446
pub id: u32,
4547

48+
/// A server-generated identifier that uniquely identifies the connection. Available on server
49+
/// versions 4.2+. This may be used to correlate driver connections with server logs.
50+
pub server_id: Option<i32>,
51+
4652
/// The address that the connection is connected to.
4753
pub address: ServerAddress,
4854
}
@@ -51,7 +57,11 @@ pub struct ConnectionInfo {
5157
#[derive(Derivative)]
5258
#[derivative(Debug)]
5359
pub(crate) struct Connection {
60+
/// Driver-generated ID for the connection.
5461
pub(super) id: u32,
62+
/// Server-generated ID for the connection.
63+
pub(crate) server_id: Option<i32>,
64+
5565
pub(crate) address: ServerAddress,
5666
pub(crate) generation: ConnectionGeneration,
5767

@@ -109,6 +119,7 @@ impl Connection {
109119

110120
let conn = Self {
111121
id,
122+
server_id: None,
112123
generation: ConnectionGeneration::Normal(generation),
113124
pool_manager: None,
114125
command_executing: false,
@@ -174,6 +185,7 @@ impl Connection {
174185
pub(crate) fn info(&self) -> ConnectionInfo {
175186
ConnectionInfo {
176187
id: self.id,
188+
server_id: self.server_id,
177189
address: self.address.clone(),
178190
}
179191
}
@@ -370,6 +382,7 @@ impl Connection {
370382
fn take(&mut self) -> Connection {
371383
Connection {
372384
id: self.id,
385+
server_id: self.server_id,
373386
address: self.address.clone(),
374387
generation: self.generation.clone(),
375388
stream: std::mem::replace(&mut self.stream, AsyncStream::Null),

src/cmap/establish/handshake/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,8 @@ impl Handshaker {
269269
}
270270
}
271271

272+
conn.server_id = hello_reply.command_response.connection_id;
273+
272274
Ok(HandshakeResult {
273275
hello_reply,
274276
first_round,

src/event/command.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@ use serde::Serialize;
88
use crate::{
99
bson::{oid::ObjectId, Document},
1010
bson_util::serialize_error_as_string,
11-
cmap::ConnectionInfo,
1211
error::Error,
1312
};
1413

14+
pub use crate::cmap::ConnectionInfo;
15+
1516
/// An event that triggers when a database command is initiated.
1617
#[derive(Clone, Debug, Serialize)]
1718
#[serde(rename_all = "camelCase")]

src/hello.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,10 @@ pub(crate) struct HelloCommandResponse {
237237

238238
/// The maximum permitted size of a BSON wire protocol message.
239239
pub max_message_size_bytes: i32,
240+
241+
/// The server-generated ID for the connection the "hello" command was run on.
242+
/// Present on server versions 4.2+.
243+
pub connection_id: Option<i32>,
240244
}
241245

242246
impl PartialEq for HelloCommandResponse {

src/sdam/description/topology/test/sdam.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ impl From<TestHelloCommandResponse> for HelloCommandResponse {
126126
compressors: None,
127127
hello_ok: test.hello_ok,
128128
max_message_size_bytes: 48 * 1024 * 1024,
129+
connection_id: None,
129130
}
130131
}
131132
}
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
{
2+
"description": "pre-42-server-connection-id",
3+
"schemaVersion": "1.6",
4+
"runOnRequirements": [
5+
{
6+
"maxServerVersion": "4.0.99"
7+
}
8+
],
9+
"createEntities": [
10+
{
11+
"client": {
12+
"id": "client",
13+
"observeEvents": [
14+
"commandStartedEvent",
15+
"commandSucceededEvent",
16+
"commandFailedEvent"
17+
]
18+
}
19+
},
20+
{
21+
"database": {
22+
"id": "database",
23+
"client": "client",
24+
"databaseName": "server-connection-id-tests"
25+
}
26+
},
27+
{
28+
"collection": {
29+
"id": "collection",
30+
"database": "database",
31+
"collectionName": "coll"
32+
}
33+
}
34+
],
35+
"initialData": [
36+
{
37+
"databaseName": "server-connection-id-tests",
38+
"collectionName": "coll",
39+
"documents": []
40+
}
41+
],
42+
"tests": [
43+
{
44+
"description": "command events do not include server connection id",
45+
"operations": [
46+
{
47+
"name": "insertOne",
48+
"object": "collection",
49+
"arguments": {
50+
"document": {
51+
"x": 1
52+
}
53+
}
54+
},
55+
{
56+
"name": "find",
57+
"object": "collection",
58+
"arguments": {
59+
"filter": {
60+
"$or": true
61+
}
62+
},
63+
"expectError": {
64+
"isError": true
65+
}
66+
}
67+
],
68+
"expectEvents": [
69+
{
70+
"client": "client",
71+
"events": [
72+
{
73+
"commandStartedEvent": {
74+
"commandName": "insert",
75+
"hasServerConnectionId": false
76+
}
77+
},
78+
{
79+
"commandSucceededEvent": {
80+
"commandName": "insert",
81+
"hasServerConnectionId": false
82+
}
83+
},
84+
{
85+
"commandStartedEvent": {
86+
"commandName": "find",
87+
"hasServerConnectionId": false
88+
}
89+
},
90+
{
91+
"commandFailedEvent": {
92+
"commandName": "find",
93+
"hasServerConnectionId": false
94+
}
95+
}
96+
]
97+
}
98+
]
99+
}
100+
]
101+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
description: "pre-42-server-connection-id"
2+
3+
schemaVersion: "1.6"
4+
5+
runOnRequirements:
6+
- maxServerVersion: "4.0.99"
7+
8+
createEntities:
9+
- client:
10+
id: &client client
11+
observeEvents:
12+
- commandStartedEvent
13+
- commandSucceededEvent
14+
- commandFailedEvent
15+
- database:
16+
id: &database database
17+
client: *client
18+
databaseName: &databaseName server-connection-id-tests
19+
- collection:
20+
id: &collection collection
21+
database: *database
22+
collectionName: &collectionName coll
23+
24+
initialData:
25+
- databaseName: *databaseName
26+
collectionName: *collectionName
27+
documents: []
28+
29+
tests:
30+
- description: "command events do not include server connection id"
31+
operations:
32+
- name: insertOne
33+
object: *collection
34+
arguments:
35+
document: { x: 1 }
36+
- name: find
37+
object: *collection
38+
arguments:
39+
filter: { $or: true }
40+
expectError:
41+
isError: true
42+
expectEvents:
43+
- client: *client
44+
events:
45+
- commandStartedEvent:
46+
commandName: insert
47+
hasServerConnectionId: false
48+
- commandSucceededEvent:
49+
commandName: insert
50+
hasServerConnectionId: false
51+
- commandStartedEvent:
52+
commandName: find
53+
hasServerConnectionId: false
54+
- commandFailedEvent:
55+
commandName: find
56+
hasServerConnectionId: false
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
{
2+
"description": "server-connection-id",
3+
"schemaVersion": "1.6",
4+
"runOnRequirements": [
5+
{
6+
"minServerVersion": "4.2"
7+
}
8+
],
9+
"createEntities": [
10+
{
11+
"client": {
12+
"id": "client",
13+
"observeEvents": [
14+
"commandStartedEvent",
15+
"commandSucceededEvent",
16+
"commandFailedEvent"
17+
]
18+
}
19+
},
20+
{
21+
"database": {
22+
"id": "database",
23+
"client": "client",
24+
"databaseName": "server-connection-id-tests"
25+
}
26+
},
27+
{
28+
"collection": {
29+
"id": "collection",
30+
"database": "database",
31+
"collectionName": "coll"
32+
}
33+
}
34+
],
35+
"initialData": [
36+
{
37+
"databaseName": "server-connection-id-tests",
38+
"collectionName": "coll",
39+
"documents": []
40+
}
41+
],
42+
"tests": [
43+
{
44+
"description": "command events include server connection id",
45+
"operations": [
46+
{
47+
"name": "insertOne",
48+
"object": "collection",
49+
"arguments": {
50+
"document": {
51+
"x": 1
52+
}
53+
}
54+
},
55+
{
56+
"name": "find",
57+
"object": "collection",
58+
"arguments": {
59+
"filter": {
60+
"$or": true
61+
}
62+
},
63+
"expectError": {
64+
"isError": true
65+
}
66+
}
67+
],
68+
"expectEvents": [
69+
{
70+
"client": "client",
71+
"events": [
72+
{
73+
"commandStartedEvent": {
74+
"commandName": "insert",
75+
"hasServerConnectionId": true
76+
}
77+
},
78+
{
79+
"commandSucceededEvent": {
80+
"commandName": "insert",
81+
"hasServerConnectionId": true
82+
}
83+
},
84+
{
85+
"commandStartedEvent": {
86+
"commandName": "find",
87+
"hasServerConnectionId": true
88+
}
89+
},
90+
{
91+
"commandFailedEvent": {
92+
"commandName": "find",
93+
"hasServerConnectionId": true
94+
}
95+
}
96+
]
97+
}
98+
]
99+
}
100+
]
101+
}

0 commit comments

Comments
 (0)