diff --git a/.gitignore b/.gitignore index fa3e412d8..92e9ccd05 100644 --- a/.gitignore +++ b/.gitignore @@ -16,6 +16,7 @@ var sdist develop-eggs .installed.cfg +pyvenv.cfg lib lib64 __pycache__ diff --git a/instrumentation/opentelemetry-instrumentation-aiopg/src/opentelemetry/instrumentation/aiopg/aiopg_integration.py b/instrumentation/opentelemetry-instrumentation-aiopg/src/opentelemetry/instrumentation/aiopg/aiopg_integration.py index 14f986da0..982423756 100644 --- a/instrumentation/opentelemetry-instrumentation-aiopg/src/opentelemetry/instrumentation/aiopg/aiopg_integration.py +++ b/instrumentation/opentelemetry-instrumentation-aiopg/src/opentelemetry/instrumentation/aiopg/aiopg_integration.py @@ -101,9 +101,16 @@ class AsyncTracedCursor(TracedCursor): *args: typing.Tuple[typing.Any, typing.Any], **kwargs: typing.Dict[typing.Any, typing.Any] ): + name = "" + if len(args) > 0 and args[0]: + name = args[0] + elif self._db_api_integration.database: + name = self._db_api_integration.database + else: + name = self._db_api_integration.name with self._db_api_integration.get_tracer().start_as_current_span( - self._db_api_integration.name, kind=SpanKind.CLIENT + name, kind=SpanKind.CLIENT ) as span: self._populate_span(span, *args) try: diff --git a/instrumentation/opentelemetry-instrumentation-aiopg/tests/test_aiopg_integration.py b/instrumentation/opentelemetry-instrumentation-aiopg/tests/test_aiopg_integration.py index 89e7cc05a..c6f771e1b 100644 --- a/instrumentation/opentelemetry-instrumentation-aiopg/tests/test_aiopg_integration.py +++ b/instrumentation/opentelemetry-instrumentation-aiopg/tests/test_aiopg_integration.py @@ -215,12 +215,12 @@ class TestAiopgIntegration(TestBase): spans_list = self.memory_exporter.get_finished_spans() self.assertEqual(len(spans_list), 1) span = spans_list[0] - self.assertEqual(span.name, "testcomponent.testdatabase") + self.assertEqual(span.name, "Test query") self.assertIs(span.kind, trace_api.SpanKind.CLIENT) self.assertEqual(span.attributes["component"], "testcomponent") - self.assertEqual(span.attributes["db.type"], "testtype") - self.assertEqual(span.attributes["db.instance"], "testdatabase") + self.assertEqual(span.attributes["db.system"], "testcomponent") + self.assertEqual(span.attributes["db.name"], "testdatabase") self.assertEqual(span.attributes["db.statement"], "Test query") self.assertEqual( span.attributes["db.statement.parameters"], @@ -230,7 +230,7 @@ class TestAiopgIntegration(TestBase): self.assertEqual(span.attributes["net.peer.name"], "testhost") self.assertEqual(span.attributes["net.peer.port"], 123) self.assertIs( - span.status.status_code, trace_api.status.StatusCode.UNSET, + span.status.status_code, trace_api.status.StatusCode.UNSET ) def test_span_not_recording(self): @@ -281,7 +281,7 @@ class TestAiopgIntegration(TestBase): span = spans_list[0] self.assertEqual(span.attributes["db.statement"], "Test query") self.assertIs( - span.status.status_code, trace_api.status.StatusCode.ERROR, + span.status.status_code, trace_api.status.StatusCode.ERROR ) self.assertEqual(span.status.description, "Test Exception") diff --git a/instrumentation/opentelemetry-instrumentation-dbapi/CHANGELOG.md b/instrumentation/opentelemetry-instrumentation-dbapi/CHANGELOG.md index 37f59c018..0c13891d8 100644 --- a/instrumentation/opentelemetry-instrumentation-dbapi/CHANGELOG.md +++ b/instrumentation/opentelemetry-instrumentation-dbapi/CHANGELOG.md @@ -2,7 +2,10 @@ ## Unreleased -Stop capturing query parameters by default +- Update dbapi and its dependant instrumentations to follow semantic conventions + ([#195](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/195)) + +- Stop capturing query parameters by default ([#156](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/156)) ## Version 0.13b0 diff --git a/instrumentation/opentelemetry-instrumentation-dbapi/src/opentelemetry/instrumentation/dbapi/__init__.py b/instrumentation/opentelemetry-instrumentation-dbapi/src/opentelemetry/instrumentation/dbapi/__init__.py index 5b120d3de..197f4ade4 100644 --- a/instrumentation/opentelemetry-instrumentation-dbapi/src/opentelemetry/instrumentation/dbapi/__init__.py +++ b/instrumentation/opentelemetry-instrumentation-dbapi/src/opentelemetry/instrumentation/dbapi/__init__.py @@ -62,19 +62,19 @@ def trace_integration( capture_parameters: bool = False, ): """Integrate with DB API library. - https://www.python.org/dev/peps/pep-0249/ + https://www.python.org/dev/peps/pep-0249/ - Args: - connect_module: Module name where connect method is available. - connect_method_name: The connect method name. - database_component: Database driver name or database name "JDBI", - "jdbc", "odbc", "postgreSQL". - database_type: The Database type. For any SQL database, "sql". - connection_attributes: Attribute names for database, port, host and - user in Connection object. - tracer_provider: The :class:`opentelemetry.trace.TracerProvider` to - use. If ommited the current configured one is used. - capture_parameters: Configure if db.statement.parameters should be captured. + Args: + connect_module: Module name where connect method is available. + connect_method_name: The connect method name. + database_component: Database driver name or database name "JDBI", + "jdbc", "odbc", "postgreSQL". + database_type: The Database type. For any SQL database, "sql". + connection_attributes: Attribute names for database, port, host and + user in Connection object. + tracer_provider: The :class:`opentelemetry.trace.TracerProvider` to + use. If ommited the current configured one is used. + capture_parameters: Configure if db.statement.parameters should be captured. """ wrap_connect( __name__, @@ -101,18 +101,18 @@ def wrap_connect( capture_parameters: bool = False, ): """Integrate with DB API library. - https://www.python.org/dev/peps/pep-0249/ + https://www.python.org/dev/peps/pep-0249/ - Args: - tracer: The :class:`opentelemetry.trace.Tracer` to use. - connect_module: Module name where connect method is available. - connect_method_name: The connect method name. - database_component: Database driver name or database name "JDBI", - "jdbc", "odbc", "postgreSQL". - database_type: The Database type. For any SQL database, "sql". - connection_attributes: Attribute names for database, port, host and - user in Connection object. - capture_parameters: Configure if db.statement.parameters should be captured. + Args: + tracer: The :class:`opentelemetry.trace.Tracer` to use. + connect_module: Module name where connect method is available. + connect_method_name: The connect method name. + database_component: Database driver name or database name "JDBI", + "jdbc", "odbc", "postgreSQL". + database_type: The Database type. For any SQL database, "sql". + connection_attributes: Attribute names for database, port, host and + user in Connection object. + capture_parameters: Configure if db.statement.parameters should be captured. """ @@ -143,14 +143,14 @@ def wrap_connect( def unwrap_connect( - connect_module: typing.Callable[..., typing.Any], connect_method_name: str, + connect_module: typing.Callable[..., typing.Any], connect_method_name: str ): """Disable integration with DB API library. - https://www.python.org/dev/peps/pep-0249/ + https://www.python.org/dev/peps/pep-0249/ - Args: - connect_module: Module name where the connect method is available. - connect_method_name: The connect method name. + Args: + connect_module: Module name where the connect method is available. + connect_method_name: The connect method name. """ unwrap(connect_module, connect_method_name) @@ -251,8 +251,7 @@ class DatabaseApiIntegration: args: typing.Tuple[typing.Any, typing.Any], kwargs: typing.Dict[typing.Any, typing.Any], ): - """Add object proxy to connection object. - """ + """Add object proxy to connection object.""" connection = connect_method(*args, **kwargs) self.get_connection_attributes(connection) return get_traced_connection_proxy(connection, self) @@ -278,6 +277,9 @@ class DatabaseApiIntegration: self.database = self.database.decode(errors="ignore") self.name += "." + self.database user = self.connection_props.get("user") + # PyMySQL encodes this data + if user and isinstance(user, bytes): + user = user.decode() if user is not None: self.span_attributes["db.user"] = str(user) host = self.connection_props.get("host") @@ -325,8 +327,10 @@ class TracedCursor: span.set_attribute( "component", self._db_api_integration.database_component ) - span.set_attribute("db.type", self._db_api_integration.database_type) - span.set_attribute("db.instance", self._db_api_integration.database) + span.set_attribute( + "db.system", self._db_api_integration.database_component + ) + span.set_attribute("db.name", self._db_api_integration.database) span.set_attribute("db.statement", statement) for ( @@ -344,9 +348,16 @@ class TracedCursor: *args: typing.Tuple[typing.Any, typing.Any], **kwargs: typing.Dict[typing.Any, typing.Any] ): + name = "" + if args: + name = args[0] + elif self._db_api_integration.database: + name = self._db_api_integration.database + else: + name = self._db_api_integration.name with self._db_api_integration.get_tracer().start_as_current_span( - self._db_api_integration.name, kind=SpanKind.CLIENT + name, kind=SpanKind.CLIENT ) as span: self._populate_span(span, *args) try: diff --git a/instrumentation/opentelemetry-instrumentation-dbapi/tests/test_dbapi_integration.py b/instrumentation/opentelemetry-instrumentation-dbapi/tests/test_dbapi_integration.py index 40176363c..e69bf60c9 100644 --- a/instrumentation/opentelemetry-instrumentation-dbapi/tests/test_dbapi_integration.py +++ b/instrumentation/opentelemetry-instrumentation-dbapi/tests/test_dbapi_integration.py @@ -50,19 +50,19 @@ class TestDBApiIntegration(TestBase): spans_list = self.memory_exporter.get_finished_spans() self.assertEqual(len(spans_list), 1) span = spans_list[0] - self.assertEqual(span.name, "testcomponent.testdatabase") + self.assertEqual(span.name, "Test query") self.assertIs(span.kind, trace_api.SpanKind.CLIENT) self.assertEqual(span.attributes["component"], "testcomponent") - self.assertEqual(span.attributes["db.type"], "testtype") - self.assertEqual(span.attributes["db.instance"], "testdatabase") + self.assertEqual(span.attributes["db.system"], "testcomponent") + self.assertEqual(span.attributes["db.name"], "testdatabase") self.assertEqual(span.attributes["db.statement"], "Test query") self.assertFalse("db.statement.parameters" in span.attributes) self.assertEqual(span.attributes["db.user"], "testuser") self.assertEqual(span.attributes["net.peer.name"], "testhost") self.assertEqual(span.attributes["net.peer.port"], 123) self.assertIs( - span.status.status_code, trace_api.status.StatusCode.UNSET, + span.status.status_code, trace_api.status.StatusCode.UNSET ) def test_span_succeeded_with_capture_of_statement_parameters(self): @@ -93,12 +93,12 @@ class TestDBApiIntegration(TestBase): spans_list = self.memory_exporter.get_finished_spans() self.assertEqual(len(spans_list), 1) span = spans_list[0] - self.assertEqual(span.name, "testcomponent.testdatabase") + self.assertEqual(span.name, "Test query") self.assertIs(span.kind, trace_api.SpanKind.CLIENT) self.assertEqual(span.attributes["component"], "testcomponent") - self.assertEqual(span.attributes["db.type"], "testtype") - self.assertEqual(span.attributes["db.instance"], "testdatabase") + self.assertEqual(span.attributes["db.system"], "testcomponent") + self.assertEqual(span.attributes["db.name"], "testdatabase") self.assertEqual(span.attributes["db.statement"], "Test query") self.assertEqual( span.attributes["db.statement.parameters"], @@ -108,7 +108,7 @@ class TestDBApiIntegration(TestBase): self.assertEqual(span.attributes["net.peer.name"], "testhost") self.assertEqual(span.attributes["net.peer.port"], 123) self.assertIs( - span.status.status_code, trace_api.status.StatusCode.UNSET, + span.status.status_code, trace_api.status.StatusCode.UNSET ) def test_span_not_recording(self): @@ -159,7 +159,7 @@ class TestDBApiIntegration(TestBase): span = spans_list[0] self.assertEqual(span.attributes["db.statement"], "Test query") self.assertIs( - span.status.status_code, trace_api.status.StatusCode.ERROR, + span.status.status_code, trace_api.status.StatusCode.ERROR ) self.assertEqual(span.status.description, "Test Exception") diff --git a/instrumentation/opentelemetry-instrumentation-sqlite3/src/opentelemetry/instrumentation/sqlite3/__init__.py b/instrumentation/opentelemetry-instrumentation-sqlite3/src/opentelemetry/instrumentation/sqlite3/__init__.py index 986b0c621..bad033b29 100644 --- a/instrumentation/opentelemetry-instrumentation-sqlite3/src/opentelemetry/instrumentation/sqlite3/__init__.py +++ b/instrumentation/opentelemetry-instrumentation-sqlite3/src/opentelemetry/instrumentation/sqlite3/__init__.py @@ -51,7 +51,7 @@ class SQLite3Instrumentor(BaseInstrumentor): # No useful attributes of sqlite3 connection object _CONNECTION_ATTRIBUTES = {} - _DATABASE_COMPONENT = "sqlite3" + _DATABASE_COMPONENT = "sqlite" _DATABASE_TYPE = "sql" def _instrument(self, **kwargs): diff --git a/instrumentation/opentelemetry-instrumentation-sqlite3/tests/test_sqlite3.py b/instrumentation/opentelemetry-instrumentation-sqlite3/tests/test_sqlite3.py index 0e385cf3e..a4fc88706 100644 --- a/instrumentation/opentelemetry-instrumentation-sqlite3/tests/test_sqlite3.py +++ b/instrumentation/opentelemetry-instrumentation-sqlite3/tests/test_sqlite3.py @@ -37,7 +37,7 @@ class TestSQLite3(TestBase): if cls._connection: cls._connection.close() - def validate_spans(self): + def validate_spans(self, span_name): spans = self.memory_exporter.get_finished_spans() self.assertEqual(len(spans), 2) for span in spans: @@ -50,34 +50,30 @@ class TestSQLite3(TestBase): self.assertIsNotNone(root_span) self.assertIsNotNone(child_span) self.assertEqual(root_span.name, "rootSpan") - self.assertEqual(child_span.name, "sqlite3") + self.assertEqual(child_span.name, span_name) self.assertIsNotNone(child_span.parent) self.assertIs(child_span.parent, root_span.get_span_context()) self.assertIs(child_span.kind, trace_api.SpanKind.CLIENT) def test_execute(self): - """Should create a child span for execute method - """ + """Should create a child span for execute method""" + stmt = "CREATE TABLE IF NOT EXISTS test (id integer)" with self._tracer.start_as_current_span("rootSpan"): - self._cursor.execute( - "CREATE TABLE IF NOT EXISTS test (id integer)" - ) - self.validate_spans() + self._cursor.execute(stmt) + self.validate_spans(stmt) def test_executemany(self): - """Should create a child span for executemany - """ + """Should create a child span for executemany""" + stmt = "INSERT INTO test (id) VALUES (?)" with self._tracer.start_as_current_span("rootSpan"): data = [("1",), ("2",), ("3",)] - stmt = "INSERT INTO test (id) VALUES (?)" self._cursor.executemany(stmt, data) - self.validate_spans() + self.validate_spans(stmt) def test_callproc(self): - """Should create a child span for callproc - """ + """Should create a child span for callproc""" with self._tracer.start_as_current_span("rootSpan"), self.assertRaises( Exception ): self._cursor.callproc("test", ()) - self.validate_spans() + self.validate_spans("test") diff --git a/tests/opentelemetry-docker-tests/tests/check_availability.py b/tests/opentelemetry-docker-tests/tests/check_availability.py index 308257219..0e066610c 100644 --- a/tests/opentelemetry-docker-tests/tests/check_availability.py +++ b/tests/opentelemetry-docker-tests/tests/check_availability.py @@ -24,16 +24,16 @@ MONGODB_COLLECTION_NAME = "test" MONGODB_DB_NAME = os.getenv("MONGODB_DB_NAME", "opentelemetry-tests") MONGODB_HOST = os.getenv("MONGODB_HOST", "localhost") MONGODB_PORT = int(os.getenv("MONGODB_PORT", "27017")) -MYSQL_DB_NAME = os.getenv("MYSQL_DB_NAME ", "opentelemetry-tests") -MYSQL_HOST = os.getenv("MYSQL_HOST ", "localhost") -MYSQL_PORT = int(os.getenv("MYSQL_PORT ", "3306")) -MYSQL_USER = os.getenv("MYSQL_USER ", "testuser") -MYSQL_PASSWORD = os.getenv("MYSQL_PASSWORD ", "testpassword") +MYSQL_DB_NAME = os.getenv("MYSQL_DB_NAME", "opentelemetry-tests") +MYSQL_HOST = os.getenv("MYSQL_HOST", "localhost") +MYSQL_PORT = int(os.getenv("MYSQL_PORT", "3306")) +MYSQL_USER = os.getenv("MYSQL_USER", "testuser") +MYSQL_PASSWORD = os.getenv("MYSQL_PASSWORD", "testpassword") POSTGRES_DB_NAME = os.getenv("POSTGRESQL_DB_NAME", "opentelemetry-tests") POSTGRES_HOST = os.getenv("POSTGRESQL_HOST", "localhost") -POSTGRES_PASSWORD = os.getenv("POSTGRESQL_HOST", "testpassword") +POSTGRES_PASSWORD = os.getenv("POSTGRESQL_PASSWORD", "testpassword") POSTGRES_PORT = int(os.getenv("POSTGRESQL_PORT", "5432")) -POSTGRES_USER = os.getenv("POSTGRESQL_HOST", "testuser") +POSTGRES_USER = os.getenv("POSTGRESQL_USER", "testuser") REDIS_HOST = os.getenv("REDIS_HOST", "localhost") REDIS_PORT = int(os.getenv("REDIS_PORT ", "6379")) RETRY_COUNT = 8 diff --git a/tests/opentelemetry-docker-tests/tests/mysql/test_mysql_functional.py b/tests/opentelemetry-docker-tests/tests/mysql/test_mysql_functional.py index fc237fe12..ec6eed313 100644 --- a/tests/opentelemetry-docker-tests/tests/mysql/test_mysql_functional.py +++ b/tests/opentelemetry-docker-tests/tests/mysql/test_mysql_functional.py @@ -20,11 +20,11 @@ from opentelemetry import trace as trace_api from opentelemetry.instrumentation.mysql import MySQLInstrumentor from opentelemetry.test.test_base import TestBase -MYSQL_USER = os.getenv("MYSQL_USER ", "testuser") -MYSQL_PASSWORD = os.getenv("MYSQL_PASSWORD ", "testpassword") -MYSQL_HOST = os.getenv("MYSQL_HOST ", "localhost") -MYSQL_PORT = int(os.getenv("MYSQL_PORT ", "3306")) -MYSQL_DB_NAME = os.getenv("MYSQL_DB_NAME ", "opentelemetry-tests") +MYSQL_USER = os.getenv("MYSQL_USER", "testuser") +MYSQL_PASSWORD = os.getenv("MYSQL_PASSWORD", "testpassword") +MYSQL_HOST = os.getenv("MYSQL_HOST", "localhost") +MYSQL_PORT = int(os.getenv("MYSQL_PORT", "3306")) +MYSQL_DB_NAME = os.getenv("MYSQL_DB_NAME", "opentelemetry-tests") class TestFunctionalMysql(TestBase): @@ -53,7 +53,7 @@ class TestFunctionalMysql(TestBase): ) self._cursor = self._connection.cursor() - def validate_spans(self): + def validate_spans(self, span_name): spans = self.memory_exporter.get_finished_spans() self.assertEqual(len(spans), 2) for span in spans: @@ -66,42 +66,47 @@ class TestFunctionalMysql(TestBase): self.assertIsNotNone(root_span) self.assertIsNotNone(db_span) self.assertEqual(root_span.name, "rootSpan") - self.assertEqual(db_span.name, "mysql.opentelemetry-tests") + self.assertEqual(db_span.name, span_name) self.assertIsNotNone(db_span.parent) self.assertIs(db_span.parent, root_span.get_span_context()) self.assertIs(db_span.kind, trace_api.SpanKind.CLIENT) - self.assertEqual(db_span.attributes["db.instance"], MYSQL_DB_NAME) + self.assertEqual(db_span.attributes["db.system"], "mysql") + self.assertEqual(db_span.attributes["db.name"], MYSQL_DB_NAME) + self.assertEqual(db_span.attributes["db.user"], MYSQL_USER) self.assertEqual(db_span.attributes["net.peer.name"], MYSQL_HOST) self.assertEqual(db_span.attributes["net.peer.port"], MYSQL_PORT) def test_execute(self): """Should create a child span for execute""" + stmt = "CREATE TABLE IF NOT EXISTS test (id INT)" with self._tracer.start_as_current_span("rootSpan"): - self._cursor.execute("CREATE TABLE IF NOT EXISTS test (id INT)") - self.validate_spans() + self._cursor.execute(stmt) + self.validate_spans(stmt) def test_execute_with_connection_context_manager(self): """Should create a child span for execute with connection context""" + stmt = "CREATE TABLE IF NOT EXISTS test (id INT)" with self._tracer.start_as_current_span("rootSpan"): with self._connection as conn: cursor = conn.cursor() - cursor.execute("CREATE TABLE IF NOT EXISTS test (id INT)") - self.validate_spans() + cursor.execute(stmt) + self.validate_spans(stmt) def test_execute_with_cursor_context_manager(self): """Should create a child span for execute with cursor context""" + stmt = "CREATE TABLE IF NOT EXISTS test (id INT)" with self._tracer.start_as_current_span("rootSpan"): with self._connection.cursor() as cursor: - cursor.execute("CREATE TABLE IF NOT EXISTS test (id INT)") - self.validate_spans() + cursor.execute(stmt) + self.validate_spans(stmt) def test_executemany(self): """Should create a child span for executemany""" + stmt = "INSERT INTO test (id) VALUES (%s)" with self._tracer.start_as_current_span("rootSpan"): data = (("1",), ("2",), ("3",)) - stmt = "INSERT INTO test (id) VALUES (%s)" self._cursor.executemany(stmt, data) - self.validate_spans() + self.validate_spans(stmt) def test_callproc(self): """Should create a child span for callproc""" @@ -109,4 +114,4 @@ class TestFunctionalMysql(TestBase): Exception ): self._cursor.callproc("test", ()) - self.validate_spans() + self.validate_spans("test") diff --git a/tests/opentelemetry-docker-tests/tests/postgres/test_aiopg_functional.py b/tests/opentelemetry-docker-tests/tests/postgres/test_aiopg_functional.py index d76cd702e..030aecc66 100644 --- a/tests/opentelemetry-docker-tests/tests/postgres/test_aiopg_functional.py +++ b/tests/opentelemetry-docker-tests/tests/postgres/test_aiopg_functional.py @@ -22,11 +22,11 @@ from opentelemetry import trace as trace_api from opentelemetry.instrumentation.aiopg import AiopgInstrumentor from opentelemetry.test.test_base import TestBase -POSTGRES_HOST = os.getenv("POSTGRESQL_HOST ", "localhost") -POSTGRES_PORT = int(os.getenv("POSTGRESQL_PORT ", "5432")) -POSTGRES_DB_NAME = os.getenv("POSTGRESQL_DB_NAME ", "opentelemetry-tests") -POSTGRES_PASSWORD = os.getenv("POSTGRESQL_HOST ", "testpassword") -POSTGRES_USER = os.getenv("POSTGRESQL_HOST ", "testuser") +POSTGRES_HOST = os.getenv("POSTGRESQL_HOST", "localhost") +POSTGRES_PORT = int(os.getenv("POSTGRESQL_PORT", "5432")) +POSTGRES_DB_NAME = os.getenv("POSTGRESQL_DB_NAME", "opentelemetry-tests") +POSTGRES_PASSWORD = os.getenv("POSTGRESQL_PASSWORD", "testpassword") +POSTGRES_USER = os.getenv("POSTGRESQL_USER", "testuser") def async_call(coro): @@ -61,7 +61,7 @@ class TestFunctionalAiopgConnect(TestBase): cls._connection.close() AiopgInstrumentor().uninstrument() - def validate_spans(self): + def validate_spans(self, span_name): spans = self.memory_exporter.get_finished_spans() self.assertEqual(len(spans), 2) for span in spans: @@ -74,34 +74,31 @@ class TestFunctionalAiopgConnect(TestBase): self.assertIsNotNone(root_span) self.assertIsNotNone(child_span) self.assertEqual(root_span.name, "rootSpan") - self.assertEqual(child_span.name, "postgresql.opentelemetry-tests") + self.assertEqual(child_span.name, span_name) self.assertIsNotNone(child_span.parent) self.assertIs(child_span.parent, root_span.get_span_context()) self.assertIs(child_span.kind, trace_api.SpanKind.CLIENT) - self.assertEqual( - child_span.attributes["db.instance"], POSTGRES_DB_NAME - ) + self.assertEqual(child_span.attributes["db.system"], "postgresql") + self.assertEqual(child_span.attributes["db.name"], POSTGRES_DB_NAME) + self.assertEqual(child_span.attributes["db.user"], POSTGRES_USER) self.assertEqual(child_span.attributes["net.peer.name"], POSTGRES_HOST) self.assertEqual(child_span.attributes["net.peer.port"], POSTGRES_PORT) def test_execute(self): """Should create a child span for execute method""" + stmt = "CREATE TABLE IF NOT EXISTS test (id integer)" with self._tracer.start_as_current_span("rootSpan"): - async_call( - self._cursor.execute( - "CREATE TABLE IF NOT EXISTS test (id integer)" - ) - ) - self.validate_spans() + async_call(self._cursor.execute(stmt)) + self.validate_spans(stmt) def test_executemany(self): """Should create a child span for executemany""" + stmt = "INSERT INTO test (id) VALUES (%s)" with pytest.raises(psycopg2.ProgrammingError): with self._tracer.start_as_current_span("rootSpan"): data = (("1",), ("2",), ("3",)) - stmt = "INSERT INTO test (id) VALUES (%s)" async_call(self._cursor.executemany(stmt, data)) - self.validate_spans() + self.validate_spans(stmt) def test_callproc(self): """Should create a child span for callproc""" @@ -109,7 +106,7 @@ class TestFunctionalAiopgConnect(TestBase): Exception ): async_call(self._cursor.callproc("test", ())) - self.validate_spans() + self.validate_spans("test") class TestFunctionalAiopgCreatePool(TestBase): @@ -142,7 +139,7 @@ class TestFunctionalAiopgCreatePool(TestBase): cls._pool.close() AiopgInstrumentor().uninstrument() - def validate_spans(self): + def validate_spans(self, span_name): spans = self.memory_exporter.get_finished_spans() self.assertEqual(len(spans), 2) for span in spans: @@ -155,34 +152,31 @@ class TestFunctionalAiopgCreatePool(TestBase): self.assertIsNotNone(root_span) self.assertIsNotNone(child_span) self.assertEqual(root_span.name, "rootSpan") - self.assertEqual(child_span.name, "postgresql.opentelemetry-tests") + self.assertEqual(child_span.name, span_name) self.assertIsNotNone(child_span.parent) self.assertIs(child_span.parent, root_span.get_span_context()) self.assertIs(child_span.kind, trace_api.SpanKind.CLIENT) - self.assertEqual( - child_span.attributes["db.instance"], POSTGRES_DB_NAME - ) + self.assertEqual(child_span.attributes["db.system"], "postgresql") + self.assertEqual(child_span.attributes["db.name"], POSTGRES_DB_NAME) + self.assertEqual(child_span.attributes["db.user"], POSTGRES_USER) self.assertEqual(child_span.attributes["net.peer.name"], POSTGRES_HOST) self.assertEqual(child_span.attributes["net.peer.port"], POSTGRES_PORT) def test_execute(self): """Should create a child span for execute method""" + stmt = "CREATE TABLE IF NOT EXISTS test (id integer)" with self._tracer.start_as_current_span("rootSpan"): - async_call( - self._cursor.execute( - "CREATE TABLE IF NOT EXISTS test (id integer)" - ) - ) - self.validate_spans() + async_call(self._cursor.execute(stmt)) + self.validate_spans(stmt) def test_executemany(self): """Should create a child span for executemany""" + stmt = "INSERT INTO test (id) VALUES (%s)" with pytest.raises(psycopg2.ProgrammingError): with self._tracer.start_as_current_span("rootSpan"): data = (("1",), ("2",), ("3",)) - stmt = "INSERT INTO test (id) VALUES (%s)" async_call(self._cursor.executemany(stmt, data)) - self.validate_spans() + self.validate_spans(stmt) def test_callproc(self): """Should create a child span for callproc""" @@ -190,4 +184,4 @@ class TestFunctionalAiopgCreatePool(TestBase): Exception ): async_call(self._cursor.callproc("test", ())) - self.validate_spans() + self.validate_spans("test") diff --git a/tests/opentelemetry-docker-tests/tests/postgres/test_psycopg_functional.py b/tests/opentelemetry-docker-tests/tests/postgres/test_psycopg_functional.py index 28db4c064..76116dfd2 100644 --- a/tests/opentelemetry-docker-tests/tests/postgres/test_psycopg_functional.py +++ b/tests/opentelemetry-docker-tests/tests/postgres/test_psycopg_functional.py @@ -20,11 +20,11 @@ from opentelemetry import trace as trace_api from opentelemetry.instrumentation.psycopg2 import Psycopg2Instrumentor from opentelemetry.test.test_base import TestBase -POSTGRES_HOST = os.getenv("POSTGRESQL_HOST ", "localhost") -POSTGRES_PORT = int(os.getenv("POSTGRESQL_PORT ", "5432")) -POSTGRES_DB_NAME = os.getenv("POSTGRESQL_DB_NAME ", "opentelemetry-tests") -POSTGRES_PASSWORD = os.getenv("POSTGRESQL_HOST ", "testpassword") -POSTGRES_USER = os.getenv("POSTGRESQL_HOST ", "testuser") +POSTGRES_HOST = os.getenv("POSTGRESQL_HOST", "localhost") +POSTGRES_PORT = int(os.getenv("POSTGRESQL_PORT", "5432")) +POSTGRES_DB_NAME = os.getenv("POSTGRESQL_DB_NAME", "opentelemetry-tests") +POSTGRES_PASSWORD = os.getenv("POSTGRESQL_PASSWORD", "testpassword") +POSTGRES_USER = os.getenv("POSTGRESQL_USER", "testuser") class TestFunctionalPsycopg(TestBase): @@ -53,7 +53,7 @@ class TestFunctionalPsycopg(TestBase): cls._connection.close() Psycopg2Instrumentor().uninstrument() - def validate_spans(self): + def validate_spans(self, span_name): spans = self.memory_exporter.get_finished_spans() self.assertEqual(len(spans), 2) for span in spans: @@ -66,47 +66,48 @@ class TestFunctionalPsycopg(TestBase): self.assertIsNotNone(root_span) self.assertIsNotNone(child_span) self.assertEqual(root_span.name, "rootSpan") - self.assertEqual(child_span.name, "postgresql.opentelemetry-tests") + self.assertEqual(child_span.name, span_name) self.assertIsNotNone(child_span.parent) self.assertIs(child_span.parent, root_span.get_span_context()) self.assertIs(child_span.kind, trace_api.SpanKind.CLIENT) - self.assertEqual( - child_span.attributes["db.instance"], POSTGRES_DB_NAME - ) + self.assertEqual(child_span.attributes["db.system"], "postgresql") + self.assertEqual(child_span.attributes["db.name"], POSTGRES_DB_NAME) + self.assertEqual(child_span.attributes["db.user"], POSTGRES_USER) self.assertEqual(child_span.attributes["net.peer.name"], POSTGRES_HOST) self.assertEqual(child_span.attributes["net.peer.port"], POSTGRES_PORT) def test_execute(self): """Should create a child span for execute method""" + stmt = "CREATE TABLE IF NOT EXISTS test (id integer)" with self._tracer.start_as_current_span("rootSpan"): - self._cursor.execute( - "CREATE TABLE IF NOT EXISTS test (id integer)" - ) - self.validate_spans() + self._cursor.execute(stmt) + self.validate_spans(stmt) def test_execute_with_connection_context_manager(self): """Should create a child span for execute with connection context""" + stmt = "CREATE TABLE IF NOT EXISTS test (id INT)" with self._tracer.start_as_current_span("rootSpan"): with self._connection as conn: cursor = conn.cursor() - cursor.execute("CREATE TABLE IF NOT EXISTS test (id INT)") - self.validate_spans() + cursor.execute(stmt) + self.validate_spans(stmt) def test_execute_with_cursor_context_manager(self): """Should create a child span for execute with cursor context""" + stmt = "CREATE TABLE IF NOT EXISTS test (id INT)" with self._tracer.start_as_current_span("rootSpan"): with self._connection.cursor() as cursor: - cursor.execute("CREATE TABLE IF NOT EXISTS test (id INT)") - self.validate_spans() + cursor.execute(stmt) + self.validate_spans(stmt) self.assertTrue(cursor.closed) def test_executemany(self): """Should create a child span for executemany""" + stmt = "INSERT INTO test (id) VALUES (%s)" with self._tracer.start_as_current_span("rootSpan"): data = (("1",), ("2",), ("3",)) - stmt = "INSERT INTO test (id) VALUES (%s)" self._cursor.executemany(stmt, data) - self.validate_spans() + self.validate_spans(stmt) def test_callproc(self): """Should create a child span for callproc""" @@ -114,4 +115,4 @@ class TestFunctionalPsycopg(TestBase): Exception ): self._cursor.callproc("test", ()) - self.validate_spans() + self.validate_spans("test") diff --git a/tests/opentelemetry-docker-tests/tests/pymysql/test_pymysql_functional.py b/tests/opentelemetry-docker-tests/tests/pymysql/test_pymysql_functional.py index 7c0902555..b8e440480 100644 --- a/tests/opentelemetry-docker-tests/tests/pymysql/test_pymysql_functional.py +++ b/tests/opentelemetry-docker-tests/tests/pymysql/test_pymysql_functional.py @@ -20,11 +20,11 @@ from opentelemetry import trace as trace_api from opentelemetry.instrumentation.pymysql import PyMySQLInstrumentor from opentelemetry.test.test_base import TestBase -MYSQL_USER = os.getenv("MYSQL_USER ", "testuser") -MYSQL_PASSWORD = os.getenv("MYSQL_PASSWORD ", "testpassword") -MYSQL_HOST = os.getenv("MYSQL_HOST ", "localhost") -MYSQL_PORT = int(os.getenv("MYSQL_PORT ", "3306")) -MYSQL_DB_NAME = os.getenv("MYSQL_DB_NAME ", "opentelemetry-tests") +MYSQL_USER = os.getenv("MYSQL_USER", "testuser") +MYSQL_PASSWORD = os.getenv("MYSQL_PASSWORD", "testpassword") +MYSQL_HOST = os.getenv("MYSQL_HOST", "localhost") +MYSQL_PORT = int(os.getenv("MYSQL_PORT", "3306")) +MYSQL_DB_NAME = os.getenv("MYSQL_DB_NAME", "opentelemetry-tests") class TestFunctionalPyMysql(TestBase): @@ -50,7 +50,7 @@ class TestFunctionalPyMysql(TestBase): cls._connection.close() PyMySQLInstrumentor().uninstrument() - def validate_spans(self): + def validate_spans(self, span_name): spans = self.memory_exporter.get_finished_spans() self.assertEqual(len(spans), 2) for span in spans: @@ -63,34 +63,38 @@ class TestFunctionalPyMysql(TestBase): self.assertIsNotNone(root_span) self.assertIsNotNone(db_span) self.assertEqual(root_span.name, "rootSpan") - self.assertEqual(db_span.name, "mysql.opentelemetry-tests") + self.assertEqual(db_span.name, span_name) self.assertIsNotNone(db_span.parent) self.assertIs(db_span.parent, root_span.get_span_context()) self.assertIs(db_span.kind, trace_api.SpanKind.CLIENT) - self.assertEqual(db_span.attributes["db.instance"], MYSQL_DB_NAME) + self.assertEqual(db_span.attributes["db.system"], "mysql") + self.assertEqual(db_span.attributes["db.name"], MYSQL_DB_NAME) + self.assertEqual(db_span.attributes["db.user"], MYSQL_USER) self.assertEqual(db_span.attributes["net.peer.name"], MYSQL_HOST) self.assertEqual(db_span.attributes["net.peer.port"], MYSQL_PORT) def test_execute(self): """Should create a child span for execute""" + stmt = "CREATE TABLE IF NOT EXISTS test (id INT)" with self._tracer.start_as_current_span("rootSpan"): - self._cursor.execute("CREATE TABLE IF NOT EXISTS test (id INT)") - self.validate_spans() + self._cursor.execute(stmt) + self.validate_spans(stmt) def test_execute_with_cursor_context_manager(self): """Should create a child span for execute with cursor context""" + stmt = "CREATE TABLE IF NOT EXISTS test (id INT)" with self._tracer.start_as_current_span("rootSpan"): with self._connection.cursor() as cursor: - cursor.execute("CREATE TABLE IF NOT EXISTS test (id INT)") - self.validate_spans() + cursor.execute(stmt) + self.validate_spans(stmt) def test_executemany(self): """Should create a child span for executemany""" + stmt = "INSERT INTO test (id) VALUES (%s)" with self._tracer.start_as_current_span("rootSpan"): data = (("1",), ("2",), ("3",)) - stmt = "INSERT INTO test (id) VALUES (%s)" self._cursor.executemany(stmt, data) - self.validate_spans() + self.validate_spans(stmt) def test_callproc(self): """Should create a child span for callproc""" @@ -98,4 +102,4 @@ class TestFunctionalPyMysql(TestBase): Exception ): self._cursor.callproc("test", ()) - self.validate_spans() + self.validate_spans("test")