From a14eb88d2e3b2231b5f13679ee85b64e6cc2f7ac Mon Sep 17 00:00:00 2001 From: Leighton Chen Date: Mon, 3 Aug 2020 17:48:44 -0700 Subject: [PATCH 1/9] Rename db framework packages from "ext" to "instrumentation" (#966) --- .../CHANGELOG.md | 19 +++ .../README.rst | 23 +++ .../setup.cfg | 55 +++++++ .../setup.py | 31 ++++ .../instrumentation/asyncpg/__init__.py | 142 ++++++++++++++++++ .../instrumentation/asyncpg/version.py | 15 ++ .../tests/__init__.py | 0 .../tests/test_asyncpg_wrapper.py | 35 +++++ 8 files changed, 320 insertions(+) create mode 100644 instrumentation/opentelemetry-instrumentation-asyncpg/CHANGELOG.md create mode 100644 instrumentation/opentelemetry-instrumentation-asyncpg/README.rst create mode 100644 instrumentation/opentelemetry-instrumentation-asyncpg/setup.cfg create mode 100644 instrumentation/opentelemetry-instrumentation-asyncpg/setup.py create mode 100644 instrumentation/opentelemetry-instrumentation-asyncpg/src/opentelemetry/instrumentation/asyncpg/__init__.py create mode 100644 instrumentation/opentelemetry-instrumentation-asyncpg/src/opentelemetry/instrumentation/asyncpg/version.py create mode 100644 instrumentation/opentelemetry-instrumentation-asyncpg/tests/__init__.py create mode 100644 instrumentation/opentelemetry-instrumentation-asyncpg/tests/test_asyncpg_wrapper.py diff --git a/instrumentation/opentelemetry-instrumentation-asyncpg/CHANGELOG.md b/instrumentation/opentelemetry-instrumentation-asyncpg/CHANGELOG.md new file mode 100644 index 000000000000..08464e8cd9f7 --- /dev/null +++ b/instrumentation/opentelemetry-instrumentation-asyncpg/CHANGELOG.md @@ -0,0 +1,19 @@ +# Changelog + +## Unreleased + +- Change package name to opentelemetry-instrumentation-asyncpg + ([#999](https://github.com/open-telemetry/opentelemetry-python/pull/999)) + +## Version 0.11b0 + +Released 2020-07-28 + +- Shouldn't capture query parameters by default + ([#854](https://github.com/open-telemetry/opentelemetry-python/pull/854)) + +## Version 0.10b0 + +Released 2020-06-23 + +- Initial Release ([#814](https://github.com/open-telemetry/opentelemetry-python/pull/814)) diff --git a/instrumentation/opentelemetry-instrumentation-asyncpg/README.rst b/instrumentation/opentelemetry-instrumentation-asyncpg/README.rst new file mode 100644 index 000000000000..33c60852cd46 --- /dev/null +++ b/instrumentation/opentelemetry-instrumentation-asyncpg/README.rst @@ -0,0 +1,23 @@ +OpenTelemetry asyncpg Instrumentation +===================================== + +|pypi| + +.. |pypi| image:: https://badge.fury.io/py/opentelemetry-instrumentation-asyncpg.svg + :target: https://pypi.org/project/opentelemetry-instrumentation-asyncpg/ + +This library allows tracing PostgreSQL queries made by the +`asyncpg `_ library. + +Installation +------------ + +:: + + pip install opentelemetry-instrumentation-asyncpg + +References +---------- + +* `OpenTelemetry asyncpg Instrumentation `_ +* `OpenTelemetry Project `_ diff --git a/instrumentation/opentelemetry-instrumentation-asyncpg/setup.cfg b/instrumentation/opentelemetry-instrumentation-asyncpg/setup.cfg new file mode 100644 index 000000000000..1cc707df1c0f --- /dev/null +++ b/instrumentation/opentelemetry-instrumentation-asyncpg/setup.cfg @@ -0,0 +1,55 @@ +# Copyright The OpenTelemetry Authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# 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. +# +[metadata] +name = opentelemetry-instrumentation-asyncpg +description = OpenTelemetry instrumentation for AsyncPG +long_description = file: README.rst +long_description_content_type = text/x-rst +author = OpenTelemetry Authors +author_email = cncf-opentelemetry-contributors@lists.cncf.io +url = https://github.com/open-telemetry/opentelemetry-python/instrumentation/opentelemetry-instrumentation-asyncpg +platforms = any +license = Apache-2.0 +classifiers = + Development Status :: 4 - Beta + Intended Audience :: Developers + License :: OSI Approved :: Apache Software License + Programming Language :: Python + Programming Language :: Python :: 3 + Programming Language :: Python :: 3.5 + Programming Language :: Python :: 3.6 + Programming Language :: Python :: 3.7 + Programming Language :: Python :: 3.8 + +[options] +python_requires = >=3.5 +package_dir= + =src +packages=find_namespace: +install_requires = + opentelemetry-api == 0.12.dev0 + opentelemetry-instrumentation == 0.12.dev0 + asyncpg >= 0.12.0 + +[options.extras_require] +test = + opentelemetry-test == 0.12.dev0 + +[options.packages.find] +where = src + +[options.entry_points] +opentelemetry_instrumentor = + asyncpg = opentelemetry.instrumentation.asyncpg:AsyncPGInstrumentor diff --git a/instrumentation/opentelemetry-instrumentation-asyncpg/setup.py b/instrumentation/opentelemetry-instrumentation-asyncpg/setup.py new file mode 100644 index 000000000000..2ad47ac9d98e --- /dev/null +++ b/instrumentation/opentelemetry-instrumentation-asyncpg/setup.py @@ -0,0 +1,31 @@ +# Copyright The OpenTelemetry Authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# 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. +import os + +import setuptools + +BASE_DIR = os.path.dirname(__file__) +VERSION_FILENAME = os.path.join( + BASE_DIR, + "src", + "opentelemetry", + "instrumentation", + "asyncpg", + "version.py", +) +PACKAGE_INFO = {} +with open(VERSION_FILENAME) as f: + exec(f.read(), PACKAGE_INFO) + +setuptools.setup(version=PACKAGE_INFO["__version__"]) diff --git a/instrumentation/opentelemetry-instrumentation-asyncpg/src/opentelemetry/instrumentation/asyncpg/__init__.py b/instrumentation/opentelemetry-instrumentation-asyncpg/src/opentelemetry/instrumentation/asyncpg/__init__.py new file mode 100644 index 000000000000..189809809d97 --- /dev/null +++ b/instrumentation/opentelemetry-instrumentation-asyncpg/src/opentelemetry/instrumentation/asyncpg/__init__.py @@ -0,0 +1,142 @@ +# Copyright The OpenTelemetry Authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# 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. + +""" +This library allows tracing PostgreSQL queries made by the +`asyncpg `_ library. + +Usage +----- + +.. code-block:: python + + import asyncpg + from opentelemetry.instrumentation.asyncpg import AsyncPGInstrumentor + + # You can optionally pass a custom TracerProvider to AsyncPGInstrumentor.instrument() + AsyncPGInstrumentor().instrument() + conn = await asyncpg.connect(user='user', password='password', + database='database', host='127.0.0.1') + values = await conn.fetch('''SELECT 42;''') + +API +--- +""" + +import asyncpg +import wrapt +from asyncpg import exceptions + +from opentelemetry import trace +from opentelemetry.instrumentation.asyncpg.version import __version__ +from opentelemetry.instrumentation.instrumentor import BaseInstrumentor +from opentelemetry.instrumentation.utils import unwrap +from opentelemetry.trace import SpanKind +from opentelemetry.trace.status import Status, StatusCanonicalCode + +_APPLIED = "_opentelemetry_tracer" + + +def _exception_to_canonical_code(exc: Exception) -> StatusCanonicalCode: + if isinstance( + exc, (exceptions.InterfaceError, exceptions.SyntaxOrAccessError), + ): + return StatusCanonicalCode.INVALID_ARGUMENT + if isinstance(exc, exceptions.IdleInTransactionSessionTimeoutError): + return StatusCanonicalCode.DEADLINE_EXCEEDED + return StatusCanonicalCode.UNKNOWN + + +def _hydrate_span_from_args(connection, query, parameters) -> dict: + span_attributes = {"db.type": "sql"} + + params = getattr(connection, "_params", None) + span_attributes["db.instance"] = getattr(params, "database", None) + span_attributes["db.user"] = getattr(params, "user", None) + + if query is not None: + span_attributes["db.statement"] = query + + if parameters is not None and len(parameters) > 0: + span_attributes["db.statement.parameters"] = str(parameters) + + return span_attributes + + +class AsyncPGInstrumentor(BaseInstrumentor): + def __init__(self, capture_parameters=False): + super().__init__() + self.capture_parameters = capture_parameters + + def _instrument(self, **kwargs): + tracer_provider = kwargs.get( + "tracer_provider", trace.get_tracer_provider() + ) + setattr( + asyncpg, + _APPLIED, + tracer_provider.get_tracer("asyncpg", __version__), + ) + + for method in [ + "Connection.execute", + "Connection.executemany", + "Connection.fetch", + "Connection.fetchval", + "Connection.fetchrow", + ]: + wrapt.wrap_function_wrapper( + "asyncpg.connection", method, self._do_execute + ) + + def _uninstrument(self, **__): + delattr(asyncpg, _APPLIED) + for method in [ + "execute", + "executemany", + "fetch", + "fetchval", + "fetchrow", + ]: + unwrap(asyncpg.Connection, method) + + async def _do_execute(self, func, instance, args, kwargs): + span_attributes = _hydrate_span_from_args( + instance, args[0], args[1:] if self.capture_parameters else None, + ) + tracer = getattr(asyncpg, _APPLIED) + + exception = None + + with tracer.start_as_current_span( + "postgresql", kind=SpanKind.CLIENT + ) as span: + + for attribute, value in span_attributes.items(): + span.set_attribute(attribute, value) + + try: + result = await func(*args, **kwargs) + except Exception as exc: # pylint: disable=W0703 + exception = exc + raise + finally: + if exception is not None: + span.set_status( + Status(_exception_to_canonical_code(exception)) + ) + else: + span.set_status(Status(StatusCanonicalCode.OK)) + + return result diff --git a/instrumentation/opentelemetry-instrumentation-asyncpg/src/opentelemetry/instrumentation/asyncpg/version.py b/instrumentation/opentelemetry-instrumentation-asyncpg/src/opentelemetry/instrumentation/asyncpg/version.py new file mode 100644 index 000000000000..780a92b6a1bb --- /dev/null +++ b/instrumentation/opentelemetry-instrumentation-asyncpg/src/opentelemetry/instrumentation/asyncpg/version.py @@ -0,0 +1,15 @@ +# Copyright The OpenTelemetry Authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# 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. + +__version__ = "0.12.dev0" diff --git a/instrumentation/opentelemetry-instrumentation-asyncpg/tests/__init__.py b/instrumentation/opentelemetry-instrumentation-asyncpg/tests/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/instrumentation/opentelemetry-instrumentation-asyncpg/tests/test_asyncpg_wrapper.py b/instrumentation/opentelemetry-instrumentation-asyncpg/tests/test_asyncpg_wrapper.py new file mode 100644 index 000000000000..33b121ce53a7 --- /dev/null +++ b/instrumentation/opentelemetry-instrumentation-asyncpg/tests/test_asyncpg_wrapper.py @@ -0,0 +1,35 @@ +import asyncpg +from asyncpg import Connection + +from opentelemetry.instrumentation.asyncpg import AsyncPGInstrumentor +from opentelemetry.test.test_base import TestBase + + +class TestAsyncPGInstrumentation(TestBase): + def test_instrumentation_flags(self): + AsyncPGInstrumentor().instrument() + self.assertTrue(hasattr(asyncpg, "_opentelemetry_tracer")) + AsyncPGInstrumentor().uninstrument() + self.assertFalse(hasattr(asyncpg, "_opentelemetry_tracer")) + + def test_duplicated_instrumentation(self): + AsyncPGInstrumentor().instrument() + AsyncPGInstrumentor().instrument() + AsyncPGInstrumentor().instrument() + AsyncPGInstrumentor().uninstrument() + for method_name in ["execute", "fetch"]: + method = getattr(Connection, method_name, None) + self.assertFalse( + hasattr(method, "_opentelemetry_ext_asyncpg_applied") + ) + + def test_duplicated_uninstrumentation(self): + AsyncPGInstrumentor().instrument() + AsyncPGInstrumentor().uninstrument() + AsyncPGInstrumentor().uninstrument() + AsyncPGInstrumentor().uninstrument() + for method_name in ["execute", "fetch"]: + method = getattr(Connection, method_name, None) + self.assertFalse( + hasattr(method, "_opentelemetry_ext_asyncpg_applied") + ) From 39057c03a327317a7595adfdbe01368ba549abd9 Mon Sep 17 00:00:00 2001 From: Leighton Chen Date: Tue, 4 Aug 2020 19:10:51 -0700 Subject: [PATCH 2/9] Rename remaining framework packages from "ext" to "instrumentation" (#969) --- .../opentelemetry-instrumentation-asyncpg/CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/instrumentation/opentelemetry-instrumentation-asyncpg/CHANGELOG.md b/instrumentation/opentelemetry-instrumentation-asyncpg/CHANGELOG.md index 08464e8cd9f7..530bc71da754 100644 --- a/instrumentation/opentelemetry-instrumentation-asyncpg/CHANGELOG.md +++ b/instrumentation/opentelemetry-instrumentation-asyncpg/CHANGELOG.md @@ -3,7 +3,7 @@ ## Unreleased - Change package name to opentelemetry-instrumentation-asyncpg - ([#999](https://github.com/open-telemetry/opentelemetry-python/pull/999)) + ([#966](https://github.com/open-telemetry/opentelemetry-python/pull/966)) ## Version 0.11b0 From 005da9623d76e09ff87d89b8092018419b7b86a6 Mon Sep 17 00:00:00 2001 From: Leighton Chen Date: Sat, 15 Aug 2020 18:06:27 -0700 Subject: [PATCH 3/9] chore: 0.13.dev0 version update (#991) --- .../opentelemetry-instrumentation-asyncpg/CHANGELOG.md | 4 ++++ .../opentelemetry-instrumentation-asyncpg/setup.cfg | 6 +++--- .../src/opentelemetry/instrumentation/asyncpg/version.py | 2 +- 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/instrumentation/opentelemetry-instrumentation-asyncpg/CHANGELOG.md b/instrumentation/opentelemetry-instrumentation-asyncpg/CHANGELOG.md index 530bc71da754..56f261c9b54d 100644 --- a/instrumentation/opentelemetry-instrumentation-asyncpg/CHANGELOG.md +++ b/instrumentation/opentelemetry-instrumentation-asyncpg/CHANGELOG.md @@ -2,6 +2,10 @@ ## Unreleased +## Version 0.12b0 + +Released 2020-08-14 + - Change package name to opentelemetry-instrumentation-asyncpg ([#966](https://github.com/open-telemetry/opentelemetry-python/pull/966)) diff --git a/instrumentation/opentelemetry-instrumentation-asyncpg/setup.cfg b/instrumentation/opentelemetry-instrumentation-asyncpg/setup.cfg index 1cc707df1c0f..2f267b3f493d 100644 --- a/instrumentation/opentelemetry-instrumentation-asyncpg/setup.cfg +++ b/instrumentation/opentelemetry-instrumentation-asyncpg/setup.cfg @@ -39,13 +39,13 @@ package_dir= =src packages=find_namespace: install_requires = - opentelemetry-api == 0.12.dev0 - opentelemetry-instrumentation == 0.12.dev0 + opentelemetry-api == 0.13dev0 + opentelemetry-instrumentation == 0.13dev0 asyncpg >= 0.12.0 [options.extras_require] test = - opentelemetry-test == 0.12.dev0 + opentelemetry-test == 0.13dev0 [options.packages.find] where = src diff --git a/instrumentation/opentelemetry-instrumentation-asyncpg/src/opentelemetry/instrumentation/asyncpg/version.py b/instrumentation/opentelemetry-instrumentation-asyncpg/src/opentelemetry/instrumentation/asyncpg/version.py index 780a92b6a1bb..9cc445d09e13 100644 --- a/instrumentation/opentelemetry-instrumentation-asyncpg/src/opentelemetry/instrumentation/asyncpg/version.py +++ b/instrumentation/opentelemetry-instrumentation-asyncpg/src/opentelemetry/instrumentation/asyncpg/version.py @@ -12,4 +12,4 @@ # See the License for the specific language governing permissions and # limitations under the License. -__version__ = "0.12.dev0" +__version__ = "0.13dev0" From 7261064939d243e2949f0411dae1226dbebd4f44 Mon Sep 17 00:00:00 2001 From: alrex Date: Thu, 17 Sep 2020 08:23:52 -0700 Subject: [PATCH 4/9] release: updating changelogs and version to 0.13b0 (#1129) * updating changelogs and version to 0.13b0 --- .../opentelemetry-instrumentation-asyncpg/setup.cfg | 6 +++--- .../src/opentelemetry/instrumentation/asyncpg/version.py | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/instrumentation/opentelemetry-instrumentation-asyncpg/setup.cfg b/instrumentation/opentelemetry-instrumentation-asyncpg/setup.cfg index 2f267b3f493d..7f751c509372 100644 --- a/instrumentation/opentelemetry-instrumentation-asyncpg/setup.cfg +++ b/instrumentation/opentelemetry-instrumentation-asyncpg/setup.cfg @@ -39,13 +39,13 @@ package_dir= =src packages=find_namespace: install_requires = - opentelemetry-api == 0.13dev0 - opentelemetry-instrumentation == 0.13dev0 + opentelemetry-api == 0.13b0 + opentelemetry-instrumentation == 0.13b0 asyncpg >= 0.12.0 [options.extras_require] test = - opentelemetry-test == 0.13dev0 + opentelemetry-test == 0.13b0 [options.packages.find] where = src diff --git a/instrumentation/opentelemetry-instrumentation-asyncpg/src/opentelemetry/instrumentation/asyncpg/version.py b/instrumentation/opentelemetry-instrumentation-asyncpg/src/opentelemetry/instrumentation/asyncpg/version.py index 9cc445d09e13..2015e87c705f 100644 --- a/instrumentation/opentelemetry-instrumentation-asyncpg/src/opentelemetry/instrumentation/asyncpg/version.py +++ b/instrumentation/opentelemetry-instrumentation-asyncpg/src/opentelemetry/instrumentation/asyncpg/version.py @@ -12,4 +12,4 @@ # See the License for the specific language governing permissions and # limitations under the License. -__version__ = "0.13dev0" +__version__ = "0.13b0" From 6d83acf8347c03c767c7eb898ec2da4f04692d97 Mon Sep 17 00:00:00 2001 From: alrex Date: Thu, 17 Sep 2020 12:21:39 -0700 Subject: [PATCH 5/9] chore: bump dev version (#1131) --- .../opentelemetry-instrumentation-asyncpg/setup.cfg | 6 +++--- .../src/opentelemetry/instrumentation/asyncpg/version.py | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/instrumentation/opentelemetry-instrumentation-asyncpg/setup.cfg b/instrumentation/opentelemetry-instrumentation-asyncpg/setup.cfg index 7f751c509372..083d676db5ed 100644 --- a/instrumentation/opentelemetry-instrumentation-asyncpg/setup.cfg +++ b/instrumentation/opentelemetry-instrumentation-asyncpg/setup.cfg @@ -39,13 +39,13 @@ package_dir= =src packages=find_namespace: install_requires = - opentelemetry-api == 0.13b0 - opentelemetry-instrumentation == 0.13b0 + opentelemetry-api == 0.14.dev0 + opentelemetry-instrumentation == 0.14.dev0 asyncpg >= 0.12.0 [options.extras_require] test = - opentelemetry-test == 0.13b0 + opentelemetry-test == 0.14.dev0 [options.packages.find] where = src diff --git a/instrumentation/opentelemetry-instrumentation-asyncpg/src/opentelemetry/instrumentation/asyncpg/version.py b/instrumentation/opentelemetry-instrumentation-asyncpg/src/opentelemetry/instrumentation/asyncpg/version.py index 2015e87c705f..0f990278982e 100644 --- a/instrumentation/opentelemetry-instrumentation-asyncpg/src/opentelemetry/instrumentation/asyncpg/version.py +++ b/instrumentation/opentelemetry-instrumentation-asyncpg/src/opentelemetry/instrumentation/asyncpg/version.py @@ -12,4 +12,4 @@ # See the License for the specific language governing permissions and # limitations under the License. -__version__ = "0.13b0" +__version__ = "0.14.dev0" From 9ea1b71b0094b246a27668617a68a55401f32333 Mon Sep 17 00:00:00 2001 From: Leighton Chen Date: Thu, 8 Oct 2020 15:25:20 -0400 Subject: [PATCH 6/9] Use is_recording flag in aiopg, asyncpg, dbapi, psycopg2, pymemcache, pymongo, redis, sqlalchemy instrumentations (#1212) --- .../instrumentation/asyncpg/__init__.py | 27 ++++++++++--------- 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/instrumentation/opentelemetry-instrumentation-asyncpg/src/opentelemetry/instrumentation/asyncpg/__init__.py b/instrumentation/opentelemetry-instrumentation-asyncpg/src/opentelemetry/instrumentation/asyncpg/__init__.py index 189809809d97..6af816b39dff 100644 --- a/instrumentation/opentelemetry-instrumentation-asyncpg/src/opentelemetry/instrumentation/asyncpg/__init__.py +++ b/instrumentation/opentelemetry-instrumentation-asyncpg/src/opentelemetry/instrumentation/asyncpg/__init__.py @@ -112,9 +112,6 @@ def _uninstrument(self, **__): unwrap(asyncpg.Connection, method) async def _do_execute(self, func, instance, args, kwargs): - span_attributes = _hydrate_span_from_args( - instance, args[0], args[1:] if self.capture_parameters else None, - ) tracer = getattr(asyncpg, _APPLIED) exception = None @@ -122,9 +119,14 @@ async def _do_execute(self, func, instance, args, kwargs): with tracer.start_as_current_span( "postgresql", kind=SpanKind.CLIENT ) as span: - - for attribute, value in span_attributes.items(): - span.set_attribute(attribute, value) + if span.is_recording(): + span_attributes = _hydrate_span_from_args( + instance, + args[0], + args[1:] if self.capture_parameters else None, + ) + for attribute, value in span_attributes.items(): + span.set_attribute(attribute, value) try: result = await func(*args, **kwargs) @@ -132,11 +134,12 @@ async def _do_execute(self, func, instance, args, kwargs): exception = exc raise finally: - if exception is not None: - span.set_status( - Status(_exception_to_canonical_code(exception)) - ) - else: - span.set_status(Status(StatusCanonicalCode.OK)) + if span.is_recording(): + if exception is not None: + span.set_status( + Status(_exception_to_canonical_code(exception)) + ) + else: + span.set_status(Status(StatusCanonicalCode.OK)) return result From 585b69fe0b9ceb423bbdee3dad75eaa0d24ff55f Mon Sep 17 00:00:00 2001 From: Leighton Chen Date: Tue, 13 Oct 2020 14:38:09 -0400 Subject: [PATCH 7/9] chore: bump dev version (#1235) --- .../opentelemetry-instrumentation-asyncpg/setup.cfg | 6 +++--- .../src/opentelemetry/instrumentation/asyncpg/version.py | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/instrumentation/opentelemetry-instrumentation-asyncpg/setup.cfg b/instrumentation/opentelemetry-instrumentation-asyncpg/setup.cfg index 083d676db5ed..25e774c247fb 100644 --- a/instrumentation/opentelemetry-instrumentation-asyncpg/setup.cfg +++ b/instrumentation/opentelemetry-instrumentation-asyncpg/setup.cfg @@ -39,13 +39,13 @@ package_dir= =src packages=find_namespace: install_requires = - opentelemetry-api == 0.14.dev0 - opentelemetry-instrumentation == 0.14.dev0 + opentelemetry-api == 0.15.dev0 + opentelemetry-instrumentation == 0.15.dev0 asyncpg >= 0.12.0 [options.extras_require] test = - opentelemetry-test == 0.14.dev0 + opentelemetry-test == 0.15.dev0 [options.packages.find] where = src diff --git a/instrumentation/opentelemetry-instrumentation-asyncpg/src/opentelemetry/instrumentation/asyncpg/version.py b/instrumentation/opentelemetry-instrumentation-asyncpg/src/opentelemetry/instrumentation/asyncpg/version.py index 0f990278982e..e7b342d644e0 100644 --- a/instrumentation/opentelemetry-instrumentation-asyncpg/src/opentelemetry/instrumentation/asyncpg/version.py +++ b/instrumentation/opentelemetry-instrumentation-asyncpg/src/opentelemetry/instrumentation/asyncpg/version.py @@ -12,4 +12,4 @@ # See the License for the specific language governing permissions and # limitations under the License. -__version__ = "0.14.dev0" +__version__ = "0.15.dev0" From 86aef2c7510b28f851ca7b384c7e4a3dca829399 Mon Sep 17 00:00:00 2001 From: Leighton Chen Date: Wed, 28 Oct 2020 17:28:58 -0400 Subject: [PATCH 8/9] Change status codes from grpc status codes, remove setting status in instrumentations except on ERROR (#1282) --- .../instrumentation/asyncpg/__init__.py | 21 +++---------------- 1 file changed, 3 insertions(+), 18 deletions(-) diff --git a/instrumentation/opentelemetry-instrumentation-asyncpg/src/opentelemetry/instrumentation/asyncpg/__init__.py b/instrumentation/opentelemetry-instrumentation-asyncpg/src/opentelemetry/instrumentation/asyncpg/__init__.py index 6af816b39dff..2f4ecaf3af62 100644 --- a/instrumentation/opentelemetry-instrumentation-asyncpg/src/opentelemetry/instrumentation/asyncpg/__init__.py +++ b/instrumentation/opentelemetry-instrumentation-asyncpg/src/opentelemetry/instrumentation/asyncpg/__init__.py @@ -43,21 +43,11 @@ from opentelemetry.instrumentation.instrumentor import BaseInstrumentor from opentelemetry.instrumentation.utils import unwrap from opentelemetry.trace import SpanKind -from opentelemetry.trace.status import Status, StatusCanonicalCode +from opentelemetry.trace.status import Status, StatusCode _APPLIED = "_opentelemetry_tracer" -def _exception_to_canonical_code(exc: Exception) -> StatusCanonicalCode: - if isinstance( - exc, (exceptions.InterfaceError, exceptions.SyntaxOrAccessError), - ): - return StatusCanonicalCode.INVALID_ARGUMENT - if isinstance(exc, exceptions.IdleInTransactionSessionTimeoutError): - return StatusCanonicalCode.DEADLINE_EXCEEDED - return StatusCanonicalCode.UNKNOWN - - def _hydrate_span_from_args(connection, query, parameters) -> dict: span_attributes = {"db.type": "sql"} @@ -134,12 +124,7 @@ async def _do_execute(self, func, instance, args, kwargs): exception = exc raise finally: - if span.is_recording(): - if exception is not None: - span.set_status( - Status(_exception_to_canonical_code(exception)) - ) - else: - span.set_status(Status(StatusCanonicalCode.OK)) + if span.is_recording() and exception is not None: + span.set_status(Status(StatusCode.ERROR)) return result From 8ffbfb0787af1f0a665aec6cd2ce0ffa95bc14b4 Mon Sep 17 00:00:00 2001 From: alrex Date: Mon, 2 Nov 2020 09:00:06 -0800 Subject: [PATCH 9/9] [pre-release] Update changelogs, version [0.15b0] (#1320) --- .../opentelemetry-instrumentation-asyncpg/setup.cfg | 6 +++--- .../src/opentelemetry/instrumentation/asyncpg/version.py | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/instrumentation/opentelemetry-instrumentation-asyncpg/setup.cfg b/instrumentation/opentelemetry-instrumentation-asyncpg/setup.cfg index 25e774c247fb..8d31ad031ad6 100644 --- a/instrumentation/opentelemetry-instrumentation-asyncpg/setup.cfg +++ b/instrumentation/opentelemetry-instrumentation-asyncpg/setup.cfg @@ -39,13 +39,13 @@ package_dir= =src packages=find_namespace: install_requires = - opentelemetry-api == 0.15.dev0 - opentelemetry-instrumentation == 0.15.dev0 + opentelemetry-api == 0.15b0 + opentelemetry-instrumentation == 0.15b0 asyncpg >= 0.12.0 [options.extras_require] test = - opentelemetry-test == 0.15.dev0 + opentelemetry-test == 0.15b0 [options.packages.find] where = src diff --git a/instrumentation/opentelemetry-instrumentation-asyncpg/src/opentelemetry/instrumentation/asyncpg/version.py b/instrumentation/opentelemetry-instrumentation-asyncpg/src/opentelemetry/instrumentation/asyncpg/version.py index e7b342d644e0..ff494d225aa4 100644 --- a/instrumentation/opentelemetry-instrumentation-asyncpg/src/opentelemetry/instrumentation/asyncpg/version.py +++ b/instrumentation/opentelemetry-instrumentation-asyncpg/src/opentelemetry/instrumentation/asyncpg/version.py @@ -12,4 +12,4 @@ # See the License for the specific language governing permissions and # limitations under the License. -__version__ = "0.15.dev0" +__version__ = "0.15b0"