MCPcopy
hub / github.com/redis/redis-py / record_operation_duration

Function record_operation_duration

redis/asyncio/observability/recorder.py:96–145  ·  view source on GitHub ↗

Record a Redis command execution duration. This is an async-safe API that Redis async client code can call directly. If observability is not enabled, this returns immediately with zero overhead. Args: command_name: Redis command name (e.g., 'GET', 'SET') duration_s

(
    command_name: str,
    duration_seconds: float,
    server_address: Optional[str] = None,
    server_port: Optional[int] = None,
    db_namespace: Optional[str] = None,
    error: Optional[Exception] = None,
    is_blocking: Optional[bool] = None,
    retry_attempts: Optional[int] = None,
)

Source from the content-addressed store, hash-verified

94
95
96async def record_operation_duration(
97 command_name: str,
98 duration_seconds: float,
99 server_address: Optional[str] = None,
100 server_port: Optional[int] = None,
101 db_namespace: Optional[str] = None,
102 error: Optional[Exception] = None,
103 is_blocking: Optional[bool] = None,
104 retry_attempts: Optional[int] = None,
105) -> None:
106 """
107 Record a Redis command execution duration.
108
109 This is an async-safe API that Redis async client code can call directly.
110 If observability is not enabled, this returns immediately with zero overhead.
111
112 Args:
113 command_name: Redis command name (e.g., 'GET', 'SET')
114 duration_seconds: Command execution time in seconds
115 server_address: Redis server address
116 server_port: Redis server port
117 db_namespace: Redis database index
118 error: Exception if command failed, None if successful
119 is_blocking: Whether the operation is a blocking command
120 retry_attempts: Number of retry attempts made
121
122 Example:
123 >>> start = time.monotonic()
124 >>> # ... execute command ...
125 >>> await record_operation_duration('SET', time.monotonic() - start, 'localhost', 6379, '0')
126 """
127 collector = _get_or_create_collector()
128 if collector is None:
129 return
130
131 try:
132 collector.record_operation_duration(
133 command_name=command_name,
134 duration_seconds=duration_seconds,
135 server_address=server_address,
136 server_port=server_port,
137 db_namespace=db_namespace,
138 error_type=error,
139 network_peer_address=server_address,
140 network_peer_port=server_port,
141 is_blocking=is_blocking,
142 retry_attempts=retry_attempts,
143 )
144 except Exception:
145 pass
146
147
148async def record_connection_create_time(

Callers 13

_close_connectionMethod · 0.90
execute_commandMethod · 0.90
_reconnectMethod · 0.90
_executeMethod · 0.90
executeMethod · 0.90
_executeMethod · 0.90
_raise_first_errorMethod · 0.90

Calls 2

_get_or_create_collectorFunction · 0.70

Tested by

no test coverage detected