From c34b011e9b7f5e82d2a741a98e0f82a7daa70ff6 Mon Sep 17 00:00:00 2001 From: Diogo Date: Thu, 12 Jan 2023 10:27:19 +0100 Subject: [PATCH 1/4] Use pydantic model name as default `title` value --- .../src/opentelemetry/instrumentation/tortoiseorm/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/instrumentation/opentelemetry-instrumentation-tortoiseorm/src/opentelemetry/instrumentation/tortoiseorm/__init__.py b/instrumentation/opentelemetry-instrumentation-tortoiseorm/src/opentelemetry/instrumentation/tortoiseorm/__init__.py index 3b0e58c928..f1650155d4 100644 --- a/instrumentation/opentelemetry-instrumentation-tortoiseorm/src/opentelemetry/instrumentation/tortoiseorm/__init__.py +++ b/instrumentation/opentelemetry-instrumentation-tortoiseorm/src/opentelemetry/instrumentation/tortoiseorm/__init__.py @@ -300,7 +300,7 @@ async def _from_queryset(self, func, modelcls, args, kwargs): model_config = getattr(modelcls, "Config", None) if model_config: - model_title = getattr(modelcls.Config, "title") + model_title = getattr(modelcls.Config, "title", modelcls.__name__) if model_title: span_attributes["pydantic.model"] = model_title From 7ddeda5439bb1a0dbaafd773c996f3a6fedc3d10 Mon Sep 17 00:00:00 2001 From: Diogo Date: Mon, 16 Jan 2023 10:14:05 +0100 Subject: [PATCH 2/4] Update `CHANGELOG.md` --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 068a067dd1..b33531467c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed +- Fix TortoiseORM instrumentation `AttributeError: type object 'Config' has no attribute 'title'` + ([#1575](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1575)) - Fix aiopg instrumentation to work with aiopg < 2.0.0 ([#1473](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1473)) - `opentelemetry-instrumentation-aws-lambda` Adds an option to configure `disable_aws_context_propagation` by From 22644f9eae048327617515ea381e223e92f39910 Mon Sep 17 00:00:00 2001 From: Diogo Date: Thu, 19 Jan 2023 09:44:27 +0100 Subject: [PATCH 3/4] Format with black --- .../instrumentation/tortoiseorm/__init__.py | 50 ++++++------------- 1 file changed, 16 insertions(+), 34 deletions(-) diff --git a/instrumentation/opentelemetry-instrumentation-tortoiseorm/src/opentelemetry/instrumentation/tortoiseorm/__init__.py b/instrumentation/opentelemetry-instrumentation-tortoiseorm/src/opentelemetry/instrumentation/tortoiseorm/__init__.py index f1650155d4..b7434cacde 100644 --- a/instrumentation/opentelemetry-instrumentation-tortoiseorm/src/opentelemetry/instrumentation/tortoiseorm/__init__.py +++ b/instrumentation/opentelemetry-instrumentation-tortoiseorm/src/opentelemetry/instrumentation/tortoiseorm/__init__.py @@ -159,35 +159,23 @@ def _instrument(self, **kwargs): def _uninstrument(self, **kwargs): if TORTOISE_SQLITE_SUPPORT: - unwrap( - tortoise.backends.sqlite.client.SqliteClient, "execute_query" - ) - unwrap( - tortoise.backends.sqlite.client.SqliteClient, "execute_many" - ) - unwrap( - tortoise.backends.sqlite.client.SqliteClient, "execute_insert" - ) + unwrap(tortoise.backends.sqlite.client.SqliteClient, "execute_query") + unwrap(tortoise.backends.sqlite.client.SqliteClient, "execute_many") + unwrap(tortoise.backends.sqlite.client.SqliteClient, "execute_insert") unwrap( tortoise.backends.sqlite.client.SqliteClient, "execute_query_dict", ) - unwrap( - tortoise.backends.sqlite.client.SqliteClient, "execute_script" - ) + unwrap(tortoise.backends.sqlite.client.SqliteClient, "execute_script") if TORTOISE_MYSQL_SUPPORT: unwrap(tortoise.backends.mysql.client.MySQLClient, "execute_query") unwrap(tortoise.backends.mysql.client.MySQLClient, "execute_many") - unwrap( - tortoise.backends.mysql.client.MySQLClient, "execute_insert" - ) + unwrap(tortoise.backends.mysql.client.MySQLClient, "execute_insert") unwrap( tortoise.backends.mysql.client.MySQLClient, "execute_query_dict", ) - unwrap( - tortoise.backends.mysql.client.MySQLClient, "execute_script" - ) + unwrap(tortoise.backends.mysql.client.MySQLClient, "execute_script") if TORTOISE_POSTGRES_SUPPORT: unwrap( tortoise.backends.asyncpg.client.AsyncpgDBClient, @@ -214,9 +202,7 @@ def _uninstrument(self, **kwargs): tortoise.contrib.pydantic.base.PydanticModel, "from_queryset_single", ) - unwrap( - tortoise.contrib.pydantic.base.PydanticListModel, "from_queryset" - ) + unwrap(tortoise.contrib.pydantic.base.PydanticListModel, "from_queryset") def _hydrate_span_from_args(self, connection, query, parameters) -> dict: """Get network and database attributes from connection.""" @@ -224,17 +210,13 @@ def _hydrate_span_from_args(self, connection, query, parameters) -> dict: capabilities = getattr(connection, "capabilities", None) if capabilities is not None: if capabilities.dialect == "sqlite": - span_attributes[ - SpanAttributes.DB_SYSTEM - ] = DbSystemValues.SQLITE.value + span_attributes[SpanAttributes.DB_SYSTEM] = DbSystemValues.SQLITE.value elif capabilities.dialect == "postgres": span_attributes[ SpanAttributes.DB_SYSTEM ] = DbSystemValues.POSTGRESQL.value elif capabilities.dialect == "mysql": - span_attributes[ - SpanAttributes.DB_SYSTEM - ] = DbSystemValues.MYSQL.value + span_attributes[SpanAttributes.DB_SYSTEM] = DbSystemValues.MYSQL.value dbname = getattr(connection, "filename", None) if dbname: span_attributes[SpanAttributes.DB_NAME] = dbname @@ -264,9 +246,7 @@ async def _do_execute(self, func, instance, args, kwargs): exception = None name = args[0].split()[0] - with self._tracer.start_as_current_span( - name, kind=SpanKind.CLIENT - ) as span: + with self._tracer.start_as_current_span(name, kind=SpanKind.CLIENT) as span: if span.is_recording(): span_attributes = self._hydrate_span_from_args( instance, @@ -292,15 +272,17 @@ async def _from_queryset(self, func, modelcls, args, kwargs): exception = None name = f"pydantic.{func.__name__}" - with self._tracer.start_as_current_span( - name, kind=SpanKind.INTERNAL - ) as span: + with self._tracer.start_as_current_span(name, kind=SpanKind.INTERNAL) as span: if span.is_recording(): span_attributes = {} model_config = getattr(modelcls, "Config", None) if model_config: - model_title = getattr(modelcls.Config, "title", modelcls.__name__) + model_title = getattr( + modelcls.Config, + "title", + modelcls.__name__, + ) if model_title: span_attributes["pydantic.model"] = model_title From 3e694879701ad8e1f4264c4dd6ab4f596c4684c9 Mon Sep 17 00:00:00 2001 From: Diogo Date: Thu, 19 Jan 2023 18:05:41 +0100 Subject: [PATCH 4/4] Lint with `black` --- .../instrumentation/tortoiseorm/__init__.py | 45 ++++++++++++++----- 1 file changed, 34 insertions(+), 11 deletions(-) diff --git a/instrumentation/opentelemetry-instrumentation-tortoiseorm/src/opentelemetry/instrumentation/tortoiseorm/__init__.py b/instrumentation/opentelemetry-instrumentation-tortoiseorm/src/opentelemetry/instrumentation/tortoiseorm/__init__.py index b7434cacde..925c087bd5 100644 --- a/instrumentation/opentelemetry-instrumentation-tortoiseorm/src/opentelemetry/instrumentation/tortoiseorm/__init__.py +++ b/instrumentation/opentelemetry-instrumentation-tortoiseorm/src/opentelemetry/instrumentation/tortoiseorm/__init__.py @@ -159,23 +159,35 @@ def _instrument(self, **kwargs): def _uninstrument(self, **kwargs): if TORTOISE_SQLITE_SUPPORT: - unwrap(tortoise.backends.sqlite.client.SqliteClient, "execute_query") - unwrap(tortoise.backends.sqlite.client.SqliteClient, "execute_many") - unwrap(tortoise.backends.sqlite.client.SqliteClient, "execute_insert") + unwrap( + tortoise.backends.sqlite.client.SqliteClient, "execute_query" + ) + unwrap( + tortoise.backends.sqlite.client.SqliteClient, "execute_many" + ) + unwrap( + tortoise.backends.sqlite.client.SqliteClient, "execute_insert" + ) unwrap( tortoise.backends.sqlite.client.SqliteClient, "execute_query_dict", ) - unwrap(tortoise.backends.sqlite.client.SqliteClient, "execute_script") + unwrap( + tortoise.backends.sqlite.client.SqliteClient, "execute_script" + ) if TORTOISE_MYSQL_SUPPORT: unwrap(tortoise.backends.mysql.client.MySQLClient, "execute_query") unwrap(tortoise.backends.mysql.client.MySQLClient, "execute_many") - unwrap(tortoise.backends.mysql.client.MySQLClient, "execute_insert") + unwrap( + tortoise.backends.mysql.client.MySQLClient, "execute_insert" + ) unwrap( tortoise.backends.mysql.client.MySQLClient, "execute_query_dict", ) - unwrap(tortoise.backends.mysql.client.MySQLClient, "execute_script") + unwrap( + tortoise.backends.mysql.client.MySQLClient, "execute_script" + ) if TORTOISE_POSTGRES_SUPPORT: unwrap( tortoise.backends.asyncpg.client.AsyncpgDBClient, @@ -202,7 +214,9 @@ def _uninstrument(self, **kwargs): tortoise.contrib.pydantic.base.PydanticModel, "from_queryset_single", ) - unwrap(tortoise.contrib.pydantic.base.PydanticListModel, "from_queryset") + unwrap( + tortoise.contrib.pydantic.base.PydanticListModel, "from_queryset" + ) def _hydrate_span_from_args(self, connection, query, parameters) -> dict: """Get network and database attributes from connection.""" @@ -210,13 +224,17 @@ def _hydrate_span_from_args(self, connection, query, parameters) -> dict: capabilities = getattr(connection, "capabilities", None) if capabilities is not None: if capabilities.dialect == "sqlite": - span_attributes[SpanAttributes.DB_SYSTEM] = DbSystemValues.SQLITE.value + span_attributes[ + SpanAttributes.DB_SYSTEM + ] = DbSystemValues.SQLITE.value elif capabilities.dialect == "postgres": span_attributes[ SpanAttributes.DB_SYSTEM ] = DbSystemValues.POSTGRESQL.value elif capabilities.dialect == "mysql": - span_attributes[SpanAttributes.DB_SYSTEM] = DbSystemValues.MYSQL.value + span_attributes[ + SpanAttributes.DB_SYSTEM + ] = DbSystemValues.MYSQL.value dbname = getattr(connection, "filename", None) if dbname: span_attributes[SpanAttributes.DB_NAME] = dbname @@ -246,7 +264,9 @@ async def _do_execute(self, func, instance, args, kwargs): exception = None name = args[0].split()[0] - with self._tracer.start_as_current_span(name, kind=SpanKind.CLIENT) as span: + with self._tracer.start_as_current_span( + name, kind=SpanKind.CLIENT + ) as span: if span.is_recording(): span_attributes = self._hydrate_span_from_args( instance, @@ -272,7 +292,10 @@ async def _from_queryset(self, func, modelcls, args, kwargs): exception = None name = f"pydantic.{func.__name__}" - with self._tracer.start_as_current_span(name, kind=SpanKind.INTERNAL) as span: + with self._tracer.start_as_current_span( + name, + kind=SpanKind.INTERNAL, + ) as span: if span.is_recording(): span_attributes = {}