Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add recover type #405

Merged
merged 1 commit into from
Jun 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions server/egin/interceptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ func (c *Container) defaultServerInterceptor() gin.HandlerFunc {
stackInfo := stack(3)
fields = append(fields,
elog.FieldEvent(event),
elog.FieldType("recover"),
zap.ByteString("stack", stackInfo),
elog.FieldErrAny(rec),
elog.FieldCode(int32(ctx.Writer.Status())),
Expand Down
3 changes: 2 additions & 1 deletion server/egin/interceptor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func TestPanicInHandler(t *testing.T) {
n := strings.Index(string(logged), "{")
err1 := json.Unmarshal(logged[n:], &m)
assert.NoError(t, err1)
//assert.Contains(t, m["event"], `recover`)
assert.Contains(t, m["type"], `recover`)
assert.Contains(t, m["lv"], `error`)
assert.Contains(t, string(logged), "we have a panic")
assert.Contains(t, m["method"], `GET./recovery`)
Expand Down Expand Up @@ -93,6 +93,7 @@ func TestPanicInCustomHandler(t *testing.T) {
n := strings.Index(string(logged), "{")
err1 := json.Unmarshal(logged[n:], &m)
assert.NoError(t, err1)
assert.Equal(t, m["type"], `recover`)
assert.Equal(t, m["lv"], `error`)
assert.Equal(t, m["error"], panicMessage)
assert.Equal(t, m["method"], `GET./recovery`)
Expand Down
6 changes: 5 additions & 1 deletion server/egrpc/interceptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@ func (c *Container) defaultUnaryServerInterceptor() grpc.UnaryServerInterceptor
// 此处必须使用defer来recover handler内部可能出现的panic
defer func() {
cost := time.Since(beg)
logType := ""
if rec := recover(); rec != nil {
switch recType := rec.(type) {
case error:
Expand All @@ -262,7 +263,7 @@ func (c *Container) defaultUnaryServerInterceptor() grpc.UnaryServerInterceptor
stack := make([]byte, 4096)
stack = stack[:runtime.Stack(stack, true)]
fields = append(fields, elog.FieldStack(stack))
event = "recover"
logType = "recover"
err = status.New(grpcCode.Internal, "panic recover, origin err: "+err.Error()).Err()
}

Expand All @@ -281,6 +282,9 @@ func (c *Container) defaultUnaryServerInterceptor() grpc.UnaryServerInterceptor

httpStatusCode := ecode.GrpcToHTTPStatusCode(spbStatus.Code())

if logType == "recover" {
fields = append(fields, elog.FieldType("recover"))
}
fields = append(fields,
elog.FieldKey("unary"),
elog.FieldCode(int32(spbStatus.Code())),
Expand Down
Loading