Skip to content

Commit 453101d

Browse files
Fixes units and pool name attribute
Signed-off-by: Elena Kolevska <elena@kolevska.com>
1 parent 4dc1802 commit 453101d

File tree

3 files changed

+24
-13
lines changed

3 files changed

+24
-13
lines changed

redis/connection.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2638,6 +2638,11 @@ def __init__(
26382638
self._fork_lock = threading.RLock()
26392639
self._lock = threading.RLock()
26402640

2641+
# Generate unique pool ID for observability (matches go-redis behavior)
2642+
import secrets
2643+
2644+
self._pool_id = secrets.token_hex(4)
2645+
26412646
MaintNotificationsAbstractConnectionPool.__init__(
26422647
self,
26432648
maint_notifications_config=maint_notifications_config,
@@ -2906,8 +2911,10 @@ async def _mock(self, error: RedisError):
29062911
pass
29072912

29082913
def get_connection_count(self) -> List[tuple[int, dict]]:
2914+
from redis.observability.attributes import get_pool_name
2915+
29092916
attributes = AttributeBuilder.build_base_attributes()
2910-
attributes[DB_CLIENT_CONNECTION_POOL_NAME] = repr(self)
2917+
attributes[DB_CLIENT_CONNECTION_POOL_NAME] = get_pool_name(self)
29112918
free_connections_attributes = attributes.copy()
29122919
in_use_connections_attributes = attributes.copy()
29132920

redis/observability/attributes.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -352,22 +352,26 @@ def get_pool_name(pool: "ConnectionPoolInterface") -> str:
352352
Get a short string representation of a connection pool for observability.
353353
354354
This provides a concise pool identifier suitable for use as a metric attribute,
355-
in the format: ClassName(host:port/db)
355+
in the format: host:port_uniqueID (matching go-redis format)
356356
357357
Args:
358358
pool: Connection pool instance
359359
360360
Returns:
361-
Short pool name in format "ClassName(host:port/db)"
361+
Short pool name in format "host:port_uniqueID"
362362
363363
Example:
364364
>>> pool = ConnectionPool(host='localhost', port=6379, db=0)
365365
>>> get_pool_name(pool)
366-
'ConnectionPool(localhost:6379/0)'
366+
'localhost:6379_a1b2c3d4'
367367
"""
368368
host = pool.connection_kwargs.get("host", "unknown")
369369
port = pool.connection_kwargs.get("port", 6379)
370-
db = pool.connection_kwargs.get("db", 0)
371-
class_name = pool.__class__.__name__
372370

373-
return f"{class_name}({host}:{port}/{db})"
371+
# Get unique pool ID if available (added for observability)
372+
pool_id = getattr(pool, "_pool_id", "")
373+
374+
if pool_id:
375+
return f"{host}:{port}_{pool_id}"
376+
else:
377+
return f"{host}:{port}"

redis/observability/metrics.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ def _init_connection_basic_metrics(self) -> None:
128128
"""Initialize basic connection metrics."""
129129
self.connection_create_time = self.meter.create_histogram(
130130
name="db.client.connection.create_time",
131-
unit="{seconds}",
131+
unit="s",
132132
description="Time to create a new connection",
133133
explicit_bucket_boundaries_advisory=self.config.buckets_connection_create_time,
134134
)
@@ -155,7 +155,7 @@ def _init_connection_advanced_metrics(self) -> None:
155155

156156
self.connection_wait_time = self.meter.create_histogram(
157157
name="db.client.connection.wait_time",
158-
unit="{seconds}",
158+
unit="s",
159159
description="Time to obtain an open connection from the pool",
160160
explicit_bucket_boundaries_advisory=self.config.buckets_connection_wait_time,
161161
)
@@ -170,7 +170,7 @@ def _init_command_metrics(self) -> None:
170170
"""Initialize command execution metric instruments."""
171171
self.operation_duration = self.meter.create_histogram(
172172
name="db.client.operation.duration",
173-
unit="{seconds}",
173+
unit="s",
174174
description="Command execution duration",
175175
explicit_bucket_boundaries_advisory=self.config.buckets_operation_duration,
176176
)
@@ -187,7 +187,7 @@ def _init_streaming_metrics(self) -> None:
187187
"""Initialize Streaming metric instruments."""
188188
self.stream_lag = self.meter.create_histogram(
189189
name="redis.client.stream.lag",
190-
unit="{seconds}",
190+
unit="s",
191191
description="End-to-end lag per message, showing how stale are the messages when the application starts processing them.",
192192
explicit_bucket_boundaries_advisory=self.config.buckets_stream_processing_duration,
193193
)
@@ -208,7 +208,7 @@ def _init_csc_metrics(self) -> None:
208208

209209
self.csc_network_saved = self.meter.create_counter(
210210
name="redis.client.csc.network_saved",
211-
unit="{bytes}",
211+
unit="By",
212212
description="The total number of bytes saved by using CSC",
213213
)
214214

@@ -314,7 +314,7 @@ def init_connection_count(
314314

315315
self.connection_count = self.meter.create_observable_gauge(
316316
name="db.client.connection.count",
317-
unit="connections",
317+
unit="{connection}",
318318
description="Number of connections in the pool",
319319
callbacks=[callback],
320320
)

0 commit comments

Comments
 (0)