Skip to content

Commit 533ece6

Browse files
Remove span attributes pymemcache (#4076)
* opentelemetry-instrumentation-pymemcache: Remove usage of deprecated SpanAttributes * Fixed linting issues * Incorporated Review Comments for usage of NET_TRANSPORT * Added back the empty lines * Apply suggestions from code review * Update __init__.py * Update __init__.py * Update test_pymemcache.py * Executed tox --------- Co-authored-by: Riccardo Magliocchetti <riccardo.magliocchetti@gmail.com>
1 parent b31b80e commit 533ece6

File tree

3 files changed

+38
-43
lines changed

3 files changed

+38
-43
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
6868
([#4068](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/4068))
6969
- `opentelemetry-instrumentation-mysqlclient`: Replace SpanAttributes with semconv constants
7070
([#4067](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/4067))
71+
- `opentelemetry-instrumentation-pymemcache`: Remove span attributes pymemcache
72+
([#4076](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/4076))
7173
- `opentelemetry-instrumentation-pymongo`: Replace SpanAttributes with semconv constants
7274
([#4077](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/4077))
7375
- `opentelemetry-instrumentation-pymysql`: Replace SpanAttributes with semconv constants

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

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,16 @@
4747
from opentelemetry.instrumentation.pymemcache.package import _instruments
4848
from opentelemetry.instrumentation.pymemcache.version import __version__
4949
from opentelemetry.instrumentation.utils import unwrap
50-
from opentelemetry.semconv.trace import NetTransportValues, SpanAttributes
50+
from opentelemetry.semconv._incubating.attributes.db_attributes import (
51+
DB_STATEMENT,
52+
DB_SYSTEM,
53+
)
54+
from opentelemetry.semconv._incubating.attributes.net_attributes import (
55+
NET_PEER_NAME,
56+
NET_PEER_PORT,
57+
NET_TRANSPORT,
58+
NetTransportValues,
59+
)
5160
from opentelemetry.trace import SpanKind, get_tracer
5261

5362
logger = logging.getLogger(__name__)
@@ -115,7 +124,7 @@ def _wrap_cmd(tracer, cmd, wrapped, instance, args, kwargs):
115124
vals = _get_query_string(args[0])
116125

117126
query = f"{cmd}{' ' if vals else ''}{vals}"
118-
span.set_attribute(SpanAttributes.DB_STATEMENT, query)
127+
span.set_attribute(DB_STATEMENT, query)
119128

120129
_set_connection_attributes(span, instance)
121130
except Exception as ex: # pylint: disable=broad-except
@@ -153,23 +162,19 @@ def _get_query_string(arg):
153162
def _get_address_attributes(instance):
154163
"""Attempt to get host and port from Client instance."""
155164
address_attributes = {}
156-
address_attributes[SpanAttributes.DB_SYSTEM] = "memcached"
165+
address_attributes[DB_SYSTEM] = "memcached"
157166

158167
# client.base.Client contains server attribute which is either a host/port tuple, or unix socket path string
159168
# https://github.com/pinterest/pymemcache/blob/f02ddf73a28c09256589b8afbb3ee50f1171cac7/pymemcache/client/base.py#L228
160169
if hasattr(instance, "server"):
161170
if isinstance(instance.server, tuple):
162171
host, port = instance.server
163-
address_attributes[SpanAttributes.NET_PEER_NAME] = host
164-
address_attributes[SpanAttributes.NET_PEER_PORT] = port
165-
address_attributes[SpanAttributes.NET_TRANSPORT] = (
166-
NetTransportValues.IP_TCP.value
167-
)
172+
address_attributes[NET_PEER_NAME] = host
173+
address_attributes[NET_PEER_PORT] = port
174+
address_attributes[NET_TRANSPORT] = NetTransportValues.IP_TCP.value
168175
elif isinstance(instance.server, str):
169-
address_attributes[SpanAttributes.NET_PEER_NAME] = instance.server
170-
address_attributes[SpanAttributes.NET_TRANSPORT] = (
171-
NetTransportValues.OTHER.value
172-
)
176+
address_attributes[NET_PEER_NAME] = instance.server
177+
address_attributes[NET_TRANSPORT] = NetTransportValues.OTHER.value
173178

174179
return address_attributes
175180

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

Lines changed: 19 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
14+
1415
from unittest import mock
1516

1617
import pymemcache
@@ -26,7 +27,14 @@
2627

2728
from opentelemetry import trace as trace_api
2829
from opentelemetry.instrumentation.pymemcache import PymemcacheInstrumentor
29-
from opentelemetry.semconv.trace import SpanAttributes
30+
from opentelemetry.semconv._incubating.attributes.db_attributes import (
31+
DB_STATEMENT,
32+
DB_SYSTEM,
33+
)
34+
from opentelemetry.semconv._incubating.attributes.net_attributes import (
35+
NET_PEER_NAME,
36+
NET_PEER_PORT,
37+
)
3038
from opentelemetry.test.test_base import TestBase
3139
from opentelemetry.trace import get_tracer
3240

@@ -82,18 +90,10 @@ def check_spans(self, spans, num_expected, queries_expected):
8290
command, *_ = query.split(" ")
8391
self.assertEqual(span.name, command)
8492
self.assertIs(span.kind, trace_api.SpanKind.CLIENT)
85-
self.assertEqual(
86-
span.attributes[SpanAttributes.NET_PEER_NAME], TEST_HOST
87-
)
88-
self.assertEqual(
89-
span.attributes[SpanAttributes.NET_PEER_PORT], TEST_PORT
90-
)
91-
self.assertEqual(
92-
span.attributes[SpanAttributes.DB_SYSTEM], "memcached"
93-
)
94-
self.assertEqual(
95-
span.attributes[SpanAttributes.DB_STATEMENT], query
96-
)
93+
self.assertEqual(span.attributes[NET_PEER_NAME], TEST_HOST)
94+
self.assertEqual(span.attributes[NET_PEER_PORT], TEST_PORT)
95+
self.assertEqual(span.attributes[DB_SYSTEM], "memcached")
96+
self.assertEqual(span.attributes[DB_STATEMENT], query)
9797

9898
def test_set_success(self):
9999
client = self.make_client([b"STORED\r\n"])
@@ -248,12 +248,8 @@ def test_set_get(self):
248248
spans = self.memory_exporter.get_finished_spans()
249249

250250
self.assertEqual(len(spans), 2)
251-
self.assertEqual(
252-
spans[0].attributes[SpanAttributes.NET_PEER_NAME], TEST_HOST
253-
)
254-
self.assertEqual(
255-
spans[0].attributes[SpanAttributes.NET_PEER_PORT], TEST_PORT
256-
)
251+
self.assertEqual(spans[0].attributes[NET_PEER_NAME], TEST_HOST)
252+
self.assertEqual(spans[0].attributes[NET_PEER_PORT], TEST_PORT)
257253

258254
def test_append_stored(self):
259255
client = self.make_client([b"STORED\r\n"])
@@ -576,18 +572,10 @@ def check_spans(self, spans, num_expected, queries_expected):
576572
command, *_ = query.split(" ")
577573
self.assertEqual(span.name, command)
578574
self.assertIs(span.kind, trace_api.SpanKind.CLIENT)
579-
self.assertEqual(
580-
span.attributes[SpanAttributes.NET_PEER_NAME], TEST_HOST
581-
)
582-
self.assertEqual(
583-
span.attributes[SpanAttributes.NET_PEER_PORT], TEST_PORT
584-
)
585-
self.assertEqual(
586-
span.attributes[SpanAttributes.DB_SYSTEM], "memcached"
587-
)
588-
self.assertEqual(
589-
span.attributes[SpanAttributes.DB_STATEMENT], query
590-
)
575+
self.assertEqual(span.attributes[NET_PEER_NAME], TEST_HOST)
576+
self.assertEqual(span.attributes[NET_PEER_PORT], TEST_PORT)
577+
self.assertEqual(span.attributes[DB_SYSTEM], "memcached")
578+
self.assertEqual(span.attributes[DB_STATEMENT], query)
591579

592580
def test_delete_many_found(self):
593581
client = self.make_client([b"STORED\r", b"\n", b"DELETED\r\n"])

0 commit comments

Comments
 (0)