From 2228573bf33a487ae3a1584f23b3f0318e77be0b Mon Sep 17 00:00:00 2001 From: Srikanth Chekuri Date: Wed, 18 Nov 2020 01:41:59 +0530 Subject: [PATCH] Fix docker tests --- .../tests/mysql/test_mysql_functional.py | 39 ++++++------ .../tests/postgres/test_aiopg_functional.py | 60 +++++++++---------- .../tests/postgres/test_psycopg_functional.py | 43 ++++++------- .../tests/pymysql/test_pymysql_functional.py | 34 ++++++----- 4 files changed, 90 insertions(+), 86 deletions(-) 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")