Skip to content

Commit

Permalink
Report request URI at /debug/500
Browse files Browse the repository at this point in the history
  • Loading branch information
alpinskiy committed Nov 20, 2024
1 parent ee4e1e1 commit 6db9602
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 8 deletions.
6 changes: 3 additions & 3 deletions internal/api/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -676,8 +676,8 @@ func NewHandler(staticDir fs.FS, jsSettings JSSettings, showInvisible bool, chV1
return h, nil
}

func (h *Handler) savePanic(err any, stack []byte) {
v := error500{time.Now(), err, stack}
func (h *Handler) savePanic(requestURI string, err any, stack []byte) {
v := error500{time: time.Now(), requestURI: requestURI, what: err, stack: stack}
h.errorsMu.Lock()
h.errors[h.errorX] = v
h.errorX = (h.errorX + 1) % len(h.errors)
Expand Down Expand Up @@ -2113,7 +2113,7 @@ func (h *requestHandler) queryBadges(ctx context.Context, req seriesRequest, met
defer func() {
var code int
if r := recover(); r != nil {
h.savePanic(r, debug.Stack())
h.savePanic("/api/badges", r, debug.Stack())
code = 500
} else {
code = httpCode(err)
Expand Down
10 changes: 6 additions & 4 deletions internal/api/http_router.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,10 @@ type httpResponseWriter struct {
}

type error500 struct {
time time.Time
what any
stack []byte
time time.Time
requestURI string
what any
stack []byte
}

func NewHTTPRouter(h *Handler) httpRouter {
Expand Down Expand Up @@ -132,7 +133,7 @@ func (r *httpRoute) handle(w http.ResponseWriter, req *http.Request) {
if !h.w.statusCodeSent {
http.Error(&h.w, fmt.Sprint(err), http.StatusInternalServerError)
}
h.savePanic(err, debug.Stack())
h.savePanic(req.RequestURI, err, debug.Stack())
}
h.endpointStat.report(h.w.statusCode, format.BuiltinMetricNameAPIResponseTime)
}()
Expand All @@ -154,6 +155,7 @@ func DumpInternalServerErrors(r *httpRequestHandler) {
defer r.errorsMu.RUnlock()
for i := 0; i < len(r.errors) && r.errors[i].what != nil; i++ {
w.Write([]byte("# \n"))
w.Write([]byte("# " + r.errors[i].requestURI + " \n"))
w.Write([]byte(fmt.Sprintf("# %s \n", r.errors[i].what)))
w.Write([]byte("# " + r.errors[i].time.Format(time.RFC3339) + " \n"))
w.Write([]byte("# \n"))
Expand Down
2 changes: 1 addition & 1 deletion internal/api/rpc_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func (h *rpcRouter) Handle(ctx context.Context, hctx *rpc.HandlerContext) (err e
defer func() {
var code int // "0" means "success"
if r := recover(); r != nil {
w.savePanic(r, debug.Stack())
w.savePanic(hctx.RequestFunctionName, r, debug.Stack())
code = rpc.TlErrorInternal
err = &rpc.Error{Code: rpc.TlErrorInternal}
} else if err != nil {
Expand Down

0 comments on commit 6db9602

Please sign in to comment.