用戶如果需要更詳細(xì)的打點(diǎn),例如包大小,或者想要更換其他數(shù)據(jù)源,例如 influxDB,用戶可以根據(jù)自己的需求實(shí)現(xiàn) ?Trace
?接口,并通過(guò) ?WithTracer
?Option來(lái)注入。
// Tracer is executed at the start and finish of an RPC.
type Tracer interface {
Start(ctx context.Context) context.Context
Finish(ctx context.Context)
}
從 ctx 中可以獲得 RPCInfo,進(jìn)一步的從 RPCInfo 中獲取請(qǐng)求耗時(shí)、包大小和請(qǐng)求返回的錯(cuò)誤信息等,舉例:
type clientTracer struct {
// contain entities which recording metric
}
// Start record the beginning of an RPC invocation.
func (c *clientTracer) Start(ctx context.Context) context.Context {
// do nothing
return ctx
}
// Finish record after receiving the response of server.
func (c *clientTracer) Finish(ctx context.Context) {
ri := rpcinfo.GetRPCInfo(ctx)
rpcStart := ri.Stats().GetEvent(stats.RPCStart)
rpcFinish := ri.Stats().GetEvent(stats.RPCFinish)
cost := rpcFinish.Time().Sub(rpcStart.Time())
// TODO: record the cost of request
}
更多建議: