Skip to content

Handle connection attributes conditionally for metrics and set connection data on exceptions in cluster error handling#3964

Merged
petyaslavova merged 4 commits intomasterfrom
ps_fix_metrics_extraction_corner_cases
Feb 20, 2026
Merged

Handle connection attributes conditionally for metrics and set connection data on exceptions in cluster error handling#3964
petyaslavova merged 4 commits intomasterfrom
ps_fix_metrics_extraction_corner_cases

Conversation

@petyaslavova
Copy link
Copy Markdown
Collaborator

This PR modifies the metrics collection code to safely handle connections that don't have host and port attributes, such as UnixDomainSocketConnection. In client.py, direct attribute access has been changed to use getattr(conn, "host", None) and getattr(conn, "port", None) to prevent AttributeError when these attributes are missing.

In cluster.py, error handling has been enhanced to set the connection attribute on exceptions for metrics reporting. When errors occur during cluster operations, the exception's connection attribute is set to either the actual connection object (if available) or the target ClusterNode as a fallback, ensuring metrics can always extract server address and port information. This includes handling for AuthenticationError, MaxConnectionsError, ConnectionError, TimeoutError, ClusterDownError, SlotNotCoveredError, ResponseError, and generic exceptions in both _execute_command and TransactionStrategy.

The record_error_count method in both metrics.py and recorder.py now accepts optional parameters for server_address and server_port, allowing graceful handling of None values. Comprehensive unit tests validate that Redis client, PubSub, Pipeline, and cluster operations work correctly with connections lacking host/port attributes, and that cluster error handling properly sets connection attributes on exceptions.

…nection for Otel metrics. Added additional handling when providing of host/port info for metrics in case of error handling in cluster execute methods
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR improves observability robustness by preventing metrics code from assuming all connection types expose host/port, and by enriching cluster exceptions with connection context so error metrics can still be attributed to a node.

Changes:

  • Updated sync client metrics recording to use getattr(conn, "host"/"port", None) to avoid AttributeError for connections without those attributes.
  • Enhanced cluster error handling to attach a connection-like object (connection or ClusterNode) to exceptions for metrics attribution.
  • Added unit tests covering host/port-less connections across Redis client, PubSub, Pipeline, and cluster error paths.

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
redis/client.py Uses getattr() for host/port when recording metrics to support non-TCP connection types.
redis/cluster.py Sets e.connection on several exception paths to support consistent metrics attribution in cluster operations.
redis/observability/metrics.py Makes record_error_count accept optional address/port inputs for graceful omission of missing attributes.
redis/observability/recorder.py Updates public recorder wrapper signature for record_error_count to allow optional server/network fields.
tests/test_observability/test_metrics_connection_attributes.py Adds tests ensuring metrics recording does not crash when a connection lacks host/port.
tests/test_observability/test_cluster_metrics_error_handling.py Adds tests verifying cluster exceptions get a connection attribute suitable for metrics extraction.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

petyaslavova and others added 2 commits February 19, 2026 18:37
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@petyaslavova petyaslavova merged commit 2a49397 into master Feb 20, 2026
119 of 121 checks passed
@petyaslavova petyaslavova deleted the ps_fix_metrics_extraction_corner_cases branch February 20, 2026 12:03
petyaslavova added a commit that referenced this pull request Feb 25, 2026
…tion data on exceptions in cluster error handling (#3964)

* Adding conditional accessing of host and port attributes from the connection for Otel metrics. Added additional handling when providing of host/port info for metrics in case of error handling in cluster execute methods

* Update redis/cluster.py

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Update redis/cluster.py

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Fixing unit test after applied review comment.

---------

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants