Skip to content

Commit

Permalink
Merge pull request #405 from askuy/feature/slowlog20240625
Browse files Browse the repository at this point in the history
add recover type
  • Loading branch information
askuy authored Jun 26, 2024
2 parents a2c0444 + 91849df commit c4e3af6
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 2 deletions.
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

0 comments on commit c4e3af6

Please sign in to comment.