Package: py-grpc-prometheus
py-grpc-prometheusInstrument library to provide prometheus metrics similar to:https://github.com/grpc-ecosystem/java-grpc-prometheushttps://github.com/grpc-ecosystem/go-grpc-prometheusStatusCurrently, the library has the parity metrics with the Java and Go library.Server side:grpc_server_started_totalgrpc_server_handled_totalgrpc_server_msg_received_totalgrpc_server_msg_sent_totalgrpc_server_handling_secondsClient side:grpc_client_started_totalgrpc_client_handled_totalgrpc_client_msg_received_totalgrpc_client_msg_sent_totalgrpc_client_handling_secondsgrpc_client_msg_recv_handling_secondsgrpc_client_msg_send_handling_secondsHow to usepip install py-grpc-prometheusClient side:Client metrics monitoring is done by intercepting the gPRC channel.importgrpcfrompy_grpc_prometheus.prometheus_client_interceptorimportPromClientInterceptorchannel=grpc.intercept_channel(grpc.insecure_channel('server:6565'),PromClientInterceptor())# Start an end point to expose metrics.start_http_server(metrics_port)Server side:Server metrics are exposed by adding the interceptor when the gRPC server is started. Take a look attests/integration/hello_world/hello_world_client.pyfor the complete example.importgrpcfromconcurrentimportfuturesfrompy_grpc_prometheus.prometheus_server_interceptorimportPromServerInterceptorfromprometheus_clientimportstart_http_serverStart the gRPC server with the interceptor, take a look attests/integration/hello_world/hello_world_server.pyfor the complete example.server=grpc.server(futures.ThreadPoolExecutor(max_workers=10),interceptors=(PromServerInterceptor(),))# Start an end point to expose metrics.start_http_server(metrics_port)HistogramsPrometheus histogramsare a great way
to measure latency distributions of your RPCs. However, since it is bad practice to have metrics
ofhigh cardinalitythe latency monitoring metrics are disabled by default. To enable them please call the following
in your interceptor initialization code:server = grpc.server(futures.ThreadPoolExecutor(max_workers=10),
                     interceptors=(PromServerInterceptor(enable_handling_time_histogram=True),))After the call completes, its handling time will be recorded in aPrometheus histogramvariablegrpc_server_handling_seconds. The histogram variable contains three sub-metrics:grpc_server_handling_seconds_count- the count of all completed RPCs by status and methodgrpc_server_handling_seconds_sum- cumulative time of RPCs by status and method, useful for
calculating average handling timesgrpc_server_handling_seconds_bucket- contains the counts of RPCs by status and method in respective
handling-time buckets. These buckets can be used by Prometheus to estimate SLAs (seehere)Server Side:enable_handling_time_histogram: Enables 'grpc_server_handling_seconds'Client Side:enable_client_handling_time_histogram: Enables 'grpc_client_handling_seconds'enable_client_stream_receive_time_histogram: Enables 'grpc_client_msg_recv_handling_seconds'enable_client_stream_send_time_histogram: Enables 'grpc_client_msg_send_handling_seconds'Legacy metrics:Metric names have been updated to be in line with those fromhttps://github.com/grpc-ecosystem/go-grpc-prometheus.The legacy metrics are:server side:grpc_server_started_totalgrpc_server_handled_totalgrpc_server_handled_latency_secondsgrpc_server_msg_received_totalgrpc_server_msg_sent_totalclient side:grpc_client_started_totalgrpc_client_completedgrpc_client_completed_latency_secondsgrpc_client_msg_sent_totalgrpc_client_msg_received_totalIn order to be able to use these legacy metrics for backwards compatibility, thelegacyflag can be set toTruewhen initialising the server/client interceptorsFor example, to enable the server side legacy metrics:server = grpc.server(futures.ThreadPoolExecutor(max_workers=10),
                     interceptors=(PromServerInterceptor(legacy=True),))How to run and testmakeinitialize-development
maketestTODO:Unit test withhttps://github.com/census-instrumentation/opencensus-python/blob/master/tests/unit/trace/ext/grpc/test_server_interceptor.pyReferencehttps://grpc.io/grpc/python/grpc.htmlhttps://github.com/census-instrumentation/opencensus-python/blob/master/opencensus/trace/ext/grpc/utils.pyhttps://github.com/opentracing-contrib/python-grpc/blob/b4bdc7ce81fa75ede00f7c6bcf5dab8fae47332a/grpc_opentracing/grpcext/grpc_interceptor/server_interceptor.py