Beispiel #1
0
    def test__intercept_call_metadata_list(self):
        tracer = mock.Mock()
        tracer.span_context = mock.Mock()
        test_header = 'test header'
        mock_propagator = mock.Mock()
        mock_propagator.to_header.return_value = test_header

        interceptor = client_interceptor.OpenCensusClientInterceptor(
            tracer=tracer, host_port='test')
        interceptor._propagator = mock_propagator
        mock_client_call_details = mock.Mock()
        mock_client_call_details.metadata = [
            ('test_key', 'test_value'),
        ]
        mock_client_call_details.method = '/hello'

        client_call_details, request_iterator, current_span = interceptor._intercept_call(
            mock_client_call_details, mock.Mock(), 'unary_unary')

        expected_metadata = [
            ('test_key', 'test_value'),
            ('grpc-trace-bin', test_header),
        ]

        self.assertEqual(expected_metadata, client_call_details.metadata)
Beispiel #2
0
 def _unary_helper(self):
     continuation = mock.Mock()
     mock_response = mock.Mock()
     continuation.return_value = mock_response
     interceptor = client_interceptor.OpenCensusClientInterceptor()
     interceptor._intercept_call = mock.Mock(return_value=(None, iter([mock.Mock()]), None))
     return interceptor, continuation, mock_response
Beispiel #3
0
    def test__start_client_span(self):
        tracer = mock.Mock()
        interceptor = client_interceptor.OpenCensusClientInterceptor(
            tracer=tracer, host_port='test')
        interceptor._start_client_span('test_method', 'unary_unary')

        self.assertTrue(tracer.start_span.called)
        self.assertTrue(tracer.add_attribute_to_current_span.called)
Beispiel #4
0
    def test__start_client_span(self):
        tracer = mock.Mock()
        interceptor = client_interceptor.OpenCensusClientInterceptor(
            tracer=tracer, host_port='test')
        mock_client_call_details = mock.Mock()
        mock_client_call_details.method = '/hello'
        interceptor._start_client_span(mock_client_call_details)

        self.assertTrue(tracer.start_span.called)
        self.assertTrue(tracer.add_attribute_to_current_span.called)
Beispiel #5
0
    def test_constructor_default(self):
        from opencensus.trace.propagation import binary_format

        interceptor = client_interceptor.OpenCensusClientInterceptor()

        self.assertIsNone(interceptor._tracer)
        self.assertIsNone(interceptor.host_port)
        self.assertTrue(
            isinstance(interceptor._propagator,
                       binary_format.BinaryFormatPropagator))
 def _stream_helper(self):
     continuation = mock.Mock()
     mock_response = iter([mock.Mock()])
     continuation.return_value = mock_response
     mock_tracer = mock.Mock()
     interceptor = client_interceptor.OpenCensusClientInterceptor(
         tracer=mock_tracer)
     interceptor._intercept_call = mock.Mock(
         return_value=(None, iter([mock.Mock()]), mock.Mock()))
     return interceptor, continuation, mock_tracer
Beispiel #7
0
def main():
    exporter = stackdriver_exporter.StackdriverExporter()
    tracer = Tracer(exporter=exporter)
    tracer_interceptor = client_interceptor.OpenCensusClientInterceptor(
        tracer, host_port=HOST_PORT)
    channel = grpc.insecure_channel(HOST_PORT)
    channel = grpc.intercept_channel(channel, tracer_interceptor)
    stub = hello_world_pb2_grpc.GreeterStub(channel)
    response = stub.SayHello(hello_world_pb2.HelloRequest(name='you'))
    print("Message received: " + response.message)
Beispiel #8
0
    def test_constructor_explicit(self):
        from opencensus.trace.propagation import binary_format

        tracer = mock.Mock()
        host_port = 'localhost:50051'
        interceptor = client_interceptor.OpenCensusClientInterceptor(
            tracer=tracer, host_port=host_port)

        self.assertEqual(interceptor._tracer, tracer)
        self.assertEqual(interceptor.host_port, host_port)
        self.assertTrue(isinstance(
            interceptor._propagator, binary_format.BinaryFormatPropagator))
Beispiel #9
0
    def test__end_span_between_context(self):
        from opencensus.trace import execution_context

        current_span = mock.Mock()
        tracer = mock.Mock()
        interceptor = client_interceptor.OpenCensusClientInterceptor(
            tracer=tracer, host_port='test')
        interceptor._end_span_between_context(current_span)

        span_in_context = execution_context.get_current_span()

        self.assertEqual(span_in_context, current_span)
        self.assertTrue(tracer.end_span.called)
Beispiel #10
0
    def test__callback_no_exception(self):
        current_span = mock.Mock()
        tracer = MockTracer(current_span)
        interceptor = client_interceptor.OpenCensusClientInterceptor(
            tracer=tracer, host_port='test')
        current_span.attributes = {}
        callback = interceptor._callback(current_span)
        response = mock.Mock()
        response.exception.return_value = None
        callback(response)
        expected_attributes = {'/error/message': None}

        self.assertEqual(current_span.attributes, expected_attributes)
Beispiel #11
0
def _get_grpc_stub(stub_cls, host_port):
    stub = _stub_cache.get((stub_cls, host_port))
    if stub is not None:
        return stub

    channel = grpc.insecure_channel(host_port)
    tracer_interceptor = client_interceptor.OpenCensusClientInterceptor(
        host_port=HELLO_WORLD_HOST_PORT
    )
    channel = grpc.intercept_channel(channel, tracer_interceptor)
    stub = stub_cls(channel)
    _stub_cache[(stub_cls, host_port)] = stub
    return stub
    def test__intercept_call_noop_tracer(self):
        tracer = NoopTracer()
        test_header = 'test header'
        mock_propagator = mock.Mock()
        mock_propagator.to_header.return_value = test_header

        interceptor = client_interceptor.OpenCensusClientInterceptor(
            tracer=tracer, host_port='test')
        interceptor._propagator = mock_propagator
        mock_client_call_details = mock.Mock()
        mock_client_call_details.metadata = None
        mock_client_call_details.method = '/hello'

        client_call_details, request_iterator, current_span = interceptor._intercept_call(
            mock_client_call_details, mock.Mock(), 'unary_unary')

        expected_metadata = (('grpc-trace-bin', test_header), )

        self.assertEqual(expected_metadata, client_call_details.metadata)
Beispiel #13
0
                                           version='1.0.0')
            except Exception, err:
                logger.error("Could not enable debugger")
                logger.error(traceback.print_exc())
                pass
    except KeyError:
        logger.info("Debugger disabled.")

    port = os.environ.get('PORT', "8080")
    catalog_addr = os.environ.get('PRODUCT_CATALOG_SERVICE_ADDR', '')
    if catalog_addr == "":
        raise Exception(
            'PRODUCT_CATALOG_SERVICE_ADDR environment variable not set')
    logger.info("product catalog address: " + catalog_addr)

    client_tracer_interceptor = client_interceptor.OpenCensusClientInterceptor(
        tracer=tracer, host_port=catalog_addr)
    channel = grpc.insecure_channel(catalog_addr)
    channel = grpc.intercept_channel(channel, client_tracer_interceptor)
    product_catalog_stub = demo_pb2_grpc.ProductCatalogServiceStub(channel)

    # create gRPC server
    server = grpc.server(futures.ThreadPoolExecutor(max_workers=10),
                         interceptors=(tracer_interceptor, ))

    # add class to gRPC server
    service = RecommendationService()
    demo_pb2_grpc.add_RecommendationServiceServicer_to_server(service, server)
    health_pb2_grpc.add_HealthServicer_to_server(service, server)

    # start server
    logger.info("listening on port: " + port)
Beispiel #14
0
from opencensus.trace.ext.grpc import client_interceptor

from logger import getJSONLogger
logger = getJSONLogger('recommendationservice-server')

if __name__ == "__main__":
    # get port
    if len(sys.argv) > 1:
        port = sys.argv[1]
    else:
        port = "8080"

    try:
        exporter = stackdriver_exporter.StackdriverExporter()
        tracer = Tracer(exporter=exporter)
        tracer_interceptor = client_interceptor.OpenCensusClientInterceptor(
            tracer, host_port='localhost:' + port)
    except:
        tracer_interceptor = client_interceptor.OpenCensusClientInterceptor()

    # set up server stub
    channel = grpc.insecure_channel('localhost:' + port)
    channel = grpc.intercept_channel(channel, tracer_interceptor)
    stub = demo_pb2_grpc.RecommendationServiceStub(channel)
    # form request
    request = demo_pb2.ListRecommendationsRequest(user_id="test",
                                                  product_ids=["test"])
    # make call to server
    response = stub.ListRecommendations(request)
    logger.info(response)
import demo_pb2
import demo_pb2_grpc

from logger import getJSONLogger

logger = getJSONLogger('emailservice-client')

from opencensus.trace.tracer import Tracer
from opencensus.trace.exporters import stackdriver_exporter
from opencensus.trace.ext.grpc import client_interceptor

try:
    exporter = stackdriver_exporter.StackdriverExporter()
    tracer = Tracer(exporter=exporter)
    tracer_interceptor = client_interceptor.OpenCensusClientInterceptor(
        tracer, host_port='0.0.0.0:8080')
except:
    tracer_interceptor = client_interceptor.OpenCensusClientInterceptor()


def send_confirmation_email(email, order):
    channel = grpc.insecure_channel('0.0.0.0:8080')
    channel = grpc.intercept_channel(channel, tracer_interceptor)
    stub = demo_pb2_grpc.EmailServiceStub(channel)
    try:
        response = stub.SendOrderConfirmation(
            demo_pb2.SendOrderConfirmationRequest(email=email, order=order))
        logger.info('Request sent.')
    except grpc.RpcError as err:
        logger.error(err.details())
        logger.error('{}, {}'.format(err.code().name, err.code().value))
Beispiel #16
0
#from logger import getJSONLogger
#logger = getJSONLogger('okex-quote-client')

from opencensus.trace.tracer import Tracer
from opencensus.trace.exporters import stackdriver_exporter
from opencensus.trace.ext.grpc import client_interceptor

# 當前okex市場所有合約
__builtins__.cur_fu_instruments = []
__builtins__.cur_op_instruments = []

hostPort = 'localhost:50051'
try:
    exporter = stackdriver_exporter.StackdriverExporter()
    tracer = Tracer(exporter=exporter)
    tracer_interceptor = client_interceptor.OpenCensusClientInterceptor(
        tracer, host_port=hostPort)
except:
    tracer_interceptor = client_interceptor.OpenCensusClientInterceptor()


# 存合約市場深度
def cb_save_instr_market_data(jsonObj):
    channel = grpc.insecure_channel(hostPort)
    channel = grpc.intercept_channel(channel, tracer_interceptor)
    stub = quote_pb2_grpc.QuoteServiceStub(channel)
    try:
        response = stub.OnNotifyTicks(
            quote_pb2.Tick(symbol=jsonObj['instrument_id'],
                           timestamp=str(jsonObj['timestamp']),
                           best_bid_price=str(jsonObj['best_bid']),
                           best_bid_amount=str(jsonObj['best_bid_size']),