1919
2020import flask
2121import mock
22+ from google .rpc import code_pb2
2223
2324from opencensus .trace import execution_context
25+ from opencensus .trace import span_data
26+ from opencensus .trace import stack_trace
27+ from opencensus .trace import status
2428from opencensus .trace .exporters import print_exporter
2529from opencensus .trace .ext .flask import flask_middleware
2630from opencensus .trace .propagation import google_cloud_format
@@ -44,6 +48,10 @@ def index():
4448 def health_check ():
4549 return 'test health check' # pragma: NO COVER
4650
51+ @app .route ('/error' )
52+ def error ():
53+ raise Exception ('error' )
54+
4755 return app
4856
4957 def tearDown (self ):
@@ -241,3 +249,36 @@ def test__after_request_blacklist(self):
241249
242250 self .assertEqual (response .status_code , 200 )
243251 assert isinstance (tracer , noop_tracer .NoopTracer )
252+
253+ def test_teardown_include_exception (self ):
254+ mock_exporter = mock .MagicMock ()
255+ app = self .create_app ()
256+ flask_middleware .FlaskMiddleware (app = app , exporter = mock_exporter )
257+ response = app .test_client ().get ('/error' )
258+
259+ self .assertEqual (response .status_code , 500 )
260+
261+ exported_spandata = mock_exporter .export .call_args [0 ][0 ][0 ]
262+ self .assertIsInstance (exported_spandata , span_data .SpanData )
263+ self .assertIsInstance (exported_spandata .status , status .Status )
264+ self .assertEqual (exported_spandata .status .code , code_pb2 .UNKNOWN )
265+ self .assertEqual (exported_spandata .status .message , 'error' )
266+
267+ def test_teardown_include_exception_and_traceback (self ):
268+ mock_exporter = mock .MagicMock ()
269+ app = self .create_app ()
270+ app .config ['TESTING' ] = True
271+ flask_middleware .FlaskMiddleware (app = app , exporter = mock_exporter )
272+ with self .assertRaises (Exception ):
273+ app .test_client ().get ('/error' )
274+
275+ exported_spandata = mock_exporter .export .call_args [0 ][0 ][0 ]
276+ self .assertIsInstance (exported_spandata , span_data .SpanData )
277+ self .assertIsInstance (exported_spandata .status , status .Status )
278+ self .assertEqual (exported_spandata .status .code , code_pb2 .UNKNOWN )
279+ self .assertEqual (exported_spandata .status .message , 'error' )
280+ self .assertIsInstance (
281+ exported_spandata .stack_trace , stack_trace .StackTrace
282+ )
283+ self .assertIsNotNone (exported_spandata .stack_trace .stack_trace_hash_id )
284+ self .assertNotEqual (exported_spandata .stack_trace .stack_frames , [])
0 commit comments