diff --git a/instrumentation/opentelemetry-instrumentation-redis/tests/test_redis.py b/instrumentation/opentelemetry-instrumentation-redis/tests/test_redis.py index 2d2670fee3..f4ee8d5299 100644 --- a/instrumentation/opentelemetry-instrumentation-redis/tests/test_redis.py +++ b/instrumentation/opentelemetry-instrumentation-redis/tests/test_redis.py @@ -20,6 +20,11 @@ from opentelemetry import trace from opentelemetry.instrumentation.redis import RedisInstrumentor +from opentelemetry.semconv.trace import ( + DbSystemValues, + NetTransportValues, + SpanAttributes, +) from opentelemetry.test.test_base import TestBase from opentelemetry.trace import SpanKind @@ -226,3 +231,69 @@ def test_no_op_tracer_provider(self): spans = self.memory_exporter.get_finished_spans() self.assertEqual(len(spans), 0) + + def test_attributes_default(self): + redis_client = redis.Redis() + + 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[SpanAttributes.DB_SYSTEM], DbSystemValues.REDIS.value + ) + self.assertEqual(span.attributes[SpanAttributes.DB_REDIS_DATABASE_INDEX], 0) + self.assertEqual(span.attributes[SpanAttributes.NET_PEER_NAME], "localhost") + self.assertEqual(span.attributes[SpanAttributes.NET_PEER_PORT], 6379) + self.assertEqual( + span.attributes[SpanAttributes.NET_TRANSPORT], + NetTransportValues.IP_TCP.value, + ) + + def test_attributes_tcp(self): + redis_client = redis.Redis.from_url("redis://foo:bar@1.1.1.1:6380/1") + + 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[SpanAttributes.DB_SYSTEM], DbSystemValues.REDIS.value + ) + self.assertEqual(span.attributes[SpanAttributes.DB_REDIS_DATABASE_INDEX], 1) + self.assertEqual(span.attributes[SpanAttributes.NET_PEER_NAME], "1.1.1.1") + self.assertEqual(span.attributes[SpanAttributes.NET_PEER_PORT], 6380) + self.assertEqual( + span.attributes[SpanAttributes.NET_TRANSPORT], + NetTransportValues.IP_TCP.value, + ) + + def test_attributes_unix_socket(self): + redis_client = redis.Redis.from_url( + "unix://foo@/path/to/socket.sock?db=3&password=bar" + ) + + 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[SpanAttributes.DB_SYSTEM], DbSystemValues.REDIS.value + ) + self.assertEqual(span.attributes[SpanAttributes.DB_REDIS_DATABASE_INDEX], 3) + self.assertEqual( + span.attributes[SpanAttributes.NET_PEER_NAME], "/path/to/socket.sock" + ) + self.assertEqual( + span.attributes[SpanAttributes.NET_TRANSPORT], + NetTransportValues.OTHER.value, + )