Skip to content

Commit 18b5352

Browse files
authored
Fix AttributeError in cluster metrics recording when connection is None or ClusterNode object instance is used to extract the connection info (#3999)
1 parent 53e6d8a commit 18b5352

3 files changed

Lines changed: 735 additions & 107 deletions

File tree

redis/cluster.py

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1560,7 +1560,7 @@ def _execute_command(self, target_node, *args, **kwargs):
15601560
self._record_command_metric(
15611561
command_name=command,
15621562
duration_seconds=time.monotonic() - start_time,
1563-
connection=connection,
1563+
connection=e.connection,
15641564
error=e,
15651565
)
15661566
raise
@@ -1576,7 +1576,7 @@ def _execute_command(self, target_node, *args, **kwargs):
15761576
self._record_command_metric(
15771577
command_name=command,
15781578
duration_seconds=time.monotonic() - start_time,
1579-
connection=connection,
1579+
connection=e.connection,
15801580
error=e,
15811581
)
15821582
raise
@@ -1615,11 +1615,10 @@ def _execute_command(self, target_node, *args, **kwargs):
16151615

16161616
# DON'T set redis_connection = None - keep the pool for reuse
16171617
self.nodes_manager.initialize()
1618-
e.connection = connection
16191618
self._record_command_metric(
16201619
command_name=command,
16211620
duration_seconds=time.monotonic() - start_time,
1622-
connection=connection,
1621+
connection=e.connection,
16231622
error=e,
16241623
)
16251624
raise e
@@ -1723,17 +1722,19 @@ def _execute_command(self, target_node, *args, **kwargs):
17231722
self._record_command_metric(
17241723
command_name=command,
17251724
duration_seconds=time.monotonic() - start_time,
1726-
connection=connection,
1725+
connection=e.connection,
17271726
error=e,
17281727
)
17291728
raise
17301729
except ResponseError as e:
17311730
# this is used to report the metrics based on host and port info
1732-
e.connection = connection
1731+
# ResponseError typically happens after get_connection() succeeds,
1732+
# so connection should be available
1733+
e.connection = connection if connection else target_node
17331734
self._record_command_metric(
17341735
command_name=command,
17351736
duration_seconds=time.monotonic() - start_time,
1736-
connection=connection,
1737+
connection=e.connection,
17371738
error=e,
17381739
)
17391740
raise
@@ -1748,7 +1749,7 @@ def _execute_command(self, target_node, *args, **kwargs):
17481749
self._record_command_metric(
17491750
command_name=command,
17501751
duration_seconds=time.monotonic() - start_time,
1751-
connection=connection,
1752+
connection=e.connection,
17521753
error=e,
17531754
)
17541755
raise e
@@ -1780,12 +1781,16 @@ def _record_command_metric(
17801781
"""
17811782
Records operation duration metric directly.
17821783
"""
1784+
host = connection.host if connection else "unknown"
1785+
port = connection.port if connection else 0
1786+
db = str(connection.db) if connection and hasattr(connection, "db") else "0"
1787+
17831788
record_operation_duration(
17841789
command_name=command_name,
17851790
duration_seconds=duration_seconds,
1786-
server_address=connection.host,
1787-
server_port=connection.port,
1788-
db_namespace=str(connection.db),
1791+
server_address=host,
1792+
server_port=port,
1793+
db_namespace=db,
17891794
error=error,
17901795
)
17911796

0 commit comments

Comments
 (0)