1818from opentelemetry import trace as trace_api
1919from opentelemetry .instrumentation .sqlite3 import SQLite3Instrumentor
2020from opentelemetry .instrumentation .utils import suppress_instrumentation
21+ from opentelemetry .semconv ._incubating .attributes .db_attributes import (
22+ DB_STATEMENT ,
23+ )
2124from opentelemetry .test .test_base import TestBase
2225
2326
@@ -139,6 +142,7 @@ def test_uninstrument(self):
139142 SQLite3Instrumentor ().instrument (tracer_provider = self .tracer_provider )
140143 cnx = self ._connect ()
141144 cursor = cnx .cursor ()
145+ self .addCleanup (cursor .close )
142146 cursor .execute ("CREATE TABLE IF NOT EXISTS test (id integer)" )
143147
144148 spans_list = self .memory_exporter .get_finished_spans ()
@@ -149,6 +153,7 @@ def test_uninstrument(self):
149153
150154 cnx2 = self ._connect ()
151155 cursor2 = cnx2 .cursor ()
156+ self .addCleanup (cursor2 .close )
152157 cursor2 .execute ("CREATE TABLE IF NOT EXISTS test (id integer)" )
153158
154159 spans_list = self .memory_exporter .get_finished_spans ()
@@ -160,6 +165,7 @@ def test_uninstrument_connection_with_instrument(self):
160165 cnx = self ._connect ()
161166 query = "CREATE TABLE IF NOT EXISTS test (id integer)"
162167 cursor = cnx .cursor ()
168+ self .addCleanup (cursor .close )
163169 cursor .execute (query )
164170
165171 spans_list = self .memory_exporter .get_finished_spans ()
@@ -168,6 +174,7 @@ def test_uninstrument_connection_with_instrument(self):
168174 self .memory_exporter .clear ()
169175 cnx = SQLite3Instrumentor .uninstrument_connection (cnx )
170176 cursor = cnx .cursor ()
177+ self .addCleanup (cursor .close )
171178 cursor .execute (query )
172179
173180 spans_list = self .memory_exporter .get_finished_spans ()
@@ -180,6 +187,7 @@ def test_no_op_tracer_provider(self):
180187 )
181188 cnx = self ._connect ()
182189 cursor = cnx .cursor ()
190+ self .addCleanup (cursor .close )
183191 cursor .execute ("CREATE TABLE IF NOT EXISTS test (id integer)" )
184192
185193 spans_list = self .memory_exporter .get_finished_spans ()
@@ -192,6 +200,7 @@ def test_suppress_instrumentation(self):
192200
193201 with suppress_instrumentation ():
194202 cursor = cnx .cursor ()
203+ self .addCleanup (cursor .close )
195204 cursor .execute ("CREATE TABLE IF NOT EXISTS test (id integer)" )
196205
197206 spans_list = self .memory_exporter .get_finished_spans ()
@@ -202,13 +211,18 @@ def test_span_failed(self):
202211 SQLite3Instrumentor ().instrument (tracer_provider = self .tracer_provider )
203212 cnx = self ._connect ()
204213 cursor = cnx .cursor ()
214+ self .addCleanup (cursor .close )
205215
206216 with self .assertRaises (sqlite3 .OperationalError ):
207217 cursor .execute ("SELECT * FROM nonexistent_table" )
208218
209219 spans_list = self .memory_exporter .get_finished_spans ()
210220 self .assertEqual (len (spans_list ), 1 )
211221 span = spans_list [0 ]
222+ self .assertEqual (
223+ span .attributes [DB_STATEMENT ],
224+ "SELECT * FROM nonexistent_table" ,
225+ )
212226 self .assertIs (span .status .status_code , trace_api .StatusCode .ERROR )
213227 self .assertEqual (
214228 span .status .description ,
0 commit comments