@@ -7,23 +7,36 @@ import (
77 "context"
88 "time"
99
10+ "code.gitea.io/gitea/modules/gtprof"
1011 "code.gitea.io/gitea/modules/log"
12+ "code.gitea.io/gitea/modules/setting"
1113
1214 "xorm.io/xorm/contexts"
1315)
1416
15- type SlowQueryHook struct {
17+ type EngineHook struct {
1618 Threshold time.Duration
1719 Logger log.Logger
1820}
1921
20- var _ contexts.Hook = (* SlowQueryHook )(nil )
22+ var _ contexts.Hook = (* EngineHook )(nil )
2123
22- func (* SlowQueryHook ) BeforeProcess (c * contexts.ContextHook ) (context.Context , error ) {
23- return c .Ctx , nil
24+ func (* EngineHook ) BeforeProcess (c * contexts.ContextHook ) (context.Context , error ) {
25+ ctx , _ := gtprof .GetTracer ().Start (c .Ctx , gtprof .TraceSpanDatabase )
26+ return ctx , nil
2427}
2528
26- func (h * SlowQueryHook ) AfterProcess (c * contexts.ContextHook ) error {
29+ func (h * EngineHook ) AfterProcess (c * contexts.ContextHook ) error {
30+ span := gtprof .GetContextSpan (c .Ctx )
31+ if span != nil {
32+ // Do not record SQL parameters here:
33+ // * It shouldn't expose the parameters because they contain sensitive information, end users need to report the trace details safely.
34+ // * Some parameters contain quite long texts, waste memory and are difficult to display.
35+ span .SetAttributeString (gtprof .TraceAttrDbSQL , c .SQL )
36+ span .End ()
37+ } else {
38+ setting .PanicInDevOrTesting ("span in database engine hook is nil" )
39+ }
2740 if c .ExecuteTime >= h .Threshold {
2841 // 8 is the amount of skips passed to runtime.Caller, so that in the log the correct function
2942 // is being displayed (the function that ultimately wants to execute the query in the code)
0 commit comments