Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .changelog/4828.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
`opentelemetry-instrumentation-redis`: treat explicit `db=None` as database index 0
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,11 @@ def _extract_conn_attributes(
attributes, DbSystemValues.REDIS.value, db_sem_conv_opt_in_mode
)

# redis-py may pass db=None explicitly (e.g. from_url); treat that as 0.
# dict.get("db", 0) only defaults when the key is missing, not when it is None.
db = conn_kwargs.get("db", 0)
if db is None:
db = 0
_set_db_redis_database_index(attributes, db, db_sem_conv_opt_in_mode)
if "path" in conn_kwargs:
_set_http_net_peer_name_client(
Expand Down Expand Up @@ -144,6 +148,8 @@ def _build_span_name(
name = cmd_args[0]
else:
name = instance.connection_pool.connection_kwargs.get("db", 0)
if name is None:
name = 0
return name


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,24 @@ def test_attributes_default(self):
NetTransportValues.IP_TCP.value,
)

def test_attributes_db_none(self):
"""db=None in connection kwargs should default to index 0 (issue #1905)."""
redis_client = redis.Redis()
redis_client.connection_pool.connection_kwargs["db"] = None

with mock.patch.object(redis_client, "connection"):
redis_client.set("key", "value")

spans = self.memory_exporter.get_finished_spans()
self.assertEqual(len(spans), 1)

span = spans[0]
self.assertEqual(span.attributes[DB_REDIS_DATABASE_INDEX], 0)
self.assertEqual(
span.attributes[DB_SYSTEM],
DbSystemValues.REDIS.value,
)

def test_attributes_tcp(self):
redis_client = redis.Redis.from_url("redis://foo:bar@1.1.1.1:6380/1")

Expand Down