mirror of
https://github.com/SigNoz/signoz.git
synced 2026-03-24 13:20:27 +00:00
* feat(middleware): add panic recovery middleware with TypeFatal error type Add a global HTTP recovery middleware that catches panics, logs them with OTel exception semantic conventions via errors.Attr, and returns a safe user-facing error response. Introduce TypeFatal/CodeFatal for unrecoverable failures and WithStacktrace to attach pre-formatted stack traces to errors. Remove redundant per-handler panic recovery blocks in querier APIs. * style(errors): keep WithStacktrace call on same line in test * fix(middleware): replace fmt.Errorf with errors.New in recovery test * feat(middleware): add request context to panic recovery logs Capture request body before handler runs and include method, path, and body in panic recovery logs using OTel semconv attributes. Improve error message to direct users to GitHub issues or support.
46 lines
1.0 KiB
Go
46 lines
1.0 KiB
Go
package errors
|
|
|
|
import (
|
|
"fmt"
|
|
"runtime"
|
|
"strings"
|
|
)
|
|
|
|
// stacktrace holds a snapshot of program counters.
|
|
type stacktrace []uintptr
|
|
|
|
// newStackTrace captures a stack trace, skipping 3 frames to record the
|
|
// snapshot at the origin of the error:
|
|
// 1. runtime.Callers
|
|
// 2. newStackTrace
|
|
// 3. the constructor (New, Newf, Wrapf, Wrap)
|
|
//
|
|
// Inspired by https://github.com/thanos-io/thanos/blob/main/pkg/errors/stacktrace.go
|
|
func newStackTrace() stacktrace {
|
|
const depth = 16
|
|
pc := make([]uintptr, depth)
|
|
n := runtime.Callers(3, pc)
|
|
return stacktrace(pc[:n:n])
|
|
}
|
|
|
|
// String formats the stacktrace as function/file/line pairs.
|
|
func (s stacktrace) String() string {
|
|
var buf strings.Builder
|
|
frames := runtime.CallersFrames(s)
|
|
for {
|
|
frame, more := frames.Next()
|
|
fmt.Fprintf(&buf, "%s\n\t%s:%d\n", frame.Function, frame.File, frame.Line)
|
|
if !more {
|
|
break
|
|
}
|
|
}
|
|
return buf.String()
|
|
}
|
|
|
|
// rawStacktrace holds a pre-formatted stacktrace string.
|
|
type rawStacktrace string
|
|
|
|
func (r rawStacktrace) String() string {
|
|
return string(r)
|
|
}
|