Skip to content

Commit c841110

Browse files
authored
Merge pull request #183 from lonewolf3739/memcached-semantic-conv
2 parents 7513d7b + 0fc6fa2 commit c841110

File tree

3 files changed

+21
-30
lines changed

3 files changed

+21
-30
lines changed

instrumentation/opentelemetry-instrumentation-pymemcache/CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
## Unreleased
44

5+
- Update pymemcache instrumentation to follow semantic conventions
6+
([#183](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/183))
7+
58
## Version 0.13b0
69

710
Released 2020-09-17

instrumentation/opentelemetry-instrumentation-pymemcache/src/opentelemetry/instrumentation/pymemcache/__init__.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,13 @@
5555
# https://github.com/open-telemetry/opentelemetry-specification/blob/master/specification/trace/semantic_conventions/span-general.md#general-network-connection-attributes
5656
_HOST = "net.peer.name"
5757
_PORT = "net.peer.port"
58+
_TRANSPORT = "net.transport"
5859
# Database semantic conventions here:
5960
# https://github.com/open-telemetry/opentelemetry-specification/blob/master/specification/trace/semantic_conventions/database.md
60-
_DB = "db.type"
61-
_URL = "db.url"
61+
_DB = "db.system"
6262

6363
_DEFAULT_SERVICE = "memcached"
6464
_RAWCMD = "db.statement"
65-
_CMD = "memcached.command"
6665
COMMANDS = [
6766
"set",
6867
"set_many",
@@ -115,7 +114,7 @@ def wrapper(wrapped, instance, args, kwargs):
115114
@_with_tracer_wrapper
116115
def _wrap_cmd(tracer, cmd, wrapped, instance, args, kwargs):
117116
with tracer.start_as_current_span(
118-
_CMD, kind=SpanKind.INTERNAL, attributes={}
117+
cmd, kind=SpanKind.CLIENT, attributes={}
119118
) as span:
120119
try:
121120
if span.is_recording():
@@ -173,9 +172,10 @@ def _get_address_attributes(instance):
173172
host, port = instance.server
174173
address_attributes[_HOST] = host
175174
address_attributes[_PORT] = port
176-
address_attributes[_URL] = "memcached://{}:{}".format(host, port)
175+
address_attributes[_TRANSPORT] = "IP.TCP"
177176
elif isinstance(instance.server, str):
178-
address_attributes[_URL] = "memcached://{}".format(instance.server)
177+
address_attributes[_HOST] = instance.server
178+
address_attributes[_TRANSPORT] = "Unix"
179179

180180
return address_attributes
181181

instrumentation/opentelemetry-instrumentation-pymemcache/tests/test_pymemcache.py

+12-24
Original file line numberDiff line numberDiff line change
@@ -62,17 +62,12 @@ def check_spans(self, spans, num_expected, queries_expected):
6262
self.assertEqual(num_expected, len(spans))
6363

6464
for span, query in zip(spans, queries_expected):
65-
self.assertEqual(span.name, "memcached.command")
66-
self.assertIs(span.kind, trace_api.SpanKind.INTERNAL)
67-
self.assertEqual(
68-
span.attributes["net.peer.name"], "{}".format(TEST_HOST)
69-
)
65+
command, *_ = query.split(" ")
66+
self.assertEqual(span.name, command)
67+
self.assertIs(span.kind, trace_api.SpanKind.CLIENT)
68+
self.assertEqual(span.attributes["net.peer.name"], TEST_HOST)
7069
self.assertEqual(span.attributes["net.peer.port"], TEST_PORT)
71-
self.assertEqual(span.attributes["db.type"], "memcached")
72-
self.assertEqual(
73-
span.attributes["db.url"],
74-
"memcached://{}:{}".format(TEST_HOST, TEST_PORT),
75-
)
70+
self.assertEqual(span.attributes["db.system"], "memcached")
7671
self.assertEqual(span.attributes["db.statement"], query)
7772

7873
def test_set_success(self):
@@ -214,10 +209,8 @@ def test_set_get(self):
214209
spans = self.memory_exporter.get_finished_spans()
215210

216211
self.assertEqual(len(spans), 2)
217-
self.assertEqual(
218-
spans[0].attributes["db.url"],
219-
"memcached://{}:{}".format(TEST_HOST, TEST_PORT),
220-
)
212+
self.assertEqual(spans[0].attributes["net.peer.name"], TEST_HOST)
213+
self.assertEqual(spans[0].attributes["net.peer.port"], TEST_PORT)
221214

222215
def test_append_stored(self):
223216
client = self.make_client([b"STORED\r\n"])
@@ -517,17 +510,12 @@ def check_spans(self, spans, num_expected, queries_expected):
517510
self.assertEqual(num_expected, len(spans))
518511

519512
for span, query in zip(spans, queries_expected):
520-
self.assertEqual(span.name, "memcached.command")
521-
self.assertIs(span.kind, trace_api.SpanKind.INTERNAL)
522-
self.assertEqual(
523-
span.attributes["net.peer.name"], "{}".format(TEST_HOST)
524-
)
513+
command, *_ = query.split(" ")
514+
self.assertEqual(span.name, command)
515+
self.assertIs(span.kind, trace_api.SpanKind.CLIENT)
516+
self.assertEqual(span.attributes["net.peer.name"], TEST_HOST)
525517
self.assertEqual(span.attributes["net.peer.port"], TEST_PORT)
526-
self.assertEqual(span.attributes["db.type"], "memcached")
527-
self.assertEqual(
528-
span.attributes["db.url"],
529-
"memcached://{}:{}".format(TEST_HOST, TEST_PORT),
530-
)
518+
self.assertEqual(span.attributes["db.system"], "memcached")
531519
self.assertEqual(span.attributes["db.statement"], query)
532520

533521
def test_delete_many_found(self):

0 commit comments

Comments
 (0)