From 2c8ac406ebef1b1ab0ca5b4f57f92fc19bf07422 Mon Sep 17 00:00:00 2001 From: avzis Date: Mon, 19 Dec 2022 23:57:58 +0200 Subject: [PATCH 1/7] add a test using NoOpTracerProvider --- .../tests/test_sqlalchemy.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/instrumentation/opentelemetry-instrumentation-sqlalchemy/tests/test_sqlalchemy.py b/instrumentation/opentelemetry-instrumentation-sqlalchemy/tests/test_sqlalchemy.py index 099c088f64..cfdfde4e71 100644 --- a/instrumentation/opentelemetry-instrumentation-sqlalchemy/tests/test_sqlalchemy.py +++ b/instrumentation/opentelemetry-instrumentation-sqlalchemy/tests/test_sqlalchemy.py @@ -253,3 +253,13 @@ def test_uninstrument(self): cnx2.execute("SELECT 2 + 2;").fetchall() spans = self.memory_exporter.get_finished_spans() self.assertEqual(len(spans), 0) + + def test_no_op_tracer_provider(self): + engine = create_engine("sqlite:///:memory:") + SQLAlchemyInstrumentor().instrument( + engine=engine, + tracer_provider=trace.NoOpTracerProvider, + ) + engine.connect() + spans = self.memory_exporter.get_finished_spans() + self.assertEqual(len(spans), 0) From 621a507b5841b373867bf68e8d19a2747317af35 Mon Sep 17 00:00:00 2001 From: avzis Date: Wed, 21 Dec 2022 16:43:26 +0200 Subject: [PATCH 2/7] validate execute doesnt create a span --- .../tests/test_sqlalchemy.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/instrumentation/opentelemetry-instrumentation-sqlalchemy/tests/test_sqlalchemy.py b/instrumentation/opentelemetry-instrumentation-sqlalchemy/tests/test_sqlalchemy.py index cfdfde4e71..31421b49f3 100644 --- a/instrumentation/opentelemetry-instrumentation-sqlalchemy/tests/test_sqlalchemy.py +++ b/instrumentation/opentelemetry-instrumentation-sqlalchemy/tests/test_sqlalchemy.py @@ -260,6 +260,7 @@ def test_no_op_tracer_provider(self): engine=engine, tracer_provider=trace.NoOpTracerProvider, ) - engine.connect() + cnx = engine.connect() + cnx.execute("SELECT 1 + 1;").fetchall() spans = self.memory_exporter.get_finished_spans() self.assertEqual(len(spans), 0) From c5cb8853535dec3aeacbf267402e119f533c6b53 Mon Sep 17 00:00:00 2001 From: avzis Date: Wed, 21 Dec 2022 16:44:22 +0200 Subject: [PATCH 3/7] refactor --- .../tests/test_sqlalchemy.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/instrumentation/opentelemetry-instrumentation-sqlalchemy/tests/test_sqlalchemy.py b/instrumentation/opentelemetry-instrumentation-sqlalchemy/tests/test_sqlalchemy.py index 31421b49f3..7f0e655a9e 100644 --- a/instrumentation/opentelemetry-instrumentation-sqlalchemy/tests/test_sqlalchemy.py +++ b/instrumentation/opentelemetry-instrumentation-sqlalchemy/tests/test_sqlalchemy.py @@ -261,6 +261,6 @@ def test_no_op_tracer_provider(self): tracer_provider=trace.NoOpTracerProvider, ) cnx = engine.connect() - cnx.execute("SELECT 1 + 1;").fetchall() + cnx.execute("SELECT 1 + 1;").fetchall() spans = self.memory_exporter.get_finished_spans() self.assertEqual(len(spans), 0) From a3b4272d335585c88ce22562444501313e0b7b62 Mon Sep 17 00:00:00 2001 From: avzis Date: Thu, 29 Dec 2022 10:41:44 +0200 Subject: [PATCH 4/7] pymongo - capture collection name --- CHANGELOG.md | 3 ++- .../src/opentelemetry/instrumentation/pymongo/__init__.py | 3 +++ .../tests/pymongo/test_pymongo_functional.py | 3 +++ 3 files changed, 8 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9bd207b86c..2bd2657a1c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,7 +14,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - `opentelemetry-instrumentation-aws-lambda` Adds an option to configure `disable_aws_context_propagation` by environment variable: `OTEL_LAMBDA_DISABLE_AWS_CONTEXT_PROPAGATION` ([#1507](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1507)) - +- Fix pymongo to collect the property DB_MONGODB_COLLECTION + ([#1507](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1507)) ## Version 1.15.0/0.36b0 (2022-12-10) diff --git a/instrumentation/opentelemetry-instrumentation-pymongo/src/opentelemetry/instrumentation/pymongo/__init__.py b/instrumentation/opentelemetry-instrumentation-pymongo/src/opentelemetry/instrumentation/pymongo/__init__.py index 4e9ae8a14b..6d898dcc4c 100644 --- a/instrumentation/opentelemetry-instrumentation-pymongo/src/opentelemetry/instrumentation/pymongo/__init__.py +++ b/instrumentation/opentelemetry-instrumentation-pymongo/src/opentelemetry/instrumentation/pymongo/__init__.py @@ -126,6 +126,7 @@ def started(self, event: monitoring.CommandStartedEvent): statement = event.command_name if command: statement += " " + str(command) + collection = event.command.get(event.command_name) try: span = self._tracer.start_span(name, kind=SpanKind.CLIENT) @@ -135,6 +136,8 @@ def started(self, event: monitoring.CommandStartedEvent): ) span.set_attribute(SpanAttributes.DB_NAME, event.database_name) span.set_attribute(SpanAttributes.DB_STATEMENT, statement) + if collection: + span.set_attribute(SpanAttributes.DB_MONGODB_COLLECTION, collection) if event.connection_id is not None: span.set_attribute( SpanAttributes.NET_PEER_NAME, event.connection_id[0] diff --git a/tests/opentelemetry-docker-tests/tests/pymongo/test_pymongo_functional.py b/tests/opentelemetry-docker-tests/tests/pymongo/test_pymongo_functional.py index 92c694953b..c802051021 100644 --- a/tests/opentelemetry-docker-tests/tests/pymongo/test_pymongo_functional.py +++ b/tests/opentelemetry-docker-tests/tests/pymongo/test_pymongo_functional.py @@ -68,6 +68,9 @@ def validate_spans(self): self.assertEqual( pymongo_span.attributes[SpanAttributes.NET_PEER_PORT], MONGODB_PORT ) + self.assertEqual( + pymongo_span.attributes[SpanAttributes.DB_MONGODB_COLLECTION], MONGODB_COLLECTION_NAME + ) def test_insert(self): """Should create a child span for insert""" From 9c64167fd1954397e95bf26fcfac53a23c8ec0f2 Mon Sep 17 00:00:00 2001 From: avzis Date: Thu, 29 Dec 2022 10:45:30 +0200 Subject: [PATCH 5/7] refactor --- CHANGELOG.md | 2 +- .../tests/test_sqlalchemy.py | 10 ---------- 2 files changed, 1 insertion(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2bd2657a1c..e1f6b1ef07 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,7 +15,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 environment variable: `OTEL_LAMBDA_DISABLE_AWS_CONTEXT_PROPAGATION` ([#1507](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1507)) - Fix pymongo to collect the property DB_MONGODB_COLLECTION - ([#1507](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1507)) + ([#1555](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1555)) ## Version 1.15.0/0.36b0 (2022-12-10) diff --git a/instrumentation/opentelemetry-instrumentation-sqlalchemy/tests/test_sqlalchemy.py b/instrumentation/opentelemetry-instrumentation-sqlalchemy/tests/test_sqlalchemy.py index 7f0e655a9e..52fb1a94fa 100644 --- a/instrumentation/opentelemetry-instrumentation-sqlalchemy/tests/test_sqlalchemy.py +++ b/instrumentation/opentelemetry-instrumentation-sqlalchemy/tests/test_sqlalchemy.py @@ -254,13 +254,3 @@ def test_uninstrument(self): spans = self.memory_exporter.get_finished_spans() self.assertEqual(len(spans), 0) - def test_no_op_tracer_provider(self): - engine = create_engine("sqlite:///:memory:") - SQLAlchemyInstrumentor().instrument( - engine=engine, - tracer_provider=trace.NoOpTracerProvider, - ) - cnx = engine.connect() - cnx.execute("SELECT 1 + 1;").fetchall() - spans = self.memory_exporter.get_finished_spans() - self.assertEqual(len(spans), 0) From 91ff1230939e344e1fa8504d889e64d5bee788ae Mon Sep 17 00:00:00 2001 From: avzis Date: Thu, 29 Dec 2022 13:45:32 +0200 Subject: [PATCH 6/7] lint --- .../src/opentelemetry/instrumentation/pymongo/__init__.py | 3 ++- .../tests/test_sqlalchemy.py | 1 - .../tests/pymongo/test_pymongo_functional.py | 3 ++- 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/instrumentation/opentelemetry-instrumentation-pymongo/src/opentelemetry/instrumentation/pymongo/__init__.py b/instrumentation/opentelemetry-instrumentation-pymongo/src/opentelemetry/instrumentation/pymongo/__init__.py index 6d898dcc4c..696c1b54a5 100644 --- a/instrumentation/opentelemetry-instrumentation-pymongo/src/opentelemetry/instrumentation/pymongo/__init__.py +++ b/instrumentation/opentelemetry-instrumentation-pymongo/src/opentelemetry/instrumentation/pymongo/__init__.py @@ -137,7 +137,8 @@ def started(self, event: monitoring.CommandStartedEvent): span.set_attribute(SpanAttributes.DB_NAME, event.database_name) span.set_attribute(SpanAttributes.DB_STATEMENT, statement) if collection: - span.set_attribute(SpanAttributes.DB_MONGODB_COLLECTION, collection) + span.set_attribute( + SpanAttributes.DB_MONGODB_COLLECTION, collection) if event.connection_id is not None: span.set_attribute( SpanAttributes.NET_PEER_NAME, event.connection_id[0] diff --git a/instrumentation/opentelemetry-instrumentation-sqlalchemy/tests/test_sqlalchemy.py b/instrumentation/opentelemetry-instrumentation-sqlalchemy/tests/test_sqlalchemy.py index 52fb1a94fa..099c088f64 100644 --- a/instrumentation/opentelemetry-instrumentation-sqlalchemy/tests/test_sqlalchemy.py +++ b/instrumentation/opentelemetry-instrumentation-sqlalchemy/tests/test_sqlalchemy.py @@ -253,4 +253,3 @@ def test_uninstrument(self): cnx2.execute("SELECT 2 + 2;").fetchall() spans = self.memory_exporter.get_finished_spans() self.assertEqual(len(spans), 0) - diff --git a/tests/opentelemetry-docker-tests/tests/pymongo/test_pymongo_functional.py b/tests/opentelemetry-docker-tests/tests/pymongo/test_pymongo_functional.py index c802051021..e8158c0657 100644 --- a/tests/opentelemetry-docker-tests/tests/pymongo/test_pymongo_functional.py +++ b/tests/opentelemetry-docker-tests/tests/pymongo/test_pymongo_functional.py @@ -69,7 +69,8 @@ def validate_spans(self): pymongo_span.attributes[SpanAttributes.NET_PEER_PORT], MONGODB_PORT ) self.assertEqual( - pymongo_span.attributes[SpanAttributes.DB_MONGODB_COLLECTION], MONGODB_COLLECTION_NAME + pymongo_span.attributes[SpanAttributes.DB_MONGODB_COLLECTION], + MONGODB_COLLECTION_NAME, ) def test_insert(self): From 8d2781dbbac812bd759a11ce2fcb859c35476f7c Mon Sep 17 00:00:00 2001 From: avzis Date: Thu, 29 Dec 2022 14:37:21 +0200 Subject: [PATCH 7/7] lint --- .../src/opentelemetry/instrumentation/pymongo/__init__.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/instrumentation/opentelemetry-instrumentation-pymongo/src/opentelemetry/instrumentation/pymongo/__init__.py b/instrumentation/opentelemetry-instrumentation-pymongo/src/opentelemetry/instrumentation/pymongo/__init__.py index 696c1b54a5..65315b2be5 100644 --- a/instrumentation/opentelemetry-instrumentation-pymongo/src/opentelemetry/instrumentation/pymongo/__init__.py +++ b/instrumentation/opentelemetry-instrumentation-pymongo/src/opentelemetry/instrumentation/pymongo/__init__.py @@ -138,7 +138,8 @@ def started(self, event: monitoring.CommandStartedEvent): span.set_attribute(SpanAttributes.DB_STATEMENT, statement) if collection: span.set_attribute( - SpanAttributes.DB_MONGODB_COLLECTION, collection) + SpanAttributes.DB_MONGODB_COLLECTION, collection + ) if event.connection_id is not None: span.set_attribute( SpanAttributes.NET_PEER_NAME, event.connection_id[0]