Enable gRPC reflection in Python-based services
Proposers
- Chris Aslanoglou (UBITECH)
Description
gRPC reflection is quite useful for development and debugging and enables usage of grpcurl
.
It's currently enabled in automation component, let's enable it in the Python based services too.
Example for Context
diff --git a/src/context/requirements.in b/src/context/requirements.in
index e80c645..2e925f1 100644
--- a/src/context/requirements.in
+++ b/src/context/requirements.in
@@ -1,5 +1,6 @@
flask-restful
grpcio-health-checking
+grpcio-reflection
grpcio
prometheus-client
pytest
diff --git a/src/context/service/grpc_server/ContextService.py b/src/context/service/grpc_server/ContextService.py
index 9f1028d..4894cae 100644
--- a/src/context/service/grpc_server/ContextService.py
+++ b/src/context/service/grpc_server/ContextService.py
@@ -4,8 +4,10 @@ from concurrent import futures
from grpc_health.v1.health import HealthServicer, OVERALL_HEALTH
from grpc_health.v1.health_pb2 import HealthCheckResponse
from grpc_health.v1.health_pb2_grpc import add_HealthServicer_to_server
+from grpc_reflection.v1alpha import reflection
from context.Config import GRPC_SERVICE_PORT, GRPC_MAX_WORKERS, GRPC_GRACE_PERIOD
from context.proto.context_pb2_grpc import add_ContextServiceServicer_to_server
+from context.proto.context_pb2 import DESCRIPTOR as CONTEXT_DESCRIPTOR
from .ContextServiceServicerImpl import ContextServiceServicerImpl
BIND_ADDRESS = '0.0.0.0'
@@ -43,6 +45,13 @@ class ContextService:
experimental_non_blocking=True, experimental_thread_pool=futures.ThreadPoolExecutor(max_workers=1))
add_HealthServicer_to_server(self.health_servicer, self.server)
+ SERVICE_NAMES = (
+ CONTEXT_DESCRIPTOR.services_by_name['ContextService'].full_name,
+ reflection.SERVICE_NAME,
+ )
+ reflection.enable_server_reflection(SERVICE_NAMES, self.server)
+ self.server.add_insecure_port('[::]:50051')
+
port = self.server.add_insecure_port(self.endpoint)
self.endpoint = '{:s}:{:s}'.format(str(self.address), str(port))
LOGGER.info('Listening on {:s}...'.format(str(self.endpoint)))