Skip to content

Commit

Permalink
semantic convention 1.20.0 verification - database instrumentation - …
Browse files Browse the repository at this point in the history
  • Loading branch information
cBiscuitSurprise committed Sep 1, 2023
1 parent e8700fc commit eb1b0b3
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,15 @@ def _extract_conn_attributes(conn_kwargs):
}
db = conn_kwargs.get("db", 0)
attributes[SpanAttributes.DB_REDIS_DATABASE_INDEX] = db
try:
attributes[SpanAttributes.NET_PEER_NAME] = conn_kwargs.get(
"host", "localhost"
)
attributes[SpanAttributes.NET_PEER_PORT] = conn_kwargs.get(
"port", 6379
)

if "host" in conn_kwargs:
attributes[SpanAttributes.NET_PEER_NAME] = conn_kwargs["host"]
attributes[SpanAttributes.NET_PEER_PORT] = conn_kwargs.get("port")
attributes[
SpanAttributes.NET_TRANSPORT
] = NetTransportValues.IP_TCP.value
except KeyError:
attributes[SpanAttributes.NET_PEER_NAME] = conn_kwargs.get("path", "")
elif "path" in conn_kwargs:
attributes[SpanAttributes.NET_SOCK_PEER_ADDR] = conn_kwargs["path"]
attributes[
SpanAttributes.NET_SOCK_FAMILY
] = NetSockFamilyValues.UNIX.value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,3 +243,64 @@ def test_no_op_tracer_provider(self):

spans = self.memory_exporter.get_finished_spans()
self.assertEqual(len(spans), 0)

def test_db_semantic_attributes_server(self):
redis_client = redis.Redis(host="test.db.semantic.conv", port=12345)
connection = redis.connection.Connection()
redis_client.connection = connection

with mock.patch.object(redis_client, "connection"):
redis_client.set("key", "value")

spans = self.memory_exporter.get_finished_spans()
self.assertEqual(len(spans), 1)

# semconv 1.20.0
span = spans[0]
self.assertEqual(span.name, "SET")
self.assertEqual(span.attributes.get("db.system"), "redis")
self.assertEqual(
span.attributes.get("net.peer.name"), "test.db.semantic.conv"
)
self.assertEqual(span.attributes.get("net.peer.port"), 12345)
self.assertEqual(span.attributes.get("net.transport"), "ip_tcp")
self.assertEqual(span.attributes.get("db.statement"), "SET ? ?")
self.assertEqual(span.attributes.get("db.redis.database_index"), 0)

self.assertNotIn("net.sock.family", span.attributes)
self.assertNotIn("net.sock.peer.addr", span.attributes)
self.assertNotIn("db.connection_string", span.attributes)
self.assertNotIn("db.user", span.attributes)
self.assertNotIn("db.name", span.attributes)
self.assertNotIn("db.operation", span.attributes)

def test_db_semantic_attributes_socket(self):
redis_client = redis.Redis(unix_socket_path="/tmp/semantic.conv.sock")
connection = redis.connection.Connection()
redis_client.connection = connection

with mock.patch.object(redis_client, "connection"):
redis_client.set("key", "value")

spans = self.memory_exporter.get_finished_spans()
self.assertEqual(len(spans), 1)

# semconv 1.20.0
span = spans[0]
self.assertEqual(span.name, "SET")
self.assertEqual(span.attributes.get("db.system"), "redis")
self.assertEqual(
span.attributes.get("net.sock.peer.addr"),
"/tmp/semantic.conv.sock",
)
self.assertEqual(span.attributes.get("net.sock.family"), "unix")
self.assertEqual(span.attributes.get("db.statement"), "SET ? ?")
self.assertEqual(span.attributes.get("db.redis.database_index"), 0)

self.assertNotIn("net.transport", span.attributes)
self.assertNotIn("net.peer.name", span.attributes)
self.assertNotIn("net.peer.port", span.attributes)
self.assertNotIn("db.connection_string", span.attributes)
self.assertNotIn("db.user", span.attributes)
self.assertNotIn("db.name", span.attributes)
self.assertNotIn("db.operation", span.attributes)

0 comments on commit eb1b0b3

Please sign in to comment.