1515import unittest
1616
1717import mock
18+ from google .rpc import code_pb2
1819
1920from opencensus .trace import execution_context
21+ from opencensus .trace import span as span_module
2022from opencensus .trace .ext .grpc import server_interceptor
2123
2224
@@ -37,8 +39,6 @@ def test_rpc_handler_wrapper(self):
3739 self .assertEqual (wrapper .response_streaming , False )
3840
3941 def test_intercept_handler_no_metadata (self ):
40- current_span = mock .Mock ()
41- mock_tracer = MockTracer (None , None , None )
4242 patch = mock .patch (
4343 'opencensus.trace.ext.grpc.server_interceptor.tracer_module.Tracer' ,
4444 MockTracer )
@@ -57,12 +57,10 @@ def test_intercept_handler_no_metadata(self):
5757 }
5858
5959 self .assertEqual (
60- execution_context .get_opencensus_tracer ().current_span .attributes ,
60+ execution_context .get_opencensus_tracer ().current_span () .attributes ,
6161 expected_attributes )
6262
6363 def test_intercept_handler (self ):
64- current_span = mock .Mock ()
65- mock_tracer = MockTracer (None , None , None )
6664 patch = mock .patch (
6765 'opencensus.trace.ext.grpc.server_interceptor.tracer_module.Tracer' ,
6866 MockTracer )
@@ -83,7 +81,7 @@ def test_intercept_handler(self):
8381 }
8482
8583 self .assertEqual (
86- execution_context .get_opencensus_tracer ().current_span .attributes ,
84+ execution_context .get_opencensus_tracer ().current_span () .attributes ,
8785 expected_attributes )
8886
8987 def test_intercept_service (self ):
@@ -95,8 +93,6 @@ def test_intercept_service(self):
9593 self .assertTrue (mock_handler .called )
9694
9795 def test_intercept_handler_exception (self ):
98- current_span = mock .Mock ()
99- mock_tracer = MockTracer (None , None , None )
10096 patch = mock .patch (
10197 'opencensus.trace.ext.grpc.server_interceptor.tracer_module.Tracer' ,
10298 MockTracer )
@@ -120,26 +116,36 @@ def test_intercept_handler_exception(self):
120116 '/error/message' : 'Test'
121117 }
122118
119+ current_span = execution_context .get_opencensus_tracer ().current_span ()
123120 self .assertEqual (
124- execution_context .get_opencensus_tracer ().current_span .attributes ,
121+ execution_context .get_opencensus_tracer ().current_span () .attributes ,
125122 expected_attributes )
126123
124+ # check that the stack trace is attached to the current span
125+ self .assertIsNotNone (current_span .stack_trace )
126+ self .assertIsNotNone (current_span .stack_trace .stack_trace_hash_id )
127+ self .assertNotEqual (current_span .stack_trace .stack_frames , [])
128+
129+ # check that the status obj is attached to the current span
130+ self .assertIsNotNone (current_span .status )
131+ self .assertEqual (current_span .status .code , code_pb2 .UNKNOWN )
132+ self .assertEqual (current_span .status .message , 'Test' )
133+
127134
128135class MockTracer (object ):
129136 def __init__ (self , * args , ** kwargs ):
130- self .current_span = mock .Mock ()
131- self .current_span .attributes = {}
137+ self ._current_span = span_module .Span ('mock_span' )
132138 execution_context .set_opencensus_tracer (self )
133139
134140 def start_span (self , name ):
135- self .current_span .name = name
136- span = mock .Mock ()
137- span .__enter__ = mock .Mock ()
138- span .__exit__ = mock .Mock ()
139- return span
141+ self ._current_span .name = name
142+ return self ._current_span
140143
141144 def end_span (self ):
142145 return
143146
144147 def add_attribute_to_current_span (self , attribute_key , attribute_value ):
145- self .current_span .attributes [attribute_key ] = attribute_value
148+ self ._current_span .attributes [attribute_key ] = attribute_value
149+
150+ def current_span (self ):
151+ return self ._current_span
0 commit comments