python-grpc-prometheus拦截器
py-grpc-prometheus的Python项目详细描述
PY GRPC普罗米修斯
提供普罗米修斯度量的仪器库类似于:
- https://github.com/grpc-ecosystem/java-grpc-prometheus
- https://github.com/grpc-ecosystem/go-grpc-prometheus
状态
目前,该库具有与java和go库的奇偶性度量。
服务器端:
- grpc_server_started_total
- GRPC U SERVER U HANDED U总计
- GRPC U服务器处理的延迟时间秒
- grpc_server_msg_received_total
- GRPC U SERVER U MSG U SENT U总计
客户端:
- GRPC_客户端_已启动总计
- GRPC_客户端已完成
- grpc_client_completed_latency_秒
- grpc_client_msg_sent_total
- GRPC_客户端_消息_已收到总计
如何使用
pip install py-grpc-prometheus
客户端:
客户端度量监控是通过截取gprc通道来完成的。
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)
服务器端:
当grpc服务器启动时,通过添加拦截器来公开服务器度量。看一看
tests/integration/hello_world/hello_world_client.py
获取完整示例。
importgrpcfromconcurrentimportfuturesfrompy_grpc_prometheus.prometheus_server_interceptorimportPromServerInterceptorfromprometheus_clientimportstart_http_server
用拦截器启动grpc服务器,看看
tests/integration/hello_world/hello_world_server.py
获取完整示例。
server=grpc.server(futures.ThreadPoolExecutor(max_workers=10),interceptors=(PromServerInterceptor(),))# Start an end point to expose metrics.start_http_server(metrics_port)
如何运行和测试
- 运行测试服务器
python -m tests.integration.hello_world.hello_world_sever
- 运行测试客户端
python -m tests.integration.hello_world.hello_world_client
- 打开http://localhost:50052获取服务器端指标
- 打开http://localhost:50053获取客户端指标
待办事项:
参考
- https://grpc.io/grpc/python/grpc.html
- https://github.com/census-instrumentation/opencensus-python/blob/master/opencensus/trace/ext/grpc/utils.py
- https://github.com/opentracing-contrib/python-grpc/blob/b4bdc7ce81fa75ede00f7c6bcf5dab8fae47332a/grpc_opentracing/grpcext/grpc_interceptor/server_interceptor.py