Skip to content

Commit

Permalink
Add tests on _extract_conn_attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
shadchin committed May 9, 2024
1 parent be2c578 commit 9ac607c
Showing 1 changed file with 71 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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:[email protected]: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,
)

0 comments on commit 9ac607c

Please sign in to comment.