Skip to content

Commit eb1b0b3

Browse files
semantic convention 1.20.0 verification - database instrumentation - redis (open-telemetry#1627)
1 parent e8700fc commit eb1b0b3

File tree

2 files changed

+67
-9
lines changed
  • instrumentation/opentelemetry-instrumentation-redis

2 files changed

+67
-9
lines changed

instrumentation/opentelemetry-instrumentation-redis/src/opentelemetry/instrumentation/redis/util.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,15 @@ def _extract_conn_attributes(conn_kwargs):
3030
}
3131
db = conn_kwargs.get("db", 0)
3232
attributes[SpanAttributes.DB_REDIS_DATABASE_INDEX] = db
33-
try:
34-
attributes[SpanAttributes.NET_PEER_NAME] = conn_kwargs.get(
35-
"host", "localhost"
36-
)
37-
attributes[SpanAttributes.NET_PEER_PORT] = conn_kwargs.get(
38-
"port", 6379
39-
)
33+
34+
if "host" in conn_kwargs:
35+
attributes[SpanAttributes.NET_PEER_NAME] = conn_kwargs["host"]
36+
attributes[SpanAttributes.NET_PEER_PORT] = conn_kwargs.get("port")
4037
attributes[
4138
SpanAttributes.NET_TRANSPORT
4239
] = NetTransportValues.IP_TCP.value
43-
except KeyError:
44-
attributes[SpanAttributes.NET_PEER_NAME] = conn_kwargs.get("path", "")
40+
elif "path" in conn_kwargs:
41+
attributes[SpanAttributes.NET_SOCK_PEER_ADDR] = conn_kwargs["path"]
4542
attributes[
4643
SpanAttributes.NET_SOCK_FAMILY
4744
] = NetSockFamilyValues.UNIX.value

instrumentation/opentelemetry-instrumentation-redis/tests/test_redis.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,3 +243,64 @@ def test_no_op_tracer_provider(self):
243243

244244
spans = self.memory_exporter.get_finished_spans()
245245
self.assertEqual(len(spans), 0)
246+
247+
def test_db_semantic_attributes_server(self):
248+
redis_client = redis.Redis(host="test.db.semantic.conv", port=12345)
249+
connection = redis.connection.Connection()
250+
redis_client.connection = connection
251+
252+
with mock.patch.object(redis_client, "connection"):
253+
redis_client.set("key", "value")
254+
255+
spans = self.memory_exporter.get_finished_spans()
256+
self.assertEqual(len(spans), 1)
257+
258+
# semconv 1.20.0
259+
span = spans[0]
260+
self.assertEqual(span.name, "SET")
261+
self.assertEqual(span.attributes.get("db.system"), "redis")
262+
self.assertEqual(
263+
span.attributes.get("net.peer.name"), "test.db.semantic.conv"
264+
)
265+
self.assertEqual(span.attributes.get("net.peer.port"), 12345)
266+
self.assertEqual(span.attributes.get("net.transport"), "ip_tcp")
267+
self.assertEqual(span.attributes.get("db.statement"), "SET ? ?")
268+
self.assertEqual(span.attributes.get("db.redis.database_index"), 0)
269+
270+
self.assertNotIn("net.sock.family", span.attributes)
271+
self.assertNotIn("net.sock.peer.addr", span.attributes)
272+
self.assertNotIn("db.connection_string", span.attributes)
273+
self.assertNotIn("db.user", span.attributes)
274+
self.assertNotIn("db.name", span.attributes)
275+
self.assertNotIn("db.operation", span.attributes)
276+
277+
def test_db_semantic_attributes_socket(self):
278+
redis_client = redis.Redis(unix_socket_path="/tmp/semantic.conv.sock")
279+
connection = redis.connection.Connection()
280+
redis_client.connection = connection
281+
282+
with mock.patch.object(redis_client, "connection"):
283+
redis_client.set("key", "value")
284+
285+
spans = self.memory_exporter.get_finished_spans()
286+
self.assertEqual(len(spans), 1)
287+
288+
# semconv 1.20.0
289+
span = spans[0]
290+
self.assertEqual(span.name, "SET")
291+
self.assertEqual(span.attributes.get("db.system"), "redis")
292+
self.assertEqual(
293+
span.attributes.get("net.sock.peer.addr"),
294+
"/tmp/semantic.conv.sock",
295+
)
296+
self.assertEqual(span.attributes.get("net.sock.family"), "unix")
297+
self.assertEqual(span.attributes.get("db.statement"), "SET ? ?")
298+
self.assertEqual(span.attributes.get("db.redis.database_index"), 0)
299+
300+
self.assertNotIn("net.transport", span.attributes)
301+
self.assertNotIn("net.peer.name", span.attributes)
302+
self.assertNotIn("net.peer.port", span.attributes)
303+
self.assertNotIn("db.connection_string", span.attributes)
304+
self.assertNotIn("db.user", span.attributes)
305+
self.assertNotIn("db.name", span.attributes)
306+
self.assertNotIn("db.operation", span.attributes)

0 commit comments

Comments
 (0)