feat: add tracing logs after process is complete (#8680)

- Add the written HTTP status after completing the HTTP response. This makes it easier to find that one request that returns a different status code (ref. https://codeberg.org/Codeberg/Community/issues/2049#issue-1972600)
- Add the affected amount of rows and last insert ID after the SQL query is done, I have not yet a concrete use-case but this might help with debugging which ID corresponds to some SQL query that someone might want to take a closer look at and if some SQL query affects more than necessary amount of rows.

Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/8680
Reviewed-by: Earl Warren <earl-warren@noreply.codeberg.org>
Co-authored-by: Gusted <postmaster@gusted.xyz>
Co-committed-by: Gusted <postmaster@gusted.xyz>
This commit is contained in:
Gusted 2025-07-26 05:44:58 +02:00 committed by Earl Warren
commit 02de040a5e
2 changed files with 14 additions and 1 deletions

View file

@ -386,6 +386,15 @@ func (TracingHook) BeforeProcess(c *contexts.ContextHook) (context.Context, erro
} }
func (TracingHook) AfterProcess(c *contexts.ContextHook) error { func (TracingHook) AfterProcess(c *contexts.ContextHook) error {
if c.Result != nil {
if rowsAffected, err := c.Result.RowsAffected(); err == nil {
trace.Logf(c.Ctx, "rows affected", "%d", rowsAffected)
}
if lastID, err := c.Result.LastInsertId(); err == nil {
trace.Logf(c.Ctx, "last insert id", "%d", lastID)
}
}
c.Ctx.Value(sqlTask{}).(*trace.Task).End() c.Ctx.Value(sqlTask{}).(*trace.Task).End()
return nil return nil
} }

View file

@ -46,7 +46,11 @@ func ProtocolMiddlewares() (handlers []any) {
defer finished() defer finished()
trace.Log(ctx, "method", req.Method) trace.Log(ctx, "method", req.Method)
trace.Log(ctx, "url", req.RequestURI) trace.Log(ctx, "url", req.RequestURI)
next.ServeHTTP(context.WrapResponseWriter(resp), req.WithContext(cache.WithCacheContext(ctx)))
respWriter := context.WrapResponseWriter(resp)
next.ServeHTTP(respWriter, req.WithContext(cache.WithCacheContext(ctx)))
trace.Logf(ctx, "status", "%d", respWriter.WrittenStatus())
}) })
}) })