Skip to content

Commit c425e96

Browse files
authored
Merge branch 'main' into logging-silence-out-of-spec-attributes
2 parents 4fc8c2d + 533ece6 commit c425e96

5 files changed

Lines changed: 266 additions & 43 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
3737
([#4051](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/4051))
3838
- `opentelemetry-instrumentation-aiohttp-server` Implement new semantic convention opt-in migration
3939
([#3980](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3980))
40+
- `opentelemetry-instrumentation`: add database stability attribute setters in `_semconv` utilities
41+
([#4108](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/4108))
4042

4143
### Fixed
4244

@@ -66,6 +68,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
6668
([#4068](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/4068))
6769
- `opentelemetry-instrumentation-mysqlclient`: Replace SpanAttributes with semconv constants
6870
([#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))
6973
- `opentelemetry-instrumentation-pymongo`: Replace SpanAttributes with semconv constants
7074
([#4077](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/4077))
7175
- `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"])

opentelemetry-instrumentation/src/opentelemetry/instrumentation/_semconv.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@
2020
from typing import Container, Mapping, MutableMapping
2121

2222
from opentelemetry.instrumentation.utils import http_status_to_status_code
23+
from opentelemetry.semconv._incubating.attributes.db_attributes import (
24+
DB_NAME,
25+
DB_STATEMENT,
26+
DB_SYSTEM,
27+
DB_USER,
28+
)
2329
from opentelemetry.semconv._incubating.attributes.http_attributes import (
2430
HTTP_FLAVOR,
2531
HTTP_HOST,
@@ -42,6 +48,11 @@
4248
CLIENT_ADDRESS,
4349
CLIENT_PORT,
4450
)
51+
from opentelemetry.semconv.attributes.db_attributes import (
52+
DB_NAMESPACE,
53+
DB_QUERY_TEXT,
54+
DB_SYSTEM_NAME,
55+
)
4556
from opentelemetry.semconv.attributes.error_attributes import ERROR_TYPE
4657
from opentelemetry.semconv.attributes.http_attributes import (
4758
HTTP_REQUEST_METHOD,
@@ -533,6 +544,55 @@ def _set_http_net_peer_name_server(
533544
set_string_attribute(result, CLIENT_ADDRESS, name)
534545

535546

547+
# Database
548+
549+
550+
def _set_db_system(
551+
result: MutableMapping[str, AttributeValue],
552+
system: str,
553+
sem_conv_opt_in_mode: _StabilityMode,
554+
) -> None:
555+
if _report_old(sem_conv_opt_in_mode):
556+
set_string_attribute(result, DB_SYSTEM, system)
557+
if _report_new(sem_conv_opt_in_mode):
558+
set_string_attribute(result, DB_SYSTEM_NAME, system)
559+
560+
561+
def _set_db_name(
562+
result: MutableMapping[str, AttributeValue],
563+
name: str,
564+
sem_conv_opt_in_mode: _StabilityMode,
565+
) -> None:
566+
if _report_old(sem_conv_opt_in_mode):
567+
set_string_attribute(result, DB_NAME, name)
568+
if _report_new(sem_conv_opt_in_mode):
569+
set_string_attribute(result, DB_NAMESPACE, name)
570+
571+
572+
def _set_db_statement(
573+
result: MutableMapping[str, AttributeValue],
574+
statement: str,
575+
sem_conv_opt_in_mode: _StabilityMode,
576+
) -> None:
577+
if _report_old(sem_conv_opt_in_mode):
578+
set_string_attribute(result, DB_STATEMENT, statement)
579+
if _report_new(sem_conv_opt_in_mode):
580+
set_string_attribute(result, DB_QUERY_TEXT, statement)
581+
582+
583+
def _set_db_user(
584+
result: MutableMapping[str, AttributeValue],
585+
user: str,
586+
sem_conv_opt_in_mode: _StabilityMode,
587+
) -> None:
588+
if _report_old(sem_conv_opt_in_mode):
589+
set_string_attribute(result, DB_USER, user)
590+
# No new attribute - db.user was removed with no replacement
591+
592+
593+
# General
594+
595+
536596
def _set_status(
537597
span: Span,
538598
metrics_attributes: MutableMapping[str, AttributeValue],

0 commit comments

Comments
 (0)