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

Function record_operation_duration

redis/observability/recorder.py:55–113  ·  view source on GitHub ↗

Record a Redis command execution duration. This is a simple, clean API that Redis core 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_seconds

(
    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,
    batch_size: Optional[int] = None,  # noqa
    retry_attempts: Optional[int] = None,
)

Source from the content-addressed store, hash-verified

53 version="7.2.1",
54)
55def record_operation_duration(
56 command_name: str,
57 duration_seconds: float,
58 server_address: Optional[str] = None,
59 server_port: Optional[int] = None,
60 db_namespace: Optional[str] = None,
61 error: Optional[Exception] = None,
62 is_blocking: Optional[bool] = None,
63 batch_size: Optional[int] = None, # noqa
64 retry_attempts: Optional[int] = None,
65) -> None:
66 """
67 Record a Redis command execution duration.
68
69 This is a simple, clean API that Redis core code can call directly.
70 If observability is not enabled, this returns immediately with zero overhead.
71
72 Args:
73 command_name: Redis command name (e.g., 'GET', 'SET')
74 duration_seconds: Command execution time in seconds
75 server_address: Redis server address
76 server_port: Redis server port
77 db_namespace: Redis database index
78 error: Exception if command failed, None if successful
79 is_blocking: Whether the operation is a blocking command
80 batch_size: Number of commands in batch (for pipelines/transactions)
81 retry_attempts: Number of retry attempts made
82
83 Example:
84 >>> start = time.monotonic()
85 >>> # ... execute command ...
86 >>> record_operation_duration('SET', time.monotonic() - start, 'localhost', 6379, '0')
87 """
88 global _metrics_collector
89
90 # Fast path: if collector not initialized, observability is disabled
91 if _metrics_collector is None:
92 # Try to initialize (only once)
93 _metrics_collector = _get_or_create_collector()
94 if _metrics_collector is None:
95 return # Observability not enabled
96
97 # Record the metric
98 try:
99 _metrics_collector.record_operation_duration(
100 command_name=command_name,
101 duration_seconds=duration_seconds,
102 server_address=server_address,
103 server_port=server_port,
104 db_namespace=db_namespace,
105 error_type=error,
106 network_peer_address=server_address,
107 network_peer_port=server_port,
108 is_blocking=is_blocking,
109 retry_attempts=retry_attempts,
110 )
111 except Exception:
112 # Don't let metric recording errors break Redis operations

Calls 2

_get_or_create_collectorFunction · 0.70