sogou / srpc

RPC framework based on C++ Workflow. Supports SRPC, Baidu bRPC, Tencent tRPC, thrift protocols.
Apache License 2.0
1.95k stars 385 forks source link

add metrics #213

Closed holmes1412 closed 2 years ago

holmes1412 commented 2 years ago
  1. add var from project workflow-prometheus;
    • refactor collect() of each VARs;
    • add RPCVarCollector, add APIs according to the most common TYPE int64_t and double (refer to OpenTelemetry);
  2. add proto files from project OpenTelemetry;
  3. add metrics module and filter :
    • add RPCMetricsPull for prometheus and VARs output as std::string;
    • add RPCMetricsOTel for OpenTelemetry and VARs output as Protobuf::Message; (collect and report is not finished)
    • move some api from RPCVarFactory to RPCMetricsFilter;
    • make collect() implemented by each filter, since each requires different output data kinds;
  4. add tutorial-16 and test the report to prometheus;

Basic Usage:

RPCMetricsPull filter;

class ExampleServiceImpl : public Example::Service
{
public:
    void Echo(EchoRequest *req, EchoResponse *resp, RPCContext *ctx) override
    {
        filter.histogram("echo_request_size")->observe(req->ByteSizeLong());
    }
};

int main()
{
    // ...
    filter.init(8080); /* export port for prometheus */
    filter.create_histogram("echo_request_size", "Echo request size", {1, 10, 100});
    server.add_filter(&filter);
    // ...
    filter.deinit();
    return 0;
}

Screen Output:

[@gdylj_35_53 tutorial]# ./srpc_pb_client 
message: "Hi back"

message: "Hi back"

^C
[@gdylj_35_53 srpc_1412_metrics]# curl localhost:8080/metrics
# HELP total_request_count total request count
# TYPE total_request_count gauge
total_request_count 2.000000
# HELP total_request_method request method statistics
# TYPE total_request_method counter
total_request_method{method="Echo",service="Example"} 2.000000
# HELP total_request_latency request latency nano seconds
# TYPE total_request_latency summary
total_request_latency{quantile="0.500000"} 645078.500000
total_request_latency{quantile="0.900000"} 645078.500000
total_request_latency_sum 1290157.000000
total_request_latency_count 2
# HELP echo_request_size Echo request size
# TYPE echo_request_size histogram
echo_request_size_bucket{le="1.000000"}0
echo_request_size_bucket{le="10.000000"}0
echo_request_size_bucket{le="100.000000"}2
echo_request_size_bucket{le="+Inf"} 2
echo_request_size_sum 40.000000
echo_request_size_count 2