英文:
Access information about the request and response payloads in grpc-go's stat/HandleRPC
问题
我正在使用stats/HandleRPC()来记录有关RPC持续时间的一些指标,当我收到stats/End数据时,我想要使用一些从传入和传出负载中提取的信息对指标进行标记。实现这一目标的最佳方法是什么?
func (h *myStatsHandler) HandleRPC(ctx context.Context, rpcStats stats.RPCStats) {
switch stat := rpcStats.(type) {
case *stats.End:
durationMs := stat.EndTime.Sub(stat.BeginTime).Seconds() * 1000.0
// 在发送这个值之前,我需要知道例如请求负载中特定键的值,或者响应是否为nil
}
}
英文:
I am using stats/HandleRPC() to emit some metrics about the RPC duration, when I receive the stats/End data, and I want to tag the metrics with some information that can be extracted from the incoming and outgoing payloads. What would be the best way to achieve this?
func (h *myStatsHandler) HandleRPC(ctx context.Context, rpcStats stats.RPCStats) {
switch stat := rpcStats.(type) {
case *stats.End:
durationMs := stat.EndTime.Sub(stat.BeginTime).Seconds() * 1000.0
// Now before sending this value, I need to know, for example the value of a specific key in the request payload, or whether the response is nil or not
}
}
答案1
得分: 1
在你的TagRPC
实现中,你可以创建一个结构体并将其指针添加到上下文中。然后,在连续调用HandleRPC
时,在其中添加信息。因此,如果你需要从仅在*stats.InPayload
调用中可用的Payload中获取某些内容,你可以将其提取出来并存储在你添加到上下文中的结构体中,然后在后续调用HandleRPC
时再次访问它。
type recorderCtxKey struct{}
type recorder struct {
size int64
}
func (sl *statsHandler) TagRPC(ctx context.Context, info *stats.RPCTagInfo) context.Context {
return context.WithValue(ctx, rpcStatCtxKey{}, &recorder{})
}
func (h *statsHandler) HandleRPC(ctx context.Context, rpcStats stats.RPCStats) {
switch stat := rpcStats.(type) {
case *stats.InPayload:
r, _ := ctx.Value(recorderContextKey{}).(*Recorder)
r.size += stat.WireLength
case *stats.End:
durationMs := stat.EndTime.Sub(stat.BeginTime).Seconds() * 1000.0
r, _ := ctx.Value(recorderContextKey{}).(*Recorder)
# 使用 r.size #
}
}
英文:
In your implementation of TagRPC
, you can create a struct and add a pointer to it to the context. Then add information in it over the successive calls to HandleRPC
. So if you need something from the Payload that's only available in the *stats.InPayload
invocation, you can pull it out and store it in the struct you added to the context, and then access it later when HandleRPC
is called again with *stats.End
type recorderCtxKey struct{}
type recorder struct {
size int64
}
func (sl *statsHandler) TagRPC(ctx context.Context, info *stats.RPCTagInfo) context.Context {
return context.WithValue(ctx, rpcStatCtxKey{}, &recorder{})
}
func (h *statsHandler) HandleRPC(ctx context.Context, rpcStats stats.RPCStats) {
switch stat := rpcStats.(type) {
case *stats.InPayload:
r, _ := ctx.Value(recorderContextKey{}).(*Recorder)
r.size += stat.WireLength
case *stats.End:
durationMs := stat.EndTime.Sub(stat.BeginTime).Seconds() * 1000.0
r, _ := ctx.Value(recorderContextKey{}).(*Recorder)
# use r.size #
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论