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 29, 2024
1 parent 6ac715a commit 18a75d4
Showing 1 changed file with 85 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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,83 @@ 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,
)

0 comments on commit 18a75d4

Please sign in to comment.