-
Notifications
You must be signed in to change notification settings - Fork 96
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(log): print traceback when panics occur (#2166)
Signed-off-by: Petu Eusebiu <peusebiu@cisco.com>
- Loading branch information
1 parent
d1bf713
commit ee9bbb0
Showing
6 changed files
with
122 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package api | ||
|
||
import ( | ||
"encoding/json" | ||
"net/http" | ||
"runtime" | ||
|
||
"github.com/gorilla/mux" | ||
|
||
"zotregistry.io/zot/pkg/log" | ||
) | ||
|
||
type Stack struct { | ||
Frames []Frame `json:"stack"` | ||
} | ||
|
||
type Frame struct { | ||
Name string `json:"function"` | ||
File string `json:"file"` | ||
Line int `json:"line"` | ||
} | ||
|
||
// RecoveryHandler is a HTTP middleware that recovers from a panic. | ||
// It logs the panic and its traceback in json format, writes http.StatusInternalServerError | ||
// and continues to the next handler. | ||
func RecoveryHandler(log log.Logger) mux.MiddlewareFunc { | ||
return func(next http.Handler) http.Handler { | ||
return http.HandlerFunc(func(response http.ResponseWriter, request *http.Request) { | ||
defer func() { | ||
if err := recover(); err != nil { | ||
response.WriteHeader(http.StatusInternalServerError) | ||
|
||
stack := Stack{ | ||
Frames: getStacktrace(), | ||
} | ||
|
||
recoveredErr, ok := err.(error) | ||
if ok { | ||
buf, err := json.Marshal(stack) | ||
if err == nil { | ||
log.Error().Err(recoveredErr).RawJSON("traceback", buf).Msg("panic recovered") //nolint: check-logs | ||
} | ||
} | ||
} | ||
}() | ||
|
||
// Process request | ||
next.ServeHTTP(response, request) | ||
}) | ||
} | ||
} | ||
|
||
func getStacktrace() []Frame { | ||
stack := []Frame{} | ||
//nolint: varnamelen | ||
pc := make([]uintptr, 64) | ||
|
||
n := runtime.Callers(0, pc) | ||
if n == 0 { | ||
return []Frame{} | ||
} | ||
|
||
// first three frames are from this file, don't need them. | ||
pc = pc[3:] | ||
frames := runtime.CallersFrames(pc) | ||
|
||
// loop to get frames. | ||
for { | ||
frame, more := frames.Next() | ||
|
||
// store this frame | ||
stack = append(stack, Frame{ | ||
Name: frame.Function, | ||
File: frame.File, | ||
Line: frame.Line, | ||
}) | ||
|
||
// check whether there are more frames to process after this one. | ||
if !more { | ||
break | ||
} | ||
} | ||
|
||
return stack | ||
} |