anon-iclr-submission/benchname-bug-localization huggingface dataset:
Repository: open-telemetry/opentelemetry-python-contrib
Issue: Add database_index attribute for redis instrumentation when db is not 0

Prior discussion [here](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/184#discussion_r528913282).

The spec says for redis if db is not 0 it should have `db.redis.database_index` set to the index of the database being accessed as used in the SELECT command, provided as an integer.

Changed files:
tests/opentelemetry-docker-tests/tests/redis/test_redis_functional.py
instrumentation/opentelemetry-instrumentation-redis/src/opentelemetry/instrumentation/redis/util.py

diff --git a/instrumentation/opentelemetry-instrumentation-redis/src/opentelemetry/instrumentation/redis/util.py b/instrumentation/opentelemetry-instrumentation-redis/src/opentelemetry/instrumentation/redis/util.py
index f2ac71a3..8898dab9 100644
--- a/instrumentation/opentelemetry-instrumentation-redis/src/opentelemetry/instrumentation/redis/util.py
+++ b/instrumentation/opentelemetry-instrumentation-redis/src/opentelemetry/instrumentation/redis/util.py
@@ -21,8 +21,10 @@ def _extract_conn_attributes(conn_kwargs):
     """ Transform redis conn info into dict """
     attributes = {
         "db.system": "redis",
-        "db.name": conn_kwargs.get("db", 0),
     }
+    db = conn_kwargs.get("db", 0)
+    attributes["db.name"] = db
+    attributes["db.redis.database_index"] = db
     try:
         attributes["net.peer.name"] = conn_kwargs["host"]
         attributes["net.peer.ip"] = conn_kwargs["port"]
diff --git a/tests/opentelemetry-docker-tests/tests/redis/test_redis_functional.py b/tests/opentelemetry-docker-tests/tests/redis/test_redis_functional.py
index b893a33b..ef9ea90f 100644
--- a/tests/opentelemetry-docker-tests/tests/redis/test_redis_functional.py
+++ b/tests/opentelemetry-docker-tests/tests/redis/test_redis_functional.py
@@ -115,3 +115,30 @@ class TestRedisInstrument(TestBase):
             child_span.attributes.get("service"), self.test_service
         )
         self.assertEqual(child_span.name, "GET")
+
+
+class TestRedisDBIndexInstrument(TestBase):
+    def setUp(self):
+        super().setUp()
+        self.redis_client = redis.Redis(port=6379, db=10)
+        self.redis_client.flushall()
+        RedisInstrumentor().instrument(tracer_provider=self.tracer_provider)
+
+    def tearDown(self):
+        super().tearDown()
+        RedisInstrumentor().uninstrument()
+
+    def _check_span(self, span, name):
+        self.assertEqual(span.name, name)
+        self.assertIs(span.status.status_code, trace.status.StatusCode.UNSET)
+        self.assertEqual(span.attributes["net.peer.name"], "localhost")
+        self.assertEqual(span.attributes["net.peer.ip"], 6379)
+        self.assertEqual(span.attributes["db.redis.database_index"], 10)
+
+    def test_get(self):
+        self.assertIsNone(self.redis_client.get("foo"))
+        spans = self.memory_exporter.get_finished_spans()
+        self.assertEqual(len(spans), 1)
+        span = spans[0]
+        self._check_span(span, "GET")
+        self.assertEqual(span.attributes.get("db.statement"), "GET foo")