Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
go
god
Commits
fcbf44a9
Commit
fcbf44a9
authored
3 years ago
by
zs
Browse files
Options
Download
Email Patches
Plain Diff
更新rpc拦截器
parent
2930fc3a
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
23 additions
and
23 deletions
+23
-23
rpc/internal/rpcserver.go
rpc/internal/rpcserver.go
+2
-2
rpc/internal/serverinterceptors/crash_interceptor.go
rpc/internal/serverinterceptors/crash_interceptor.go
+8
-8
rpc/internal/serverinterceptors/prometheus_interceptor.go
rpc/internal/serverinterceptors/prometheus_interceptor.go
+13
-13
No files found.
rpc/internal/rpcserver.go
View file @
fcbf44a9
...
...
@@ -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
...
)
...
...
This diff is collapsed.
Click to expand it.
rpc/internal/serverinterceptors/crash_interceptor.go
View file @
fcbf44a9
...
...
@@ -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
.
UnaryServerIn
terceptor
{
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
.
UnaryServerIn
fo
,
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
)
{
...
...
This diff is collapsed.
Click to expand it.
rpc/internal/serverinterceptors/prometheus_interceptor.go
View file @
fcbf44a9
...
...
@@ -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
}
This diff is collapsed.
Click to expand it.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment