Prometheus monitoring for gRPC C# servers and clients.
This project was inspired by grpc-ecosystem/go-grpc-prometheus and grpc-ecosystem/java-grpc-prometheus
Library is build in .NET standart 2.0 and uses prometheus-net for Prometheus metrics handling.
You can set up client-side or server-side interceptor.
ServerInterceptor interceptor = new ServerInterceptor(hostname: "127.0.0.1", port: "1234");
Server server = new Server();
server.Services.Add(TestService.BindService(new TestServiceImp()).Intercept(interceptor));
server.Ports.Add(new ServerPort(host: "127.0.0.1", port: 50051, credentials: ServerCredentials.Insecure));
server.Start();
ClientInterceptor interceptor = new ClientInterceptor(hostname: "127.0.0.1", port: "1234");
Channel channel = new Channel(host: "127.0.0.1", port: 50051, credentials: ChannelCredentials.Insecure);
TestService.TestServiceClient client = new TestService.TestServiceClient(channel.Intercept(interceptor));
Metrics can be found on hostname and port specified in constructor for ServerInterceptor
or ClientInterceptor
under the /metrics
endpoint.
All server-side metrics start with grpc_server
as Prometheus subsystem name. All client-side metrics start with grpc_client
.
Both of them have mirror-concepts. Similarly all methods contain the same rich labels:
grpc_service
- the gRPC service name, which is the combination ofprotobuf
package and thegrpc_service
section name. E.g. forpackage NetGrpcPrometheusTest;
and serviceTestService
the label will begrpc_service="NetGrpcPrometheusTest.TestService"
grpc_method
- the name of the method called on the gRPC service. E.g.grpc_method="Ping"
grpc_type
- the gRPC type of request
Additionally for completed RPCs, the following labels are used:
grpc_code
- the human-readable gRPC status code
There are four types of counters defined for both client and server side:
grpc_server_started_total
- counts total calls started on the servergrpc_server_handled_total
- counts total calls handled and sent back to client from the servergrpc_server_msg_received_total
- counts total number of messages received through the streamsgrpc_server_msg_sent_total
- counts total number of messages sent through the streams
grpc_client_started_total
- counts total calls sent to the servergrpc_client_handled_total
- counts total calls received from the servergrpc_client_msg_received_total
- counts total number of messages received through the streamsgrpc_client_msg_sent_total
- counts total number of messages sent through the streams
For more detailed documention go to (MetricsBase.cs)[NetGrpcPrometheus/Helpers/MetricsBase.cs]
Prometheus histograms are a great way to measure latency distributions of your RPCs. However since it is bad practice to have metrics of high cardinality) the latency monitoring metrics are disabled by default. To enable them please call the following in your server initialization code:
ServerInterceptor interceptor = new ServerInterceptor(hostname: "127.0.0.1", port: "1234");
interceptor.EnableLatencyMetrics = true;
After the call completes, it's handling time will be recorded in a Prometheus histogram variable grpc_server_handling_seconds. It 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
For custom metrics follow guidelines on prometheus-net. Please note that MetricsServer is already running so you can jump directly to creating the metrics.
Please find them on grpc-ecosystem/go-grpc-prometheus
Please find the license here