-
Notifications
You must be signed in to change notification settings - Fork 620
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tests on
_extract_conn_attributes
- Loading branch information
Showing
1 changed file
with
71 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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,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, | ||
) |