Skip to content

Commit 1d301de

Browse files
test(sqlite3): address review feedback and add CHANGELOG
- Add _connect helper with addCleanup for proper resource teardown - Rename test_uninstrument_connection to test_uninstrument_connection_with_instrument for consistency with psycopg2 - Use same query before and after uninstrumenting - Clear memory exporter and assert zero spans after uninstrument - Use sqlite3.OperationalError instead of generic Exception - Add span.status.description assertion for error test - Add CHANGELOG entry Signed-off-by: Artem Muterko <artem@sopho.tech>
1 parent a9635ea commit 1d301de

2 files changed

Lines changed: 29 additions & 14 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1313

1414
### Added
1515

16+
- `opentelemetry-instrumentation-sqlite3`: Add uninstrument, error status, suppress, and no-op tests
17+
([#4335](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/4335))
1618
- `opentelemetry-instrumentation-confluent-kafka`: Loosen confluent-kafka upper bound to <3.0.0
1719
([#4289](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/4289))
1820
- `opentelemetry-instrumentation`: Add support for wrapt 2.x

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

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -128,48 +128,57 @@ def tearDown(self):
128128
super().tearDown()
129129
SQLite3Instrumentor().uninstrument()
130130

131+
def _connect(self):
132+
"""Create an in-memory connection with cleanup registered."""
133+
cnx = sqlite3.connect(":memory:")
134+
self.addCleanup(cnx.close)
135+
return cnx
136+
131137
def test_uninstrument(self):
132138
"""Should stop generating spans after uninstrument."""
133139
SQLite3Instrumentor().instrument(tracer_provider=self.tracer_provider)
134-
cnx = sqlite3.connect(":memory:")
140+
cnx = self._connect()
135141
cursor = cnx.cursor()
136142
cursor.execute("CREATE TABLE IF NOT EXISTS test (id integer)")
137143

138144
spans_list = self.memory_exporter.get_finished_spans()
139145
self.assertEqual(len(spans_list), 1)
140146

141147
SQLite3Instrumentor().uninstrument()
148+
self.memory_exporter.clear()
142149

143-
cnx = sqlite3.connect(":memory:")
144-
cursor = cnx.cursor()
145-
cursor.execute("CREATE TABLE IF NOT EXISTS test (id integer)")
150+
cnx2 = self._connect()
151+
cursor2 = cnx2.cursor()
152+
cursor2.execute("CREATE TABLE IF NOT EXISTS test (id integer)")
146153

147154
spans_list = self.memory_exporter.get_finished_spans()
148-
self.assertEqual(len(spans_list), 1)
155+
self.assertEqual(len(spans_list), 0)
149156

150-
def test_uninstrument_connection(self):
157+
def test_uninstrument_connection_with_instrument(self):
151158
"""Should stop generating spans for uninstrumented connection."""
152159
SQLite3Instrumentor().instrument(tracer_provider=self.tracer_provider)
153-
cnx = sqlite3.connect(":memory:")
160+
cnx = self._connect()
161+
query = "CREATE TABLE IF NOT EXISTS test (id integer)"
154162
cursor = cnx.cursor()
155-
cursor.execute("CREATE TABLE IF NOT EXISTS test (id integer)")
163+
cursor.execute(query)
156164

157165
spans_list = self.memory_exporter.get_finished_spans()
158166
self.assertEqual(len(spans_list), 1)
159167

168+
self.memory_exporter.clear()
160169
cnx = SQLite3Instrumentor.uninstrument_connection(cnx)
161170
cursor = cnx.cursor()
162-
cursor.execute("INSERT INTO test (id) VALUES (1)")
171+
cursor.execute(query)
163172

164173
spans_list = self.memory_exporter.get_finished_spans()
165-
self.assertEqual(len(spans_list), 1)
174+
self.assertEqual(len(spans_list), 0)
166175

167176
def test_no_op_tracer_provider(self):
168177
"""Should produce no spans with NoOpTracerProvider."""
169178
SQLite3Instrumentor().instrument(
170179
tracer_provider=trace_api.NoOpTracerProvider()
171180
)
172-
cnx = sqlite3.connect(":memory:")
181+
cnx = self._connect()
173182
cursor = cnx.cursor()
174183
cursor.execute("CREATE TABLE IF NOT EXISTS test (id integer)")
175184

@@ -179,7 +188,7 @@ def test_no_op_tracer_provider(self):
179188
def test_suppress_instrumentation(self):
180189
"""Should produce no spans when suppressed."""
181190
SQLite3Instrumentor().instrument(tracer_provider=self.tracer_provider)
182-
cnx = sqlite3.connect(":memory:")
191+
cnx = self._connect()
183192

184193
with suppress_instrumentation():
185194
cursor = cnx.cursor()
@@ -191,13 +200,17 @@ def test_suppress_instrumentation(self):
191200
def test_span_failed(self):
192201
"""Should set error status on span when query fails."""
193202
SQLite3Instrumentor().instrument(tracer_provider=self.tracer_provider)
194-
cnx = sqlite3.connect(":memory:")
203+
cnx = self._connect()
195204
cursor = cnx.cursor()
196205

197-
with self.assertRaises(Exception):
206+
with self.assertRaises(sqlite3.OperationalError):
198207
cursor.execute("SELECT * FROM nonexistent_table")
199208

200209
spans_list = self.memory_exporter.get_finished_spans()
201210
self.assertEqual(len(spans_list), 1)
202211
span = spans_list[0]
203212
self.assertIs(span.status.status_code, trace_api.StatusCode.ERROR)
213+
self.assertEqual(
214+
span.status.description,
215+
"OperationalError: no such table: nonexistent_table",
216+
)

0 commit comments

Comments
 (0)