Skip to content
Merged
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,16 @@
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._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,
NetTransportValues,
)
from opentelemetry.trace import SpanKind, get_tracer

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -153,23 +162,19 @@ 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] = (
NetTransportValues.IP_TCP.value
)
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] = (
NetTransportValues.OTHER.value
)
address_attributes[NET_PEER_NAME] = instance.server
address_attributes[NET_TRANSPORT] = NetTransportValues.OTHER.value

return address_attributes

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -26,7 +27,14 @@

from opentelemetry import trace as trace_api
from opentelemetry.instrumentation.pymemcache import PymemcacheInstrumentor
from opentelemetry.semconv.trace import SpanAttributes
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,
)
from opentelemetry.test.test_base import TestBase
from opentelemetry.trace import get_tracer

Expand Down Expand Up @@ -82,18 +90,10 @@ def check_spans(self, spans, num_expected, queries_expected):
command, *_ = query.split(" ")
self.assertEqual(span.name, command)
self.assertIs(span.kind, trace_api.SpanKind.CLIENT)
self.assertEqual(
span.attributes[SpanAttributes.NET_PEER_NAME], TEST_HOST
)
self.assertEqual(
span.attributes[SpanAttributes.NET_PEER_PORT], TEST_PORT
)
self.assertEqual(
span.attributes[SpanAttributes.DB_SYSTEM], "memcached"
)
self.assertEqual(
span.attributes[SpanAttributes.DB_STATEMENT], query
)
self.assertEqual(span.attributes[NET_PEER_NAME], TEST_HOST)
self.assertEqual(span.attributes[NET_PEER_PORT], TEST_PORT)
self.assertEqual(span.attributes[DB_SYSTEM], "memcached")
self.assertEqual(span.attributes[DB_STATEMENT], query)

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

self.assertEqual(len(spans), 2)
self.assertEqual(
spans[0].attributes[SpanAttributes.NET_PEER_NAME], TEST_HOST
)
self.assertEqual(
spans[0].attributes[SpanAttributes.NET_PEER_PORT], TEST_PORT
)
self.assertEqual(spans[0].attributes[NET_PEER_NAME], TEST_HOST)
self.assertEqual(spans[0].attributes[NET_PEER_PORT], TEST_PORT)

def test_append_stored(self):
client = self.make_client([b"STORED\r\n"])
Expand Down Expand Up @@ -576,18 +572,10 @@ def check_spans(self, spans, num_expected, queries_expected):
command, *_ = query.split(" ")
self.assertEqual(span.name, command)
self.assertIs(span.kind, trace_api.SpanKind.CLIENT)
self.assertEqual(
span.attributes[SpanAttributes.NET_PEER_NAME], TEST_HOST
)
self.assertEqual(
span.attributes[SpanAttributes.NET_PEER_PORT], TEST_PORT
)
self.assertEqual(
span.attributes[SpanAttributes.DB_SYSTEM], "memcached"
)
self.assertEqual(
span.attributes[SpanAttributes.DB_STATEMENT], query
)
self.assertEqual(span.attributes[NET_PEER_NAME], TEST_HOST)
self.assertEqual(span.attributes[NET_PEER_PORT], TEST_PORT)
self.assertEqual(span.attributes[DB_SYSTEM], "memcached")
self.assertEqual(span.attributes[DB_STATEMENT], query)

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