12
12
# See the License for the specific language governing permissions and
13
13
# limitations under the License.
14
14
15
- import sys
15
+ from unittest import mock
16
16
17
17
import httpretty
18
18
import requests
19
- import urllib3
20
19
21
20
import opentelemetry .ext .requests
22
21
from opentelemetry import context , propagators , trace
23
22
from opentelemetry .ext .requests import RequestsInstrumentor
24
23
from opentelemetry .sdk import resources
25
24
from opentelemetry .test .mock_httptextformat import MockHTTPTextFormat
26
25
from opentelemetry .test .test_base import TestBase
26
+ from opentelemetry .trace .status import StatusCanonicalCode
27
27
28
28
29
29
class TestRequestsIntegration (TestBase ):
@@ -92,13 +92,8 @@ def test_not_foundbasic(self):
92
92
93
93
def test_invalid_url (self ):
94
94
url = "http://[::1/nope"
95
- exception_type = requests .exceptions .InvalidURL
96
- if sys .version_info [:2 ] < (3 , 5 ) and tuple (
97
- map (int , urllib3 .__version__ .split ("." )[:2 ])
98
- ) < (1 , 25 ):
99
- exception_type = ValueError
100
95
101
- with self .assertRaises (exception_type ):
96
+ with self .assertRaises (ValueError ):
102
97
requests .post (url )
103
98
104
99
span_list = self .memory_exporter .get_finished_spans ()
@@ -110,6 +105,9 @@ def test_invalid_url(self):
110
105
span .attributes ,
111
106
{"component" : "http" , "http.method" : "POST" , "http.url" : url },
112
107
)
108
+ self .assertEqual (
109
+ span .status .canonical_code , StatusCanonicalCode .INVALID_ARGUMENT
110
+ )
113
111
114
112
def test_uninstrument (self ):
115
113
RequestsInstrumentor ().uninstrument ()
@@ -229,3 +227,75 @@ def test_custom_tracer_provider(self):
229
227
span = span_list [0 ]
230
228
231
229
self .assertIs (span .resource , resource )
230
+
231
+ @mock .patch ("requests.Session.send" , side_effect = requests .RequestException )
232
+ def test_requests_exception_without_response (self , * _ , ** __ ):
233
+
234
+ with self .assertRaises (requests .RequestException ):
235
+ requests .get (self .URL )
236
+
237
+ span_list = self .memory_exporter .get_finished_spans ()
238
+ self .assertEqual (len (span_list ), 1 )
239
+ span = span_list [0 ]
240
+ self .assertEqual (
241
+ span .attributes ,
242
+ {"component" : "http" , "http.method" : "GET" , "http.url" : self .URL },
243
+ )
244
+ self .assertEqual (
245
+ span .status .canonical_code , StatusCanonicalCode .UNKNOWN
246
+ )
247
+
248
+ mocked_response = requests .Response ()
249
+ mocked_response .status_code = 500
250
+ mocked_response .reason = "Internal Server Error"
251
+
252
+ @mock .patch (
253
+ "requests.Session.send" ,
254
+ side_effect = requests .RequestException (response = mocked_response ),
255
+ )
256
+ def test_requests_exception_with_response (self , * _ , ** __ ):
257
+
258
+ with self .assertRaises (requests .RequestException ):
259
+ requests .get (self .URL )
260
+
261
+ span_list = self .memory_exporter .get_finished_spans ()
262
+ self .assertEqual (len (span_list ), 1 )
263
+ span = span_list [0 ]
264
+ self .assertEqual (
265
+ span .attributes ,
266
+ {
267
+ "component" : "http" ,
268
+ "http.method" : "GET" ,
269
+ "http.url" : self .URL ,
270
+ "http.status_code" : 500 ,
271
+ "http.status_text" : "Internal Server Error" ,
272
+ },
273
+ )
274
+ self .assertEqual (
275
+ span .status .canonical_code , StatusCanonicalCode .INTERNAL
276
+ )
277
+
278
+ @mock .patch ("requests.Session.send" , side_effect = Exception )
279
+ def test_requests_basic_exception (self , * _ , ** __ ):
280
+
281
+ with self .assertRaises (Exception ):
282
+ requests .get (self .URL )
283
+
284
+ span_list = self .memory_exporter .get_finished_spans ()
285
+ self .assertEqual (len (span_list ), 1 )
286
+ self .assertEqual (
287
+ span_list [0 ].status .canonical_code , StatusCanonicalCode .UNKNOWN
288
+ )
289
+
290
+ @mock .patch ("requests.Session.send" , side_effect = requests .Timeout )
291
+ def test_requests_timeout_exception (self , * _ , ** __ ):
292
+
293
+ with self .assertRaises (Exception ):
294
+ requests .get (self .URL )
295
+
296
+ span_list = self .memory_exporter .get_finished_spans ()
297
+ self .assertEqual (len (span_list ), 1 )
298
+ self .assertEqual (
299
+ span_list [0 ].status .canonical_code ,
300
+ StatusCanonicalCode .DEADLINE_EXCEEDED ,
301
+ )
0 commit comments