@@ -51,40 +51,44 @@ class OpenCensusClientInterceptor(grpc.UnaryUnaryClientInterceptor,
5151 grpc .StreamStreamClientInterceptor ):
5252
5353 def __init__ (self , tracer = None , host_port = None ):
54- if tracer is None :
55- tracer = execution_context .get_opencensus_tracer ()
56-
5754 self ._tracer = tracer
5855 self .host_port = host_port
5956 self ._propagator = binary_format .BinaryFormatPropagator ()
6057
58+ @property
59+ def tracer (self ):
60+ return self ._tracer or execution_context .get_opencensus_tracer ()
61+
6162 def _start_client_span (self , client_call_details ):
62- span = self ._tracer .start_span (
63+ span = self .tracer .start_span (
6364 name = _get_span_name (client_call_details )
6465 )
6566
6667 # Add the component grpc to span attribute
67- self ._tracer .add_attribute_to_current_span (
68+ self .tracer .add_attribute_to_current_span (
6869 attribute_key = attributes_helper .COMMON_ATTRIBUTES .get (
6970 ATTRIBUTE_COMPONENT ),
7071 attribute_value = 'grpc' )
7172
7273 # Add the host:port info to span attribute
73- self ._tracer .add_attribute_to_current_span (
74+ self .tracer .add_attribute_to_current_span (
7475 attribute_key = attributes_helper .GRPC_ATTRIBUTES .get (
7576 GRPC_HOST_PORT ),
7677 attribute_value = self .host_port )
7778
7879 # Add the method to span attribute
79- self ._tracer .add_attribute_to_current_span (
80+ self .tracer .add_attribute_to_current_span (
8081 attribute_key = attributes_helper .GRPC_ATTRIBUTES .get (GRPC_METHOD ),
8182 attribute_value = str (client_call_details .method ))
8283
84+ execution_context .set_opencensus_tracer (self .tracer )
85+ execution_context .set_current_span (span )
86+
8387 return span
8488
8589 def _end_span_between_context (self , current_span ):
8690 execution_context .set_current_span (current_span )
87- self ._tracer .end_span ()
91+ self .tracer .end_span ()
8892
8993 def _intercept_call (
9094 self , client_call_details , request_iterator , grpc_type ):
@@ -95,14 +99,12 @@ def _intercept_call(
9599 # Start a span
96100 current_span = self ._start_client_span (client_call_details )
97101
98- span_context = self . _tracer .span_context
102+ span_context = current_span . context_tracer .span_context
99103 header = self ._propagator .to_header (span_context )
100104 grpc_trace_metadata = {
101105 oc_grpc .GRPC_TRACE_KEY : header ,
102106 }
103107
104- metadata_to_append = None
105-
106108 if isinstance (metadata , list ):
107109 metadata_to_append = list (six .iteritems (grpc_trace_metadata ))
108110 else :
@@ -122,7 +124,7 @@ def _callback(self, current_span):
122124 def callback (future_response ):
123125 execution_context .set_current_span (current_span )
124126 self ._trace_future_exception (future_response )
125- self ._tracer .end_span ()
127+ self .tracer .end_span ()
126128
127129 return callback
128130
@@ -133,7 +135,7 @@ def _trace_future_exception(self, response):
133135 if exception is not None :
134136 exception = str (exception )
135137
136- self ._tracer .add_attribute_to_current_span (
138+ self .tracer .add_attribute_to_current_span (
137139 attribute_key = attributes_helper .COMMON_ATTRIBUTES .get (
138140 ATTRIBUTE_ERROR_MESSAGE ),
139141 attribute_value = exception )
0 commit comments