18
18
19
19
from opentelemetry import trace as trace_api
20
20
from opentelemetry .instrumentation import dbapi
21
+ from opentelemetry .sdk import resources
21
22
from opentelemetry .semconv .trace import SpanAttributes
22
23
from opentelemetry .test .test_base import TestBase
23
24
@@ -41,7 +42,7 @@ def test_span_succeeded(self):
41
42
"user" : "user" ,
42
43
}
43
44
db_integration = dbapi .DatabaseApiIntegration (
44
- self . tracer , "testcomponent" , connection_attributes
45
+ "testname" , "testcomponent" , connection_attributes
45
46
)
46
47
mock_connection = db_integration .wrapped_connection (
47
48
mock_connect , {}, connection_props
@@ -73,7 +74,7 @@ def test_span_succeeded(self):
73
74
74
75
def test_span_name (self ):
75
76
db_integration = dbapi .DatabaseApiIntegration (
76
- self . tracer , "testcomponent" , {}
77
+ "testname" , "testcomponent" , {}
77
78
)
78
79
mock_connection = db_integration .wrapped_connection (
79
80
mock_connect , {}, {}
@@ -106,7 +107,7 @@ def test_span_succeeded_with_capture_of_statement_parameters(self):
106
107
"user" : "user" ,
107
108
}
108
109
db_integration = dbapi .DatabaseApiIntegration (
109
- self . tracer ,
110
+ "testname" ,
110
111
"testcomponent" ,
111
112
connection_attributes ,
112
113
capture_parameters = True ,
@@ -155,12 +156,10 @@ def test_span_not_recording(self):
155
156
"host" : "server_host" ,
156
157
"user" : "user" ,
157
158
}
158
- mock_tracer = mock .Mock ()
159
159
mock_span = mock .Mock ()
160
160
mock_span .is_recording .return_value = False
161
- mock_tracer .start_span .return_value = mock_span
162
161
db_integration = dbapi .DatabaseApiIntegration (
163
- mock_tracer , "testcomponent" , connection_attributes
162
+ "testname" , "testcomponent" , connection_attributes
164
163
)
165
164
mock_connection = db_integration .wrapped_connection (
166
165
mock_connect , {}, connection_props
@@ -192,9 +191,30 @@ def test_span_failed(self):
192
191
self .assertIs (span .status .status_code , trace_api .StatusCode .ERROR )
193
192
self .assertEqual (span .status .description , "Exception: Test Exception" )
194
193
194
+ def test_custom_tracer_provider_dbapi (self ):
195
+ resource = resources .Resource .create ({"db-resource-key" : "value" })
196
+ result = self .create_tracer_provider (resource = resource )
197
+ tracer_provider , exporter = result
198
+
199
+ db_integration = dbapi .DatabaseApiIntegration (
200
+ self .tracer , "testcomponent" , tracer_provider = tracer_provider
201
+ )
202
+ mock_connection = db_integration .wrapped_connection (
203
+ mock_connect , {}, {}
204
+ )
205
+ cursor = mock_connection .cursor ()
206
+ with self .assertRaises (Exception ):
207
+ cursor .execute ("Test query" , throw_exception = True )
208
+
209
+ spans_list = exporter .get_finished_spans ()
210
+ self .assertEqual (len (spans_list ), 1 )
211
+ span = spans_list [0 ]
212
+ self .assertEqual (span .resource .attributes ["db-resource-key" ], "value" )
213
+ self .assertIs (span .status .status_code , trace_api .StatusCode .ERROR )
214
+
195
215
def test_executemany (self ):
196
216
db_integration = dbapi .DatabaseApiIntegration (
197
- self . tracer , "testcomponent"
217
+ "testname" , "testcomponent"
198
218
)
199
219
mock_connection = db_integration .wrapped_connection (
200
220
mock_connect , {}, {}
@@ -210,7 +230,7 @@ def test_executemany(self):
210
230
211
231
def test_callproc (self ):
212
232
db_integration = dbapi .DatabaseApiIntegration (
213
- self . tracer , "testcomponent"
233
+ "testname" , "testcomponent"
214
234
)
215
235
mock_connection = db_integration .wrapped_connection (
216
236
mock_connect , {}, {}
0 commit comments