Add span for connection phase (#1134)

This commit is contained in:
shahargl
2022-07-17 18:39:15 +03:00
committed by GitHub
parent 9e2dbecedc
commit 2ce69a668f
9 changed files with 100 additions and 36 deletions

View File

@ -128,8 +128,9 @@ class SQLAlchemyTestMixin(TestBase):
self.session.commit()
spans = self.memory_exporter.get_finished_spans()
self.assertEqual(len(spans), 1)
span = spans[0]
# one span for the connection and one for the query
self.assertEqual(len(spans), 2)
span = spans[1]
stmt = "INSERT INTO players (id, name) VALUES "
if span.attributes.get(SpanAttributes.DB_SYSTEM) == "sqlite":
stmt += "(?, ?)"
@ -148,8 +149,9 @@ class SQLAlchemyTestMixin(TestBase):
self.assertEqual(len(out), 0)
spans = self.memory_exporter.get_finished_spans()
self.assertEqual(len(spans), 1)
span = spans[0]
# one span for the connection and one for the query
self.assertEqual(len(spans), 2)
span = spans[1]
stmt = "SELECT players.id AS players_id, players.name AS players_name \nFROM players \nWHERE players.name = "
if span.attributes.get(SpanAttributes.DB_SYSTEM) == "sqlite":
stmt += "?"
@ -170,8 +172,9 @@ class SQLAlchemyTestMixin(TestBase):
self.assertEqual(len(rows), 0)
spans = self.memory_exporter.get_finished_spans()
self.assertEqual(len(spans), 1)
span = spans[0]
# one span for the connection and one for the query
self.assertEqual(len(spans), 2)
span = spans[1]
self._check_span(span, "SELECT")
self.assertEqual(
span.attributes.get(SpanAttributes.DB_STATEMENT),
@ -190,8 +193,9 @@ class SQLAlchemyTestMixin(TestBase):
self.assertEqual(len(rows), 0)
spans = self.memory_exporter.get_finished_spans()
self.assertEqual(len(spans), 2)
child_span, parent_span = spans
# one span for the connection and two for the queries
self.assertEqual(len(spans), 3)
_, child_span, parent_span = spans
# confirm the parenting
self.assertIsNone(parent_span.parent)
@ -247,5 +251,5 @@ class SQLAlchemyTestMixin(TestBase):
# batch inserts together which means `insert_players` only generates one span.
# See https://docs.sqlalchemy.org/en/14/changelog/migration_14.html#orm-batch-inserts-with-psycopg2-now-batch-statements-with-returning-in-most-cases
self.assertEqual(
len(spans), 5 if self.VENDOR not in ["postgresql"] else 3
len(spans), 8 if self.VENDOR not in ["postgresql"] else 6
)

View File

@ -69,8 +69,9 @@ class MssqlConnectorTestCase(SQLAlchemyTestMixin):
conn.execute("SELECT * FROM a_wrong_table").fetchall()
spans = self.memory_exporter.get_finished_spans()
self.assertEqual(len(spans), 1)
span = spans[0]
# one span for the connection and one for the query
self.assertEqual(len(spans), 2)
span = spans[1]
# span fields
self.assertEqual(span.name, "SELECT opentelemetry-tests")
self.assertEqual(
@ -96,9 +97,9 @@ class MssqlConnectorTestCase(SQLAlchemyTestMixin):
self.session.commit()
spans = self.memory_exporter.get_finished_spans()
# identity insert on before the insert, insert, and identity insert off after the insert
self.assertEqual(len(spans), 3)
span = spans[1]
# connect, identity insert on before the insert, insert, and identity insert off after the insert
self.assertEqual(len(spans), 4)
span = spans[2]
self._check_span(span, "INSERT")
self.assertIn(
"INSERT INTO players",

View File

@ -68,8 +68,9 @@ class MysqlConnectorTestCase(SQLAlchemyTestMixin):
conn.execute("SELECT * FROM a_wrong_table").fetchall()
spans = self.memory_exporter.get_finished_spans()
self.assertEqual(len(spans), 1)
span = spans[0]
# one span for the connection and one for the query
self.assertEqual(len(spans), 2)
span = spans[1]
# span fields
self.assertEqual(span.name, "SELECT opentelemetry-tests")
self.assertEqual(

View File

@ -62,8 +62,9 @@ class PostgresTestCase(SQLAlchemyTestMixin):
conn.execute("SELECT * FROM a_wrong_table").fetchall()
spans = self.memory_exporter.get_finished_spans()
self.assertEqual(len(spans), 1)
span = spans[0]
# one span for the connection and one for the query
self.assertEqual(len(spans), 2)
span = spans[1]
# span fields
self.assertEqual(span.name, "SELECT opentelemetry-tests")
self.assertEqual(

View File

@ -38,8 +38,9 @@ class SQLiteTestCase(SQLAlchemyTestMixin):
conn.execute(stmt).fetchall()
spans = self.memory_exporter.get_finished_spans()
self.assertEqual(len(spans), 1)
span = spans[0]
# one span for the connection and one span for the query
self.assertEqual(len(spans), 2)
span = spans[1]
# span fields
self.assertEqual(span.name, "SELECT :memory:")
self.assertEqual(