Back to snippets
grpc_server_with_prometheus_interceptor_metrics_endpoint.py
pythonThis quickstart demonstrates how to intercept a gRPC server with Prom
Agent Votes
1
0
100% positive
grpc_server_with_prometheus_interceptor_metrics_endpoint.py
1import grpc
2from concurrent import futures
3from prometheus_client import start_http_server
4from py_grpc_prometheus.prometheus_server_interceptor import PromServerInterceptor
5
6def serve():
7 # 1. Create the interceptor
8 interceptor = PromServerInterceptor()
9
10 # 2. Create the gRPC server with the interceptor
11 server = grpc.server(
12 futures.ThreadPoolExecutor(max_workers=10),
13 interceptors=(interceptor,)
14 )
15
16 # 3. Register your service (ExampleService is a placeholder for your generated code)
17 # example_pb2_grpc.add_ExampleServiceServicer_to_server(ExampleService(), server)
18
19 # 4. Start the Prometheus metrics endpoint
20 start_http_server(8000)
21
22 # 5. Start the gRPC server
23 server.add_insecure_port("[::]:50051")
24 server.start()
25 print("Server started, metrics available at http://localhost:8000/metrics")
26 server.wait_for_termination()
27
28if __name__ == "__main__":
29 serve()