Skip to content

Commit a9635ea

Browse files
test(sqlite3): add uninstrument, error, suppress, and no-op tests
Signed-off-by: Artem Muterko <artem@sopho.tech>
1 parent 195bcdb commit a9635ea

1 file changed

Lines changed: 81 additions & 0 deletions

File tree

instrumentation/opentelemetry-instrumentation-sqlite3/tests/test_sqlite3.py

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
from opentelemetry import trace as trace_api
1919
from opentelemetry.instrumentation.sqlite3 import SQLite3Instrumentor
20+
from opentelemetry.instrumentation.utils import suppress_instrumentation
2021
from opentelemetry.test.test_base import TestBase
2122

2223

@@ -120,3 +121,83 @@ def test_callproc(self):
120121
):
121122
self._cursor.callproc("test", ())
122123
self.validate_spans("test")
124+
125+
126+
class TestSQLite3Integration(TestBase):
127+
def tearDown(self):
128+
super().tearDown()
129+
SQLite3Instrumentor().uninstrument()
130+
131+
def test_uninstrument(self):
132+
"""Should stop generating spans after uninstrument."""
133+
SQLite3Instrumentor().instrument(tracer_provider=self.tracer_provider)
134+
cnx = sqlite3.connect(":memory:")
135+
cursor = cnx.cursor()
136+
cursor.execute("CREATE TABLE IF NOT EXISTS test (id integer)")
137+
138+
spans_list = self.memory_exporter.get_finished_spans()
139+
self.assertEqual(len(spans_list), 1)
140+
141+
SQLite3Instrumentor().uninstrument()
142+
143+
cnx = sqlite3.connect(":memory:")
144+
cursor = cnx.cursor()
145+
cursor.execute("CREATE TABLE IF NOT EXISTS test (id integer)")
146+
147+
spans_list = self.memory_exporter.get_finished_spans()
148+
self.assertEqual(len(spans_list), 1)
149+
150+
def test_uninstrument_connection(self):
151+
"""Should stop generating spans for uninstrumented connection."""
152+
SQLite3Instrumentor().instrument(tracer_provider=self.tracer_provider)
153+
cnx = sqlite3.connect(":memory:")
154+
cursor = cnx.cursor()
155+
cursor.execute("CREATE TABLE IF NOT EXISTS test (id integer)")
156+
157+
spans_list = self.memory_exporter.get_finished_spans()
158+
self.assertEqual(len(spans_list), 1)
159+
160+
cnx = SQLite3Instrumentor.uninstrument_connection(cnx)
161+
cursor = cnx.cursor()
162+
cursor.execute("INSERT INTO test (id) VALUES (1)")
163+
164+
spans_list = self.memory_exporter.get_finished_spans()
165+
self.assertEqual(len(spans_list), 1)
166+
167+
def test_no_op_tracer_provider(self):
168+
"""Should produce no spans with NoOpTracerProvider."""
169+
SQLite3Instrumentor().instrument(
170+
tracer_provider=trace_api.NoOpTracerProvider()
171+
)
172+
cnx = sqlite3.connect(":memory:")
173+
cursor = cnx.cursor()
174+
cursor.execute("CREATE TABLE IF NOT EXISTS test (id integer)")
175+
176+
spans_list = self.memory_exporter.get_finished_spans()
177+
self.assertEqual(len(spans_list), 0)
178+
179+
def test_suppress_instrumentation(self):
180+
"""Should produce no spans when suppressed."""
181+
SQLite3Instrumentor().instrument(tracer_provider=self.tracer_provider)
182+
cnx = sqlite3.connect(":memory:")
183+
184+
with suppress_instrumentation():
185+
cursor = cnx.cursor()
186+
cursor.execute("CREATE TABLE IF NOT EXISTS test (id integer)")
187+
188+
spans_list = self.memory_exporter.get_finished_spans()
189+
self.assertEqual(len(spans_list), 0)
190+
191+
def test_span_failed(self):
192+
"""Should set error status on span when query fails."""
193+
SQLite3Instrumentor().instrument(tracer_provider=self.tracer_provider)
194+
cnx = sqlite3.connect(":memory:")
195+
cursor = cnx.cursor()
196+
197+
with self.assertRaises(Exception):
198+
cursor.execute("SELECT * FROM nonexistent_table")
199+
200+
spans_list = self.memory_exporter.get_finished_spans()
201+
self.assertEqual(len(spans_list), 1)
202+
span = spans_list[0]
203+
self.assertIs(span.status.status_code, trace_api.StatusCode.ERROR)

0 commit comments

Comments
 (0)