diff --git a/CHANGELOG.md b/CHANGELOG.md index 92a3f3d08c..519d321e67 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -64,6 +64,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ([#4068](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/4068)) - `opentelemetry-instrumentation-mysqlclient`: Replace SpanAttributes with semconv constants ([#4067](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/4067)) +- `opentelemetry-instrumentation-pymemcache`: Remove span attributes pymemcache + ([#4076](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/4076)) - `opentelemetry-instrumentation-pymongo`: Replace SpanAttributes with semconv constants ([#4077](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/4077)) - `opentelemetry-instrumentation-pymysql`: Replace SpanAttributes with semconv constants diff --git a/instrumentation/opentelemetry-instrumentation-pymemcache/src/opentelemetry/instrumentation/pymemcache/__init__.py b/instrumentation/opentelemetry-instrumentation-pymemcache/src/opentelemetry/instrumentation/pymemcache/__init__.py index d763734aca..adb70c6a9a 100644 --- a/instrumentation/opentelemetry-instrumentation-pymemcache/src/opentelemetry/instrumentation/pymemcache/__init__.py +++ b/instrumentation/opentelemetry-instrumentation-pymemcache/src/opentelemetry/instrumentation/pymemcache/__init__.py @@ -47,8 +47,17 @@ from opentelemetry.instrumentation.pymemcache.package import _instruments from opentelemetry.instrumentation.pymemcache.version import __version__ from opentelemetry.instrumentation.utils import unwrap -from opentelemetry.semconv.trace import NetTransportValues, SpanAttributes +from opentelemetry.semconv.trace import NetTransportValues from opentelemetry.trace import SpanKind, get_tracer +from opentelemetry.semconv._incubating.attributes.db_attributes import ( + DB_STATEMENT, + DB_SYSTEM, +) +from opentelemetry.semconv._incubating.attributes.net_attributes import ( + NET_PEER_NAME, + NET_PEER_PORT, + NET_TRANSPORT, +) logger = logging.getLogger(__name__) @@ -115,7 +124,7 @@ def _wrap_cmd(tracer, cmd, wrapped, instance, args, kwargs): vals = _get_query_string(args[0]) query = f"{cmd}{' ' if vals else ''}{vals}" - span.set_attribute(SpanAttributes.DB_STATEMENT, query) + span.set_attribute(DB_STATEMENT, query) _set_connection_attributes(span, instance) except Exception as ex: # pylint: disable=broad-except @@ -153,21 +162,21 @@ def _get_query_string(arg): def _get_address_attributes(instance): """Attempt to get host and port from Client instance.""" address_attributes = {} - address_attributes[SpanAttributes.DB_SYSTEM] = "memcached" + address_attributes[DB_SYSTEM] = "memcached" # client.base.Client contains server attribute which is either a host/port tuple, or unix socket path string # https://github.com/pinterest/pymemcache/blob/f02ddf73a28c09256589b8afbb3ee50f1171cac7/pymemcache/client/base.py#L228 if hasattr(instance, "server"): if isinstance(instance.server, tuple): host, port = instance.server - address_attributes[SpanAttributes.NET_PEER_NAME] = host - address_attributes[SpanAttributes.NET_PEER_PORT] = port - address_attributes[SpanAttributes.NET_TRANSPORT] = ( + address_attributes[NET_PEER_NAME] = host + address_attributes[NET_PEER_PORT] = port + address_attributes[NET_TRANSPORT] = ( NetTransportValues.IP_TCP.value ) elif isinstance(instance.server, str): - address_attributes[SpanAttributes.NET_PEER_NAME] = instance.server - address_attributes[SpanAttributes.NET_TRANSPORT] = ( + address_attributes[NET_PEER_NAME] = instance.server + address_attributes[NET_TRANSPORT] = ( NetTransportValues.OTHER.value ) @@ -176,10 +185,10 @@ def _get_address_attributes(instance): class PymemcacheInstrumentor(BaseInstrumentor): """An instrumentor for pymemcache See `BaseInstrumentor`""" - + def instrumentation_dependencies(self) -> Collection[str]: return _instruments - + def _instrument(self, **kwargs): tracer_provider = kwargs.get("tracer_provider") tracer = get_tracer( @@ -195,7 +204,7 @@ def _instrument(self, **kwargs): f"Client.{cmd}", _wrap_cmd(tracer, cmd), ) - + def _uninstrument(self, **kwargs): for command in COMMANDS: unwrap(pymemcache.client.base.Client, f"{command}") diff --git a/instrumentation/opentelemetry-instrumentation-pymemcache/tests/test_pymemcache.py b/instrumentation/opentelemetry-instrumentation-pymemcache/tests/test_pymemcache.py index b9ad948819..7ce6e42a09 100644 --- a/instrumentation/opentelemetry-instrumentation-pymemcache/tests/test_pymemcache.py +++ b/instrumentation/opentelemetry-instrumentation-pymemcache/tests/test_pymemcache.py @@ -11,6 +11,7 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. + from unittest import mock import pymemcache @@ -26,12 +27,19 @@ from opentelemetry import trace as trace_api from opentelemetry.instrumentation.pymemcache import PymemcacheInstrumentor -from opentelemetry.semconv.trace import SpanAttributes from opentelemetry.test.test_base import TestBase from opentelemetry.trace import get_tracer from .utils import MockSocket, _str +from opentelemetry.semconv._incubating.attributes.db_attributes import ( + DB_STATEMENT, + DB_SYSTEM, +) +from opentelemetry.semconv._incubating.attributes.net_attributes import ( + NET_PEER_NAME, + NET_PEER_PORT, +) TEST_HOST = "localhost" TEST_PORT = 117711 @@ -83,16 +91,16 @@ def check_spans(self, spans, num_expected, queries_expected): self.assertEqual(span.name, command) self.assertIs(span.kind, trace_api.SpanKind.CLIENT) self.assertEqual( - span.attributes[SpanAttributes.NET_PEER_NAME], TEST_HOST + span.attributes[NET_PEER_NAME], TEST_HOST ) self.assertEqual( - span.attributes[SpanAttributes.NET_PEER_PORT], TEST_PORT + span.attributes[NET_PEER_PORT], TEST_PORT ) self.assertEqual( - span.attributes[SpanAttributes.DB_SYSTEM], "memcached" + span.attributes[DB_SYSTEM], "memcached" ) self.assertEqual( - span.attributes[SpanAttributes.DB_STATEMENT], query + span.attributes[DB_STATEMENT], query ) def test_set_success(self): @@ -249,10 +257,10 @@ def test_set_get(self): self.assertEqual(len(spans), 2) self.assertEqual( - spans[0].attributes[SpanAttributes.NET_PEER_NAME], TEST_HOST + spans[0].attributes[NET_PEER_NAME], TEST_HOST ) self.assertEqual( - spans[0].attributes[SpanAttributes.NET_PEER_PORT], TEST_PORT + spans[0].attributes[NET_PEER_PORT], TEST_PORT ) def test_append_stored(self): @@ -577,16 +585,16 @@ def check_spans(self, spans, num_expected, queries_expected): self.assertEqual(span.name, command) self.assertIs(span.kind, trace_api.SpanKind.CLIENT) self.assertEqual( - span.attributes[SpanAttributes.NET_PEER_NAME], TEST_HOST + span.attributes[NET_PEER_NAME], TEST_HOST ) self.assertEqual( - span.attributes[SpanAttributes.NET_PEER_PORT], TEST_PORT + span.attributes[NET_PEER_PORT], TEST_PORT ) self.assertEqual( - span.attributes[SpanAttributes.DB_SYSTEM], "memcached" + span.attributes[DB_SYSTEM], "memcached" ) self.assertEqual( - span.attributes[SpanAttributes.DB_STATEMENT], query + span.attributes[DB_STATEMENT], query ) def test_delete_many_found(self):