def intercept_unary_unary(self, continuation, client_call_details, request): metadata = [] if client_call_details.metadata is not None: metadata = list(client_call_details.metadata) metadata.append(('secret-key', self.secret_key)) new_details = _ClientCallDetails(client_call_details.method, client_call_details.timeout, metadata, client_call_details.credentials) response = continuation(new_details, request) return response
def test(self): interceptor = TracerClientInterceptor() mock_continuation = MagicMock() call_details = _ClientCallDetails('/test/Foo', None, [('a', 'b')], None) request = () mock_call = MagicMock() # type: Call mock_call.initial_metadata.return_value = [_Metadatum('c', 'd')] mock_continuation.return_value = mock_call response = interceptor.intercept_unary_unary(mock_continuation, call_details, request) # type: Call mock_continuation.assert_called_once_with( _ClientCallDetails('/test/Foo', None, [('a', 'b'), ('tracer', 'This is from interceptor')], None), request) self.assertIn(_Metadatum('c', 'd'), response.initial_metadata()) self.assertIn(_Metadatum('tracer', 'aaaa!'), response.initial_metadata())
def intercept_unary_unary(self, continuation, client_call_details, request): metadata = [] if client_call_details.metadata is not None: metadata = list(client_call_details.metadata) metadata.append(('tracer', 'This is from interceptor')) new_details = _ClientCallDetails(client_call_details.method, client_call_details.timeout, metadata, client_call_details.credentials) response = continuation(new_details, request) original_initial_metadata = response.initial_metadata response.initial_metadata = lambda: original_initial_metadata( ) + [_Metadatum('tracer', 'aaaa!')] return response
def _intercept_call(self, continuation, client_call_details, request_or_iterator): if self._tracer: with self._start_span(client_call_details.method) as span: metadata = self._inject_span_context(span, client_call_details.metadata) new_call_detial = _ClientCallDetails( client_call_details.method, client_call_details.timeout, metadata, client_call_details.credentials) # setattr(client_call_details, 'metadata', metadata) result = continuation(new_call_detial, request_or_iterator) return result return continuation(client_call_details, request_or_iterator)
def intercept_unary_unary(self, continuation, client_call_details, request): """Attach span values to an outbound gRPC call and log the call and response.""" self._tracer.logger.info( f'{client_call_details.method} - outbound gRPC call') metadata = self._generate_metadata_with_b3_values(client_call_details) client_call_details = _ClientCallDetails( client_call_details.method, client_call_details.timeout, metadata, client_call_details.credentials) response_future = continuation(client_call_details, request) response_future.result() # waits for response self._tracer.logger.info( f'Response received from {client_call_details.method}') return response_future
def intercept_unary_unary(self, continuation, client_call_details: _ClientCallDetails, request): # inject timeout client_call_details = _ClientCallDetails( client_call_details.method, self.timeout, client_call_details.metadata, client_call_details.credentials, client_call_details.wait_for_ready, client_call_details.compression) logging.info("Sending gRPC call to %r: %r", client_call_details.method, request) # FIXME: we work with continuation wrong here, should not been calling result() on it maybe? url = "grpc://" + self.address + client_call_details.method with apiritif.transaction(url) as tran: wrapped_req = self._make_request(client_call_details.method, url, request) tran.set_request(repr(request)) tran.attach_extra("grpc_req_class", request.DESCRIPTOR.full_name) continuation = continuation(client_call_details, request) code = continuation.code() tran.set_response_code(code.value[0]) msg = "gRPC status %s: %s" % (code, continuation.details()) logging.info(msg) if code != grpc.StatusCode.OK: tran.fail(msg) resp = continuation.result() tran.attach_extra("grpc_resp_class", resp.DESCRIPTOR.full_name) tran.set_response(repr(resp)) logging.info("gRPC response: %r", resp) wrapped_response = self._make_response(resp, wrapped_req, code) recorder.record_http_request(client_call_details.method, self.address, wrapped_req, wrapped_response, _context.session) return continuation