1# gRPC Custom Metrics Example 2 3You can find a complete set of instructions for building gRPC and running the 4examples in the [C++ Quick Start][]. 5 6This example shows how to implement a server that provides custom metrics usable 7by custom load balancing policies. 8 9Server needs to be setup with metrics recorder and Orca service for sending 10these metrics to a client: 11 12```c++ 13GreeterServiceImpl service; 14// Setup custom metrics recording 15auto server_metric_recorder = 16 grpc::experimental::ServerMetricRecorder::Create(); 17grpc::experimental::OrcaService orca_service( 18 server_metric_recorder.get(), 19 grpc::experimental::OrcaService::Options().set_min_report_duration( 20 absl::Seconds(0.1))); 21builder.RegisterService(&orca_service); 22grpc::ServerBuilder::experimental_type(&builder).EnableCallMetricRecording( 23 nullptr); 24``` 25 26Afterwards per-request metrics can be reported from the gRPC service 27implementation using the metric recorder from the request context: 28 29```c++ 30auto recorder = context->ExperimentalGetCallMetricRecorder(); 31if (recorder == nullptr) { 32 return Status(grpc::StatusCode::INTERNAL, 33 "Unable to access metrics recorder. Make sure " 34 "EnableCallMetricRecording had been called."); 35} 36recorder->RecordCpuUtilizationMetric(0.5); 37``` 38 39Out of band metrics can be reported using the `server_metric_recorder` 40directly: 41 42```c++ 43server_metric_recorder->SetCpuUtilization(0.75); 44``` 45 46[C++ Quick Start]: https://grpc.io/docs/languages/cpp/quickstart 47