Skip to content

Commit

Permalink
Add docstring for instrument_connection and tests for sqlite
Browse files Browse the repository at this point in the history
  • Loading branch information
beijiez committed Dec 13, 2024
1 parent 38f0e33 commit 2ab903a
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,22 @@
SQLite3Instrumentor().instrument()
cnx = sqlite3.connect(':memory:')
cursor = cnx.cursor()
cursor.execute("CREATE TABLE test (testField INTEGER)")
cursor.execute("INSERT INTO test (testField) VALUES (123)")
cursor.close()
cnx.close()
conn = sqlite3.connect(":memory:")
instrumented_connection = SQLite3Instrumentor.instrument_connection(conn)
cursor = instrumented_connection.cursor()
cursor.execute("CREATE TABLE test (testField INTEGER)")
cursor.execute("INSERT INTO test (testField) VALUES (123)")
cursor.execute("SELECT * FROM test")
cursor.close()
instrumented_connection.close()
API
---
Expand Down Expand Up @@ -104,7 +115,13 @@ def instrument_connection(
the current globally configured one is used.
Returns:
An instrumented connection.
SQLite3Connection: An instrumented SQLite connection that supports
telemetry for tracing database operations.
Notes:
- Instrumentation must be explicitly applied to the connection object
for tracing to work. This is not done automatically by simply calling
`SQLite3Instrumentor().instrument()`.
"""

return dbapi.instrument_connection(
Expand All @@ -129,3 +146,4 @@ def uninstrument_connection(
An uninstrumented connection.
"""
return dbapi.uninstrument_connection(connection)

Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ def setUp(self):
self._cursor = self._connection.cursor()
self._connection2 = dbapi2.connect(":memory:")
self._cursor2 = self._connection2.cursor()
self._connection3 = SQLite3Instrumentor.instrument_connection(dbapi2.connect(":memory:"))
self._cursor3 = self._connection3.cursor()

def tearDown(self):
super().tearDown()
Expand All @@ -40,6 +42,10 @@ def tearDown(self):
self._cursor2.close()
if self._connection2:
self._connection2.close()
if self._cursor3:
self._cursor3.close()
if self._connection3:
self._connection3.close()
SQLite3Instrumentor().uninstrument()

def validate_spans(self, span_name):
Expand All @@ -65,6 +71,7 @@ def _create_tables(self):
stmt = "CREATE TABLE IF NOT EXISTS test (id integer)"
self._cursor.execute(stmt)
self._cursor2.execute(stmt)
self._cursor3.execute(stmt)
self.memory_exporter.clear()

def test_execute(self):
Expand All @@ -77,6 +84,10 @@ def test_execute(self):
with self._tracer.start_as_current_span("rootSpan"):
self._cursor2.execute(stmt)
self.validate_spans("CREATE")

with self._tracer.start_as_current_span("rootSpan"):
self._cursor3.execute(stmt)
self.validate_spans("CREATE")

def test_executemany(self):
"""Should create a child span for executemany"""
Expand All @@ -93,10 +104,15 @@ def test_executemany(self):
self._cursor2.executemany(stmt, data)
self.validate_spans("INSERT")

with self._tracer.start_as_current_span("rootSpan"):
self._cursor3.executemany(stmt, data)
self.validate_spans("INSERT")

def test_callproc(self):
"""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("test")

0 comments on commit 2ab903a

Please sign in to comment.