52
52
53
53
from .errors import ErrInvalidUserCredentials , ErrStaleConnection
54
54
from .msg import Msg
55
+ from .stats import ClientStats , StatsInterface
55
56
from .subscription import (
56
57
DEFAULT_SUB_PENDING_BYTES_LIMIT ,
57
58
DEFAULT_SUB_PENDING_MSGS_LIMIT ,
@@ -127,6 +128,7 @@ class Srv:
127
128
server_version : Optional [str ] = None
128
129
129
130
131
+
130
132
class ServerVersion :
131
133
132
134
def __init__ (self , server_version : str ) -> None :
@@ -230,6 +232,7 @@ class Client:
230
232
"""
231
233
232
234
msg_class : type [Msg ] = Msg
235
+ stats : StatsInterface
233
236
234
237
# FIXME: Use an enum instead.
235
238
DISCONNECTED = 0
@@ -314,14 +317,7 @@ def __init__(self) -> None:
314
317
self ._public_nkey : Optional [str ] = None
315
318
316
319
self .options : Dict [str , Any ] = {}
317
- self .stats = {
318
- "in_msgs" : 0 ,
319
- "out_msgs" : 0 ,
320
- "in_bytes" : 0 ,
321
- "out_bytes" : 0 ,
322
- "reconnects" : 0 ,
323
- "errors_received" : 0 ,
324
- }
320
+ self .stats = ClientStats ()
325
321
326
322
async def connect (
327
323
self ,
@@ -947,8 +943,7 @@ async def _send_publish(
947
943
hdr .extend (_CRLF_ )
948
944
pub_cmd = prot_command .hpub_cmd (subject , reply , hdr , payload )
949
945
950
- self .stats ["out_msgs" ] += 1
951
- self .stats ["out_bytes" ] += payload_size
946
+ self .stats .message_sent (subject , payload_size , headers )
952
947
await self ._send_command (pub_cmd )
953
948
if self ._flush_queue is not None and self ._flush_queue .empty ():
954
949
await self ._flush_pending ()
@@ -1510,7 +1505,7 @@ async def _attempt_reconnect(self) -> None:
1510
1505
1511
1506
# Consider a reconnect to be done once CONNECT was
1512
1507
# processed by the server successfully.
1513
- self .stats [ "reconnects" ] += 1
1508
+ self .stats . client_reconnected ()
1514
1509
1515
1510
# Reset reconnect attempts for this server
1516
1511
# since have successfully connected.
@@ -1749,8 +1744,8 @@ async def _process_msg(
1749
1744
Process MSG sent by server.
1750
1745
"""
1751
1746
payload_size = len (data )
1752
- self . stats [ "in_msgs" ] += 1
1753
- self .stats [ "in_bytes" ] += payload_size
1747
+ hdr = await self . _process_headers ( headers )
1748
+ self .stats . message_received ( subject . decode (), payload_size , hdr )
1754
1749
1755
1750
sub = self ._subs .get (sid )
1756
1751
if not sub :
@@ -1764,8 +1759,6 @@ async def _process_msg(
1764
1759
# internal queue and the task will finish once the last
1765
1760
# message is processed.
1766
1761
self ._subs .pop (sid , None )
1767
-
1768
- hdr = await self ._process_headers (headers )
1769
1762
msg = self ._build_message (sid , subject , reply , data , hdr )
1770
1763
if not msg :
1771
1764
return
0 commit comments