diff --git a/CHANGELOG.md b/CHANGELOG.md index c07bbb7444..c2ee7393e1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 +- `opentelemetry-instrumentation-asyncpg` Fix high cardinality in the span name + ([#1324](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1324)) + ### Added - `opentelemetry-instrumentation-grpc` add supports to filter requests to instrument. ([#1241](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1241)) @@ -42,7 +45,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ([#1208](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1208)) - `opentelemetry-instrumentation-aiohttp-client` Fix producing additional spans with each newly created ClientSession - ([#1246](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1246)) -- Add _is_openetlemetry_instrumented check in _InstrumentedFastAPI class +- Add _is_opentelemetry_instrumented check in _InstrumentedFastAPI class ([#1313](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1313)) - Fix uninstrumentation of existing app instances in FastAPI ([#1258](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1258)) 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 b66168f4d0..e4074885f2 100644 --- a/instrumentation/opentelemetry-instrumentation-asyncpg/src/opentelemetry/instrumentation/asyncpg/__init__.py +++ b/instrumentation/opentelemetry-instrumentation-asyncpg/src/opentelemetry/instrumentation/asyncpg/__init__.py @@ -134,6 +134,11 @@ async def _do_execute(self, func, instance, args, kwargs): params = getattr(instance, "_params", {}) name = args[0] if args[0] else params.get("database", "postgresql") + try: + name = name.split()[0] + except IndexError: + name = "" + with self._tracer.start_as_current_span( name, kind=SpanKind.CLIENT ) as span: diff --git a/tests/opentelemetry-docker-tests/tests/asyncpg/test_asyncpg_functional.py b/tests/opentelemetry-docker-tests/tests/asyncpg/test_asyncpg_functional.py index 8111bce15e..8af57aa658 100644 --- a/tests/opentelemetry-docker-tests/tests/asyncpg/test_asyncpg_functional.py +++ b/tests/opentelemetry-docker-tests/tests/asyncpg/test_asyncpg_functional.py @@ -62,7 +62,7 @@ def test_instrumented_execute_method_without_arguments(self, *_, **__): self.assertEqual(len(spans), 1) self.assertIs(StatusCode.UNSET, spans[0].status.status_code) self.check_span(spans[0]) - self.assertEqual(spans[0].name, "SELECT 42;") + self.assertEqual(spans[0].name, "SELECT") self.assertEqual( spans[0].attributes[SpanAttributes.DB_STATEMENT], "SELECT 42;" ) @@ -72,6 +72,7 @@ def test_instrumented_fetch_method_without_arguments(self, *_, **__): spans = self.memory_exporter.get_finished_spans() self.assertEqual(len(spans), 1) self.check_span(spans[0]) + self.assertEqual(spans[0].name, "SELECT") self.assertEqual( spans[0].attributes[SpanAttributes.DB_STATEMENT], "SELECT 42;" ) @@ -189,7 +190,7 @@ def test_instrumented_execute_method_with_arguments(self, *_, **__): self.assertIs(StatusCode.UNSET, spans[0].status.status_code) self.check_span(spans[0]) - self.assertEqual(spans[0].name, "SELECT $1;") + self.assertEqual(spans[0].name, "SELECT") self.assertEqual( spans[0].attributes[SpanAttributes.DB_STATEMENT], "SELECT $1;" ) @@ -203,6 +204,7 @@ def test_instrumented_fetch_method_with_arguments(self, *_, **__): self.assertEqual(len(spans), 1) self.check_span(spans[0]) + self.assertEqual(spans[0].name, "SELECT") self.assertEqual( spans[0].attributes[SpanAttributes.DB_STATEMENT], "SELECT $1;" )