grpc 拦截器 python_Python py-grpc-prometheus包_程序模块 - PyPI - Python中文网

PY GRPC普罗米修斯

提供普罗米修斯度量的仪器库类似于:

状态

目前,该库具有与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获取完整示例。importgrpcfromconc

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是在 Python 环境下配置 gRPC 的步骤: 1. 安装 gRPC 和 protobuf ``` pip install grpcio pip install grpcio-tools pip install protobuf ``` 2. 编写 .proto 文件 这里以一个简单的示例为例: ``` syntax = "proto3"; message HelloRequest { string name = 1; } message HelloResponse { string message = 1; } service HelloService { rpc SayHello (HelloRequest) returns (HelloResponse) {} } ``` 3. 生成 Python 代码 在命令行中运行以下命令: ``` python -m grpc_tools.protoc -I./protos --python_out=. --grpc_python_out=. ./protos/hello.proto ``` 其中,`./protos/hello.proto` 是你编写的 .proto 文件路径,`--python_out` 参数表示生成的 Python 代码输出路径,`--grpc_python_out` 表示生成的 gRPC Python 代码输出路径。 4. 编写 gRPC 服务端代码 ```python import grpc from concurrent import futures import time import hello_pb2 import hello_pb2_grpc class HelloServicer(hello_pb2_grpc.HelloServiceServicer): def SayHello(self, request, context): return hello_pb2.HelloResponse(message='Hello, %s!' % request.name) def serve(): server = grpc.server(futures.ThreadPoolExecutor(max_workers=10)) hello_pb2_grpc.add_HelloServiceServicer_to_server(HelloServicer(), server) server.add_insecure_port('[::]:50051') server.start() try: while True: time.sleep(3600) except KeyboardInterrupt: server.stop(0) if __name__ == '__main__': serve() ``` 5. 编写 gRPC 客户端代码 ```python import grpc import hello_pb2 import hello_pb2_grpc def run(): with grpc.insecure_channel('localhost:50051') as channel: stub = hello_pb2_grpc.HelloServiceStub(channel) response = stub.SayHello(hello_pb2.HelloRequest(name='World')) print("Greeter client received: " + response.message) if __name__ == '__main__': run() ``` 以上就是在 Python 环境下配置 gRPC 的步骤。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值