Skip to content

Commit 5565572

Browse files
authored
PYTHON-3598 Add Command(Succeeded|Failed)Event.database_name property (#1368)
1 parent cbd61c5 commit 5565572

File tree

9 files changed

+273
-34
lines changed

9 files changed

+273
-34
lines changed

doc/changelog.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
Changelog
22
=========
33

4+
Changes in Version 4.6
5+
----------------------
6+
7+
PyMongo 4.6 brings a number of improvements including:
8+
- Added the :attr:`pymongo.monitoring.CommandSucceededEvent.database_name` property.
9+
- Added the :attr:`pymongo.monitoring.CommandFailedEvent.database_name` property.
10+
411
Changes in Version 4.5
512
----------------------
613

pymongo/message.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1097,6 +1097,7 @@ def _succeed(self, request_id: int, reply: _DocumentOut, duration: timedelta) ->
10971097
self.conn.address,
10981098
self.op_id,
10991099
self.conn.service_id,
1100+
database_name=self.db_name,
11001101
)
11011102

11021103
def _fail(self, request_id: int, failure: _DocumentOut, duration: timedelta) -> None:
@@ -1109,6 +1110,7 @@ def _fail(self, request_id: int, failure: _DocumentOut, duration: timedelta) ->
11091110
self.conn.address,
11101111
self.op_id,
11111112
self.conn.service_id,
1113+
database_name=self.db_name,
11121114
)
11131115

11141116

pymongo/monitoring.py

Lines changed: 59 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -561,7 +561,7 @@ def _is_speculative_authenticate(command_name: str, doc: Mapping[str, Any]) -> b
561561
class _CommandEvent:
562562
"""Base class for command events."""
563563

564-
__slots__ = ("__cmd_name", "__rqst_id", "__conn_id", "__op_id", "__service_id")
564+
__slots__ = ("__cmd_name", "__rqst_id", "__conn_id", "__op_id", "__service_id", "__db")
565565

566566
def __init__(
567567
self,
@@ -570,12 +570,14 @@ def __init__(
570570
connection_id: _Address,
571571
operation_id: Optional[int],
572572
service_id: Optional[ObjectId] = None,
573+
database_name: str = "",
573574
) -> None:
574575
self.__cmd_name = command_name
575576
self.__rqst_id = request_id
576577
self.__conn_id = connection_id
577578
self.__op_id = operation_id
578579
self.__service_id = service_id
580+
self.__db = database_name
579581

580582
@property
581583
def command_name(self) -> str:
@@ -605,6 +607,14 @@ def operation_id(self) -> Optional[int]:
605607
"""An id for this series of events or None."""
606608
return self.__op_id
607609

610+
@property
611+
def database_name(self) -> str:
612+
"""The database_name this command was sent to, or ``""``.
613+
614+
.. versionadded:: 4.6
615+
"""
616+
return self.__db
617+
608618

609619
class CommandStartedEvent(_CommandEvent):
610620
"""Event published when a command starts.
@@ -619,7 +629,7 @@ class CommandStartedEvent(_CommandEvent):
619629
- `service_id`: The service_id this command was sent to, or ``None``.
620630
"""
621631

622-
__slots__ = ("__cmd", "__db")
632+
__slots__ = ("__cmd",)
623633

624634
def __init__(
625635
self,
@@ -635,14 +645,18 @@ def __init__(
635645
# Command name must be first key.
636646
command_name = next(iter(command))
637647
super().__init__(
638-
command_name, request_id, connection_id, operation_id, service_id=service_id
648+
command_name,
649+
request_id,
650+
connection_id,
651+
operation_id,
652+
service_id=service_id,
653+
database_name=database_name,
639654
)
640655
cmd_name = command_name.lower()
641656
if cmd_name in _SENSITIVE_COMMANDS or _is_speculative_authenticate(cmd_name, command):
642657
self.__cmd: _DocumentOut = {}
643658
else:
644659
self.__cmd = command
645-
self.__db = database_name
646660

647661
@property
648662
def command(self) -> _DocumentOut:
@@ -652,7 +666,7 @@ def command(self) -> _DocumentOut:
652666
@property
653667
def database_name(self) -> str:
654668
"""The name of the database this command was run against."""
655-
return self.__db
669+
return super().database_name
656670

657671
def __repr__(self) -> str:
658672
return ("<{} {} db: {!r}, command: {!r}, operation_id: {}, service_id: {}>").format(
@@ -677,6 +691,7 @@ class CommandSucceededEvent(_CommandEvent):
677691
was sent to.
678692
- `operation_id`: An optional identifier for a series of related events.
679693
- `service_id`: The service_id this command was sent to, or ``None``.
694+
- `database_name`: The database this command was sent to, or ``""``.
680695
"""
681696

682697
__slots__ = ("__duration_micros", "__reply")
@@ -690,9 +705,15 @@ def __init__(
690705
connection_id: _Address,
691706
operation_id: Optional[int],
692707
service_id: Optional[ObjectId] = None,
708+
database_name: str = "",
693709
) -> None:
694710
super().__init__(
695-
command_name, request_id, connection_id, operation_id, service_id=service_id
711+
command_name,
712+
request_id,
713+
connection_id,
714+
operation_id,
715+
service_id=service_id,
716+
database_name=database_name,
696717
)
697718
self.__duration_micros = _to_micros(duration)
698719
cmd_name = command_name.lower()
@@ -713,10 +734,11 @@ def reply(self) -> _DocumentOut:
713734

714735
def __repr__(self) -> str:
715736
return (
716-
"<{} {} command: {!r}, operation_id: {}, duration_micros: {}, service_id: {}>"
737+
"<{} {} db: {!r}, command: {!r}, operation_id: {}, duration_micros: {}, service_id: {}>"
717738
).format(
718739
self.__class__.__name__,
719740
self.connection_id,
741+
self.database_name,
720742
self.command_name,
721743
self.operation_id,
722744
self.duration_micros,
@@ -736,6 +758,7 @@ class CommandFailedEvent(_CommandEvent):
736758
was sent to.
737759
- `operation_id`: An optional identifier for a series of related events.
738760
- `service_id`: The service_id this command was sent to, or ``None``.
761+
- `database_name`: The database this command was sent to, or ``""``.
739762
"""
740763

741764
__slots__ = ("__duration_micros", "__failure")
@@ -749,9 +772,15 @@ def __init__(
749772
connection_id: _Address,
750773
operation_id: Optional[int],
751774
service_id: Optional[ObjectId] = None,
775+
database_name: str = "",
752776
) -> None:
753777
super().__init__(
754-
command_name, request_id, connection_id, operation_id, service_id=service_id
778+
command_name,
779+
request_id,
780+
connection_id,
781+
operation_id,
782+
service_id=service_id,
783+
database_name=database_name,
755784
)
756785
self.__duration_micros = _to_micros(duration)
757786
self.__failure = failure
@@ -768,11 +797,12 @@ def failure(self) -> _DocumentOut:
768797

769798
def __repr__(self) -> str:
770799
return (
771-
"<{} {} command: {!r}, operation_id: {}, duration_micros: {}, "
800+
"<{} {} db: {!r}, command: {!r}, operation_id: {}, duration_micros: {}, "
772801
"failure: {!r}, service_id: {}>"
773802
).format(
774803
self.__class__.__name__,
775804
self.connection_id,
805+
self.database_name,
776806
self.command_name,
777807
self.operation_id,
778808
self.duration_micros,
@@ -1491,6 +1521,7 @@ def publish_command_success(
14911521
op_id: Optional[int] = None,
14921522
service_id: Optional[ObjectId] = None,
14931523
speculative_hello: bool = False,
1524+
database_name: str = "",
14941525
) -> None:
14951526
"""Publish a CommandSucceededEvent to all command listeners.
14961527
@@ -1504,6 +1535,7 @@ def publish_command_success(
15041535
- `op_id`: The (optional) operation id for this operation.
15051536
- `service_id`: The service_id this command was sent to, or ``None``.
15061537
- `speculative_hello`: Was the command sent with speculative auth?
1538+
- `database_name`: The database this command was sent to, or ``""``.
15071539
"""
15081540
if op_id is None:
15091541
op_id = request_id
@@ -1512,7 +1544,14 @@ def publish_command_success(
15121544
# speculativeAuthenticate.
15131545
reply = {}
15141546
event = CommandSucceededEvent(
1515-
duration, reply, command_name, request_id, connection_id, op_id, service_id
1547+
duration,
1548+
reply,
1549+
command_name,
1550+
request_id,
1551+
connection_id,
1552+
op_id,
1553+
service_id,
1554+
database_name=database_name,
15161555
)
15171556
for subscriber in self.__command_listeners:
15181557
try:
@@ -1529,6 +1568,7 @@ def publish_command_failure(
15291568
connection_id: _Address,
15301569
op_id: Optional[int] = None,
15311570
service_id: Optional[ObjectId] = None,
1571+
database_name: str = "",
15321572
) -> None:
15331573
"""Publish a CommandFailedEvent to all command listeners.
15341574
@@ -1542,11 +1582,19 @@ def publish_command_failure(
15421582
command was sent to.
15431583
- `op_id`: The (optional) operation id for this operation.
15441584
- `service_id`: The service_id this command was sent to, or ``None``.
1585+
- `database_name`: The database this command was sent to, or ``""``.
15451586
"""
15461587
if op_id is None:
15471588
op_id = request_id
15481589
event = CommandFailedEvent(
1549-
duration, failure, command_name, request_id, connection_id, op_id, service_id=service_id
1590+
duration,
1591+
failure,
1592+
command_name,
1593+
request_id,
1594+
connection_id,
1595+
op_id,
1596+
service_id=service_id,
1597+
database_name=database_name,
15501598
)
15511599
for subscriber in self.__command_listeners:
15521600
try:

pymongo/network.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,13 @@ def command(
205205
assert listeners is not None
206206
assert address is not None
207207
listeners.publish_command_failure(
208-
duration, failure, name, request_id, address, service_id=conn.service_id
208+
duration,
209+
failure,
210+
name,
211+
request_id,
212+
address,
213+
service_id=conn.service_id,
214+
database_name=dbname,
209215
)
210216
raise
211217
if publish:
@@ -220,6 +226,7 @@ def command(
220226
address,
221227
service_id=conn.service_id,
222228
speculative_hello=speculative_hello,
229+
database_name=dbname,
223230
)
224231

225232
if client and client._encrypter and reply:

pymongo/server.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ def run_operation(
178178
request_id,
179179
conn.address,
180180
service_id=conn.service_id,
181+
database_name=dbn,
181182
)
182183
raise
183184

@@ -203,6 +204,7 @@ def run_operation(
203204
request_id,
204205
conn.address,
205206
service_id=conn.service_id,
207+
database_name=dbn,
206208
)
207209

208210
# Decrypt response.

test/command_monitoring/find.json

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"description": "find",
3-
"schemaVersion": "1.1",
3+
"schemaVersion": "1.15",
44
"createEntities": [
55
{
66
"client": {
@@ -103,7 +103,8 @@
103103
]
104104
}
105105
},
106-
"commandName": "find"
106+
"commandName": "find",
107+
"databaseName": "command-monitoring-tests"
107108
}
108109
}
109110
]
@@ -198,7 +199,8 @@
198199
]
199200
}
200201
},
201-
"commandName": "find"
202+
"commandName": "find",
203+
"databaseName": "command-monitoring-tests"
202204
}
203205
}
204206
]
@@ -262,7 +264,8 @@
262264
]
263265
}
264266
},
265-
"commandName": "find"
267+
"commandName": "find",
268+
"databaseName": "command-monitoring-tests"
266269
}
267270
}
268271
]
@@ -338,7 +341,8 @@
338341
]
339342
}
340343
},
341-
"commandName": "find"
344+
"commandName": "find",
345+
"databaseName": "command-monitoring-tests"
342346
}
343347
},
344348
{
@@ -376,7 +380,8 @@
376380
]
377381
}
378382
},
379-
"commandName": "getMore"
383+
"commandName": "getMore",
384+
"databaseName": "command-monitoring-tests"
380385
}
381386
}
382387
]
@@ -464,7 +469,8 @@
464469
]
465470
}
466471
},
467-
"commandName": "find"
472+
"commandName": "find",
473+
"databaseName": "command-monitoring-tests"
468474
}
469475
},
470476
{
@@ -498,7 +504,8 @@
498504
]
499505
}
500506
},
501-
"commandName": "getMore"
507+
"commandName": "getMore",
508+
"databaseName": "command-monitoring-tests"
502509
}
503510
}
504511
]
@@ -539,7 +546,8 @@
539546
},
540547
{
541548
"commandFailedEvent": {
542-
"commandName": "find"
549+
"commandName": "find",
550+
"databaseName": "command-monitoring-tests"
543551
}
544552
}
545553
]

0 commit comments

Comments
 (0)