Commit fcbf44a9 authored by zs's avatar zs
Browse files

更新rpc拦截器

parent 2930fc3a
......@@ -56,9 +56,9 @@ func (s *server) Start(register RegisterFn) error {
// 一元拦截器
unaryInterceptors := []grpc.UnaryServerInterceptor{
serverinterceptors.UnaryTraceInterceptor, // 链路跟踪
serverinterceptors.UnaryCrashInterceptor(), // 异常捕获
serverinterceptors.UnaryCrashInterceptor, // 异常捕获
serverinterceptors.UnaryStatInterceptor(s.metrics), // 数据统计
serverinterceptors.UnaryPrometheusInterceptor(), // 监控报警
serverinterceptors.UnaryPrometheusInterceptor, // 监控报警
serverinterceptors.UnaryBreakerInterceptor,
}
unaryInterceptors = append(unaryInterceptors, s.unaryInterceptors...)
......
......@@ -2,21 +2,21 @@ package serverinterceptors
import (
"context"
"runtime/debug"
"git.zc0901.com/go/god/lib/logx"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"runtime/debug"
)
func UnaryCrashInterceptor() grpc.UnaryServerInterceptor {
return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{}, err error) {
defer handleCrash(func(r interface{}) {
err = toPanicError(r)
})
func UnaryCrashInterceptor(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo,
handler grpc.UnaryHandler) (resp interface{}, err error) {
defer handleCrash(func(r interface{}) {
err = toPanicError(r)
})
return handler(ctx, req)
}
return handler(ctx, req)
}
func StreamCrashInterceptor(srv interface{}, stream grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) (err error) {
......
......@@ -2,13 +2,14 @@ package serverinterceptors
import (
"context"
"strconv"
"time"
"git.zc0901.com/go/god/lib/prometheus"
"git.zc0901.com/go/god/lib/prometheus/metric"
"git.zc0901.com/go/god/lib/timex"
"google.golang.org/grpc"
"google.golang.org/grpc/status"
"strconv"
"time"
)
const serverNamespace = "rpc_server"
......@@ -33,16 +34,15 @@ var (
)
// UnaryPrometheusInterceptor 统计rpc服务端请求时长和状态代码
func UnaryPrometheusInterceptor() grpc.UnaryServerInterceptor {
return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
if !prometheus.Enabled() {
return handler(ctx, req)
}
startTime := timex.Now()
resp, err := handler(ctx, req)
metricServerReqDur.Observe(int64(timex.Since(startTime)/time.Millisecond), info.FullMethod)
metricServerReqCodeTotal.Inc(info.FullMethod, strconv.Itoa(int(status.Code(err))))
return resp, err
func UnaryPrometheusInterceptor(ctx context.Context, req interface{},
info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
if !prometheus.Enabled() {
return handler(ctx, req)
}
startTime := timex.Now()
resp, err := handler(ctx, req)
metricServerReqDur.Observe(int64(timex.Since(startTime)/time.Millisecond), info.FullMethod)
metricServerReqCodeTotal.Inc(info.FullMethod, strconv.Itoa(int(status.Code(err))))
return resp, err
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment