Handle connection attributes conditionally for metrics and set connection data on exceptions in cluster error handling#3964
Merged
petyaslavova merged 4 commits intomasterfrom Feb 20, 2026
Conversation
…nection for Otel metrics. Added additional handling when providing of host/port info for metrics in case of error handling in cluster execute methods
Contributor
There was a problem hiding this comment.
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 avoidAttributeErrorfor connections without those attributes. - Enhanced cluster error handling to attach a
connection-like object (connection orClusterNode) 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.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
5 tasks
vladvildanov
approved these changes
Feb 20, 2026
This was referenced Feb 20, 2026
Closed
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>
1 task
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 usegetattr(conn, "host", None)andgetattr(conn, "port", None)to prevent AttributeError when these attributes are missing.In
cluster.py, error handling has been enhanced to set theconnectionattribute 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_commandandTransactionStrategy.The
record_error_countmethod in bothmetrics.pyandrecorder.pynow 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.