Newer
Older
#!/usr/bin/python
import os
from concurrent import futures
from . import monitoring_pb2
from . import monitoring_pb2_grpc
from grpc_health.v1 import health
from grpc_health.v1 import health_pb2
from grpc_health.v1 import health_pb2_grpc
from .logger import getJSONLogger
LOGGER = getJSONLogger('monitoringservice-server')
LOGGER.setLevel('DEBUG')
from prometheus_client import start_http_server, Summary
from prometheus_client import Counter, Gauge
MONITORING_GETINSTANTKPI_REQUEST_TIME = Summary('monitoring_getinstantkpi_processing_seconds', 'Time spent processing monitoring instant kpi request')
MONITORING_INCLUDEKPI_COUNTER = Counter('monitoring_includekpi_counter', 'Monitoring include kpi request counter')
class MonitoringServiceServicerImpl(monitoring_pb2_grpc.MonitoringServiceServicer):
# receives monitoring.KPI returns context.Empty
LOGGER.info('IncludeKpi')
# receives monitoring.KpiDevice returns context.Empty
LOGGER.info('IncludeKpi')
return context_pb2.Empty()
# receives monitoring.KpiId returns stream monitoring.Kpi
LOGGER.info('IncludeKpi')
# receives monitoring.KpiId returns monitoring.Kpi
LOGGER.info('IncludeKpi')
return monitoring_pb2.Kpi()
def start_server(address='[::]', port=PORT, max_workers=10):
serverGRPC = grpc.server(futures.ThreadPoolExecutor(max_workers=max_workers)) # ,interceptors=(tracer_interceptor,))
# add monitoring servicer class to gRPC server
monitoring_servicer = MonitoringServiceServicerImpl()
monitoring_pb2_grpc.add_MonitoringServiceServicer_to_server(monitoring_servicer, serverGRPC)
# add gRPC health checker servicer class to gRPC server
health_servicer = health.HealthServicer(
experimental_non_blocking=True, experimental_thread_pool=futures.ThreadPoolExecutor(max_workers=1))
health_pb2_grpc.add_HealthServicer_to_server(health_servicer, serverGRPC)
endpoint = '{}:{}'.format(address, port)
LOGGER.info('Listening on {}'.format(endpoint))
serverGRPC.add_insecure_port(endpoint)
health_servicer.set('', health_pb2.HealthCheckResponse.SERVING) # pylint: disable=maybe-no-member
return(serverGRPC)
def stop_server(serverGRPC, grace_period=0):
serverGRPC.stop(grace_period)
if __name__ == '__main__':
LOGGER.info('initializing monitoringService')
port = os.environ.get('PORT', str(PORT))
serverGRPC = start_server(port=port)