def run():

    # NOTE(gRPC Python Team): .close() is possible on a channel and should be
    # used in circumstances in which the with statement does not fit the needs
    # of the code.
    with grpc.insecure_channel("localhost:50051") as channel:
        channel = intercept_channel(channel, client_interceptor())

        stub = route_guide_pb2_grpc.RouteGuideStub(channel)

        # Unary
        print("-------------- GetFeature --------------")
        guide_get_feature(stub)

        # Server streaming
        print("-------------- ListFeatures --------------")
        guide_list_features(stub)

        # Client streaming
        print("-------------- RecordRoute --------------")
        guide_record_route(stub)

        # Bidirectional streaming
        print("-------------- RouteChat --------------")
        guide_route_chat(stub)
 def wrapper_fn(self, original_func, instance, args, kwargs):
     channel = original_func(*args, **kwargs)
     tracer_provider = kwargs.get("tracer_provider")
     return intercept_channel(
         channel,
         client_interceptor(tracer_provider=tracer_provider),
     )
Ejemplo n.º 3
0
 def wrapper_fn_wrapper(self, original_func, instance, args,
                        kwargs) -> None:  # pylint: disable=W0613,R0201
     '''Wrap function for initializing the the request handler'''
     channel = original_func(*args, **kwargs)
     tracer_provider = kwargs.get("tracer_provider")
     return intercept_channel(
         channel,
         client_interceptor_wrapper(tracer_provider=tracer_provider),
     )
def run():
    # NOTE(gRPC Python Team): .close() is possible on a channel and should be
    # used in circumstances in which the with statement does not fit the needs
    # of the code.
    with grpc.insecure_channel("localhost:50051") as channel:

        channel = intercept_channel(channel, client_interceptor())

        stub = helloworld_pb2_grpc.GreeterStub(channel)

        # stub.SayHello is a _InterceptorUnaryUnaryMultiCallable
        response = stub.SayHello(helloworld_pb2.HelloRequest(name="YOU"))

    print("Greeter client received: " + response.message)
        SimpleSpanProcessor(CloudTraceSpanExporter())
    )
    propagate.set_global_textmap(CloudTraceFormatPropagator())

    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)

    # Create the gRPC client channel to ProductCatalog (server).
    channel = grpc.insecure_channel(catalog_addr)

    # OpenTelemetry client interceptor passes trace contexts to the server.
    channel = intercept_channel(
        channel, client_interceptor(trace.get_tracer_provider()))
    product_catalog_stub = demo_pb2_grpc.ProductCatalogServiceStub(channel)

    # Create the gRPC server for accepting ListRecommendations Requests from frontend (client).
    interceptor = server_interceptor(trace.get_tracer_provider())
    server = grpc.server(futures.ThreadPoolExecutor(max_workers=10),
                         interceptors=(interceptor,))

    # Add RecommendationService 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)
    server.add_insecure_port('[::]:'+port)