Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,27 @@ def _extract_conn_attributes(conn_kwargs):
}
db = conn_kwargs.get("db", 0)
attributes[SpanAttributes.DB_REDIS_DATABASE_INDEX] = db
try:

if conn_kwargs.get("username"):
attributes[SpanAttributes.DB_USER] = conn_kwargs["username"]

if conn_kwargs.get("client_name"):
# TODO: add to semconv?
attributes["db.redis.client_name"] = conn_kwargs["client_name"]
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't in the semconv, but would be useful for debugging and can be different from username.


if conn_kwargs.get("path"):
attributes[SpanAttributes.NET_SOCK_PEER_ADDR] = conn_kwargs["path"]
attributes[
SpanAttributes.NET_SOCK_FAMILY
] = NetSockFamilyValues.UNIX.value
else:
attributes[SpanAttributes.NET_PEER_NAME] = conn_kwargs.get(
"host", "localhost"
)
attributes[SpanAttributes.NET_PEER_PORT] = conn_kwargs.get(
"port", 6379
"host", "TRACE_MISSING"
Copy link
Author

@cBiscuitSurprise cBiscuitSurprise Sep 2, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a sentinel defined for a missing value when one is expected? None may be sufficient, but I wasn't sure.

Defaulting to localhost and 6379 might obfuscate an issue.

)
attributes[SpanAttributes.NET_PEER_PORT] = conn_kwargs.get("port", 0)
attributes[
SpanAttributes.NET_TRANSPORT
] = NetTransportValues.IP_TCP.value
except KeyError:
attributes[SpanAttributes.NET_PEER_NAME] = conn_kwargs.get("path", "")
attributes[
SpanAttributes.NET_SOCK_FAMILY
] = NetSockFamilyValues.UNIX.value

return attributes

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,3 +243,58 @@ 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,
client_name="buggy_client",
)
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)
Copy link
Author

@cBiscuitSurprise cBiscuitSurprise Sep 2, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do I also need to check the negative (e.g. assertNotIn("net.sock.family", span.attributes))?


def test_db_semantic_attributes_socket(self):
redis_client = redis.Redis(
unix_socket_path="/tmp/semantic.conv.sock",
db=2,
username="redis-user",
)
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"), 2)
self.assertEqual(span.attributes.get("db.user"), "redis-user")