6 Star 46 Fork 28

Hyperledger/fabric

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
interceptor.go 2.78 KB
一键复制 编辑 原始数据 按行查看 历史
Jason Yellick 提交于 2018-11-19 12:49 . FAB-9568 Add metrics to Broadcast
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package grpcmetrics
import (
"context"
"strings"
"time"
"github.com/hyperledger/fabric/common/metrics"
"google.golang.org/grpc"
)
type UnaryMetrics struct {
RequestDuration metrics.Histogram
RequestsReceived metrics.Counter
RequestsCompleted metrics.Counter
}
func UnaryServerInterceptor(um *UnaryMetrics) grpc.UnaryServerInterceptor {
return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
service, method := serviceMethod(info.FullMethod)
um.RequestsReceived.With("service", service, "method", method).Add(1)
startTime := time.Now()
resp, err := handler(ctx, req)
duration := time.Since(startTime)
um.RequestDuration.With(
"service", service, "method", method, "code", grpc.Code(err).String(),
).Observe(duration.Seconds())
um.RequestsCompleted.With("service", service, "method", method, "code", grpc.Code(err).String()).Add(1)
return resp, err
}
}
type StreamMetrics struct {
RequestDuration metrics.Histogram
RequestsReceived metrics.Counter
RequestsCompleted metrics.Counter
MessagesSent metrics.Counter
MessagesReceived metrics.Counter
}
func StreamServerInterceptor(sm *StreamMetrics) grpc.StreamServerInterceptor {
return func(svc interface{}, stream grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) error {
sm := sm
service, method := serviceMethod(info.FullMethod)
sm.RequestsReceived.With("service", service, "method", method).Add(1)
wrappedStream := &serverStream{
ServerStream: stream,
messagesSent: sm.MessagesSent.With("service", service, "method", method),
messagesReceived: sm.MessagesReceived.With("service", service, "method", method),
}
startTime := time.Now()
err := handler(svc, wrappedStream)
duration := time.Since(startTime)
sm.RequestDuration.With(
"service", service, "method", method, "code", grpc.Code(err).String(),
).Observe(duration.Seconds())
sm.RequestsCompleted.With("service", service, "method", method, "code", grpc.Code(err).String()).Add(1)
return err
}
}
func serviceMethod(fullMethod string) (service, method string) {
normalizedMethod := strings.Replace(fullMethod, ".", "_", -1)
parts := strings.SplitN(normalizedMethod, "/", -1)
if len(parts) != 3 {
return "unknown", "unknown"
}
return parts[1], parts[2]
}
type serverStream struct {
grpc.ServerStream
messagesSent metrics.Counter
messagesReceived metrics.Counter
}
func (ss *serverStream) SendMsg(msg interface{}) error {
ss.messagesSent.Add(1)
return ss.ServerStream.SendMsg(msg)
}
func (ss *serverStream) RecvMsg(msg interface{}) error {
err := ss.ServerStream.RecvMsg(msg)
if err == nil {
ss.messagesReceived.Add(1)
}
return err
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/hyperledger/fabric.git
git@gitee.com:hyperledger/fabric.git
hyperledger
fabric
fabric
v1.4.8

搜索帮助