-
Notifications
You must be signed in to change notification settings - Fork 249
/
client_interceptor.py
244 lines (193 loc) · 8.26 KB
/
client_interceptor.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
# Copyright 2017, OpenCensus Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import collections
import logging
import grpc
import six
from opencensus.ext import grpc as oc_grpc
from opencensus.ext.grpc import utils as grpc_utils
from opencensus.trace import attributes_helper
from opencensus.trace import execution_context
from opencensus.trace import span as span_module
from opencensus.trace import time_event
from opencensus.trace.propagation import binary_format
log = logging.getLogger(__name__)
ATTRIBUTE_COMPONENT = 'COMPONENT'
ATTRIBUTE_ERROR_NAME = 'ERROR_NAME'
ATTRIBUTE_ERROR_MESSAGE = 'ERROR_MESSAGE'
GRPC_HOST_PORT = 'GRPC_HOST_PORT'
GRPC_METHOD = 'GRPC_METHOD'
SENT_PREFIX = 'Sent'
TIMEOUT = 3
# Do not trace StackDriver Trace exporter activities to avoid deadlock.
CLOUD_TRACE = 'google.devtools.cloudtrace'
class _ClientCallDetails(
collections.namedtuple(
'_ClientCallDetails',
('method', 'timeout', 'metadata', 'credentials')),
grpc.ClientCallDetails):
pass
class OpenCensusClientInterceptor(grpc.UnaryUnaryClientInterceptor,
grpc.UnaryStreamClientInterceptor,
grpc.StreamUnaryClientInterceptor,
grpc.StreamStreamClientInterceptor):
def __init__(self, tracer=None, host_port=None):
self._tracer = tracer
self.host_port = host_port
self._propagator = binary_format.BinaryFormatPropagator()
@property
def tracer(self):
return self._tracer or execution_context.get_opencensus_tracer()
def _start_client_span(self, client_call_details):
span = self.tracer.start_span(
name=_get_span_name(client_call_details)
)
span.span_kind = span_module.SpanKind.CLIENT
# Add the component grpc to span attribute
self.tracer.add_attribute_to_current_span(
attribute_key=attributes_helper.COMMON_ATTRIBUTES.get(
ATTRIBUTE_COMPONENT),
attribute_value='grpc')
# Add the host:port info to span attribute
self.tracer.add_attribute_to_current_span(
attribute_key=attributes_helper.GRPC_ATTRIBUTES.get(
GRPC_HOST_PORT),
attribute_value=self.host_port)
# Add the method to span attribute
self.tracer.add_attribute_to_current_span(
attribute_key=attributes_helper.GRPC_ATTRIBUTES.get(GRPC_METHOD),
attribute_value=str(client_call_details.method))
return span
def _intercept_call(
self, client_call_details, request_iterator, grpc_type
):
metadata = ()
if client_call_details.metadata is not None:
metadata = client_call_details.metadata
# Start a span
current_span = self._start_client_span(client_call_details)
span_context = current_span.context_tracer.span_context
header = self._propagator.to_header(span_context)
grpc_trace_metadata = {
oc_grpc.GRPC_TRACE_KEY: header,
}
if isinstance(metadata, list):
metadata_to_append = list(six.iteritems(grpc_trace_metadata))
else:
metadata_to_append = tuple(six.iteritems(grpc_trace_metadata))
metadata = metadata + metadata_to_append
client_call_details = _ClientCallDetails(
client_call_details.method,
client_call_details.timeout,
metadata,
client_call_details.credentials)
request_iterator = grpc_utils.wrap_iter_with_message_events(
request_or_response_iter=request_iterator,
span=current_span,
message_event_type=time_event.Type.SENT
)
return client_call_details, request_iterator, current_span
def _callback(self, current_span):
def callback(future_response):
grpc_utils.add_message_event(
proto_message=future_response.result(),
span=current_span,
message_event_type=time_event.Type.RECEIVED,
)
self._trace_future_exception(future_response)
self.tracer.end_span()
return callback
def _trace_future_exception(self, response):
# Trace the exception for a grpc.Future if any
exception = response.exception()
if exception is not None:
exception = str(exception)
self.tracer.add_attribute_to_current_span(
attribute_key=attributes_helper.COMMON_ATTRIBUTES.get(
ATTRIBUTE_ERROR_MESSAGE),
attribute_value=exception)
def intercept_unary_unary(
self, continuation, client_call_details, request
):
if CLOUD_TRACE in client_call_details.method:
response = continuation(client_call_details, request)
return response
new_details, new_request, current_span = self._intercept_call(
client_call_details=client_call_details,
request_iterator=iter((request,)),
grpc_type=oc_grpc.UNARY_UNARY)
response = continuation(
new_details,
next(new_request))
response.add_done_callback(self._callback(current_span))
return response
def intercept_unary_stream(
self, continuation, client_call_details, request
):
if CLOUD_TRACE in client_call_details.method:
response = continuation(client_call_details, request)
return response
new_details, new_request_iterator, current_span = self._intercept_call(
client_call_details=client_call_details,
request_iterator=iter((request,)),
grpc_type=oc_grpc.UNARY_STREAM)
response_it = continuation(
new_details,
next(new_request_iterator))
response_it = grpc_utils.wrap_iter_with_message_events(
request_or_response_iter=response_it,
span=current_span,
message_event_type=time_event.Type.RECEIVED
)
response_it = grpc_utils.wrap_iter_with_end_span(response_it)
return response_it
def intercept_stream_unary(
self, continuation, client_call_details, request_iterator
):
if CLOUD_TRACE in client_call_details.method:
response = continuation(client_call_details, request_iterator)
return response
new_details, new_request_iterator, current_span = self._intercept_call(
client_call_details=client_call_details,
request_iterator=request_iterator,
grpc_type=oc_grpc.STREAM_UNARY)
response = continuation(
new_details,
new_request_iterator)
response.add_done_callback(self._callback(current_span))
return response
def intercept_stream_stream(
self, continuation, client_call_details, request_iterator
):
if CLOUD_TRACE in client_call_details.method:
response = continuation(client_call_details, request_iterator)
return response
new_details, new_request_iterator, current_span = self._intercept_call(
client_call_details=client_call_details,
request_iterator=request_iterator,
grpc_type=oc_grpc.STREAM_STREAM)
response_it = continuation(
new_details,
new_request_iterator)
response_it = grpc_utils.wrap_iter_with_message_events(
request_or_response_iter=response_it,
span=current_span,
message_event_type=time_event.Type.RECEIVED
)
response_it = grpc_utils.wrap_iter_with_end_span(response_it)
return response_it
def _get_span_name(client_call_details):
"""Generates a span name based off of the gRPC client call details"""
method_name = client_call_details.method[1:].replace('/', '.')
return '{}.{}'.format(SENT_PREFIX, method_name)