我的 grpc 服务器有一个日志拦截器,并且想向元数据添加一个值(我想在整个生命周期内跟踪请求):
func (m *middleware) loggingInterceptor(srv interface{},
ss grpc.ServerStream,
info *grpc.StreamServerInfo,
handler grpc.StreamHandler)
md, ok := metadata.FromIncomingContext(ss.Context())
if !ok {
return errors.New("could not get metadata from incoming stream context")
}
// add the transaction id to the metadata so that business logic can track it
md.Append("trans-id", "some-transaction-id")
// call the handler func
return handler(srv, ss)
}
但文档FromIncomingContext声明:
// FromIncomingContext returns the incoming metadata in ctx if it exists. The
// returned MD should not be modified. Writing to it may cause races.
// Modification should be made to copies of the returned MD.
好的,所以我看一下复制功能并复制元数据:
mdCopy := md.Copy()
mdCopy.Append("trans-id", "some-transaction-id")
并想“我如何将这个元数据附加回ServerStream上下文?”,然后我检查是否有一些ss.SetContext(newCtx),但我没有看到任何类似的东西。我是从错误的角度考虑这个问题,还是我错过了其他东西?
人到中年有点甜
慕运维8079593
相关分类