@@ -561,7 +561,7 @@ def _is_speculative_authenticate(command_name: str, doc: Mapping[str, Any]) -> b
561561class _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
609619class 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 :
0 commit comments